| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package backend |
| 6 |
| 7 import ( |
| 8 "net/url" |
| 9 |
| 10 "golang.org/x/net/context" |
| 11 ) |
| 12 |
| 13 // FormatSpec is a specification for formatted data. |
| 14 type FormatSpec struct { |
| 15 // Formatter is the supported destination Resolver format for this item. |
| 16 // Backends (notably the FormatterBackend) may project the Item into thi
s |
| 17 // format. |
| 18 // |
| 19 // An empty string means the original config service format. |
| 20 Formatter string |
| 21 |
| 22 // Data is additional format data describing the type. It may be empty. |
| 23 Data string |
| 24 } |
| 25 |
| 26 // Unformatted retrns true if fs does not specify a format. |
| 27 func (fs *FormatSpec) Unformatted() bool { return fs.Formatter == "" } |
| 28 |
| 29 // Item is a single config item. It is used to pass configuration data |
| 30 // between Backend instances. |
| 31 type Item struct { |
| 32 Meta |
| 33 |
| 34 Content string |
| 35 |
| 36 // FormatSpec, if non-empty, qualifies the format of the Content. |
| 37 FormatSpec FormatSpec |
| 38 } |
| 39 |
| 40 // Params are parameters supplied to Backend methods. They are generated |
| 41 // by the main interface user-facing methods (config.go) |
| 42 type Params struct { |
| 43 // Authority is the authority to use in the request. |
| 44 Authority Authority |
| 45 // Content, if true, indicates that config content should also be fetche
d. |
| 46 // Otherwise, only the content hash needs to be returned. |
| 47 Content bool |
| 48 |
| 49 // FormatSpec, if non-empty, qualifies the format of the Content. |
| 50 FormatSpec FormatSpec |
| 51 } |
| 52 |
| 53 // B is a configuration backend interface. |
| 54 type B interface { |
| 55 // ServiceURL returns the service URL. |
| 56 ServiceURL(context.Context) url.URL |
| 57 |
| 58 // Get retrieves a single configuration. |
| 59 Get(c context.Context, configSet, path string, p Params) (*Item, error) |
| 60 |
| 61 // GetAll retrieves all configurations of a given type. |
| 62 GetAll(c context.Context, t GetAllTarget, path string, p Params) ([]*Ite
m, error) |
| 63 |
| 64 // ConfigSetURL returns the URL for the specified config set. |
| 65 ConfigSetURL(c context.Context, configSet string, p Params) (url.URL, er
ror) |
| 66 } |
| 67 |
| 68 // Factory is a function that generates a B given a Context. |
| 69 type Factory func(context.Context) B |
| 70 |
| 71 // configBackendKey is the Context key for the configuration backend. |
| 72 var configBackendKey = "github.com/luci/luci-go/server/config:backend" |
| 73 |
| 74 // WithBackend returns a derivative Context with the supplied Backend installed. |
| 75 func WithBackend(c context.Context, b B) context.Context { |
| 76 return WithFactory(c, func(context.Context) B { return b }) |
| 77 } |
| 78 |
| 79 // WithFactory returns a derivative Context with the supplied BackendFactory |
| 80 // installed. |
| 81 func WithFactory(c context.Context, f Factory) context.Context { |
| 82 return context.WithValue(c, &configBackendKey, f) |
| 83 } |
| 84 |
| 85 // Get returns the Backend that is installed into the Context. |
| 86 // |
| 87 // If no Backend is installed in the Context, Get will panic. |
| 88 func Get(c context.Context) B { |
| 89 if f, ok := c.Value(&configBackendKey).(Factory); ok { |
| 90 return f(c) |
| 91 } |
| 92 panic("no Backend factory is installed in the Context") |
| 93 } |
| OLD | NEW |