Chromium Code Reviews| Index: server/config/backend.go |
| diff --git a/server/config/backend.go b/server/config/backend.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..857d5c9fd77c0750036f1ddb141ff202b6e97f97 |
| --- /dev/null |
| +++ b/server/config/backend.go |
| @@ -0,0 +1,85 @@ |
| +// Copyright 2016 The LUCI Authors. All rights reserved. |
| +// Use of this source code is governed under the Apache License, Version 2.0 |
| +// that can be found in the LICENSE file. |
| + |
| +package config |
| + |
| +import ( |
| + "net/url" |
| + |
| + "golang.org/x/net/context" |
| +) |
| + |
| +// Item is a single config item. It is used to pass configuration data |
| +// between Backend instances. |
| +type Item struct { |
| + // Meta is this config item's metadata. |
| + Meta |
| + |
| + // Content is the config item's content. |
|
iannucci
2017/01/07 20:12:15
This comment and the one above are pretty self-evi
dnj
2017/01/10 03:25:57
Done.
|
| + Content string |
| + |
| + // Format, if true, is a string key provided by the Resolver that |
| + // resolved this item used to indicate the item's cache format. |
| + // |
| + // If this is empty, the Content is varbatim from the config service. |
| + Format string |
| + // ForamtData is auxiliary data to supply to the Formatter. |
|
iannucci
2017/01/07 20:12:15
Format
dnj
2017/01/10 03:25:57
Done.
|
| + FormatData string |
| +} |
| + |
| +// Params are backend parameters. |
|
iannucci
2017/01/07 20:12:15
It would be helpful to know what these parameters
dnj
2017/01/10 03:25:57
Done.
|
| +type Params struct { |
| + // Authority is the authority to use in the request. |
| + Authority Authority |
| + // Content, if true, indicates that config content should also be fetched. |
| + // Otherwise, only the content hash needs to be returned. |
| + Content bool |
|
iannucci
2017/01/07 20:12:15
the other possibility would be a regexp-style coll
dnj
2017/01/10 03:25:57
I think this isn't bad. ATM it's explicit - add th
|
| + |
| + // Format is the supported destination Resolver format for this item. Backends |
| + // (notably the FormatterBackend) may project the Item into this format. |
| + // |
| + // An empty string means the original config service format. |
| + Format string |
|
iannucci
2017/01/07 20:12:15
would it be worth making a small type for this (e.
dnj
2017/01/10 03:25:57
Did this, called it FormatSpec.
|
| + // FormatData is additional format data describing the type. It may be empty. |
|
iannucci
2017/01/07 20:12:15
I think this is opaque to this package and is pass
dnj
2017/01/10 03:25:57
Yep
|
| + FormatData string |
| +} |
| + |
| +// Backend is a configuration backend interface. |
|
iannucci
2017/01/07 20:12:15
this is also pretty tautological :)
dnj
2017/01/10 03:25:57
Acknowledged.
|
| +type Backend interface { |
| + // ServiceURL returns the service URL. |
| + ServiceURL(context.Context) url.URL |
| + |
| + // Get retrieves a single configuration. |
| + Get(c context.Context, configSet, path string, p Params) (*Item, error) |
| + |
| + // GetAll retrieves all configurations of a given type. |
| + GetAll(c context.Context, t GetAllType, path string, p Params) ([]*Item, error) |
| + |
| + // ConfigSetURL returns the URL for the specified config set. |
| + ConfigSetURL(c context.Context, a Authority, configSet string) (url.URL, error) |
| +} |
| + |
| +// BackendFactory is a function that generates a Backend given a Context. |
| +type BackendFactory func(context.Context) Backend |
| + |
| +// configBackendKey is the Context key for the configuration backend. |
| +var configBackendKey = "github.com/luci/luci-go/server/config:backend" |
| + |
| +// WithBackend returns a derivative Context with the supplied Backend installed. |
| +func WithBackend(c context.Context, b Backend) context.Context { |
| + return WithBackendFactory(c, func(context.Context) Backend { return b }) |
| +} |
| + |
| +// WithBackendFactory returns a derivative Context with the supplied |
| +// BackendFactory installed. |
| +func WithBackendFactory(c context.Context, f BackendFactory) context.Context { |
| + return context.WithValue(c, &configBackendKey, f) |
| +} |
| + |
| +func getBackend(c context.Context) Backend { |
| + if f, ok := c.Value(&configBackendKey).(BackendFactory); ok { |
| + return f(c) |
| + } |
| + panic("no Backend factory is installed in the Context") |
| +} |