Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(203)

Side by Side Diff: server/config/backend.go

Issue 2580713002: Implement a server-side config service interface. (Closed)
Patch Set: Update MultiResolver interface, add test for MultiError. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 config
6
7 import (
8 "net/url"
9
10 "golang.org/x/net/context"
11 )
12
13 // Item is a single config item. It is used to pass configuration data
14 // between Backend instances.
15 type Item struct {
16 // Meta is this config item's metadata.
17 Meta
18
19 // 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.
20 Content string
21
22 // Format, if true, is a string key provided by the Resolver that
23 // resolved this item used to indicate the item's cache format.
24 //
25 // If this is empty, the Content is varbatim from the config service.
26 Format string
27 // 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.
28 FormatData string
29 }
30
31 // 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.
32 type Params struct {
33 // Authority is the authority to use in the request.
34 Authority Authority
35 // Content, if true, indicates that config content should also be fetche d.
36 // Otherwise, only the content hash needs to be returned.
37 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
38
39 // Format is the supported destination Resolver format for this item. Ba ckends
40 // (notably the FormatterBackend) may project the Item into this format.
41 //
42 // An empty string means the original config service format.
43 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.
44 // FormatData is additional format data describing the type. It may be e mpty.
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
45 FormatData string
46 }
47
48 // 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.
49 type Backend interface {
50 // ServiceURL returns the service URL.
51 ServiceURL(context.Context) url.URL
52
53 // Get retrieves a single configuration.
54 Get(c context.Context, configSet, path string, p Params) (*Item, error)
55
56 // GetAll retrieves all configurations of a given type.
57 GetAll(c context.Context, t GetAllType, path string, p Params) ([]*Item, error)
58
59 // ConfigSetURL returns the URL for the specified config set.
60 ConfigSetURL(c context.Context, a Authority, configSet string) (url.URL, error)
61 }
62
63 // BackendFactory is a function that generates a Backend given a Context.
64 type BackendFactory func(context.Context) Backend
65
66 // configBackendKey is the Context key for the configuration backend.
67 var configBackendKey = "github.com/luci/luci-go/server/config:backend"
68
69 // WithBackend returns a derivative Context with the supplied Backend installed.
70 func WithBackend(c context.Context, b Backend) context.Context {
71 return WithBackendFactory(c, func(context.Context) Backend { return b })
72 }
73
74 // WithBackendFactory returns a derivative Context with the supplied
75 // BackendFactory installed.
76 func WithBackendFactory(c context.Context, f BackendFactory) context.Context {
77 return context.WithValue(c, &configBackendKey, f)
78 }
79
80 func getBackend(c context.Context) Backend {
81 if f, ok := c.Value(&configBackendKey).(BackendFactory); ok {
82 return f(c)
83 }
84 panic("no Backend factory is installed in the Context")
85 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698