OLD | NEW |
1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
4 | 4 |
5 package memory | 5 package memory |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "regexp" | |
10 | 9 |
11 "github.com/luci/gae/impl/dummy" | 10 "github.com/luci/gae/impl/dummy" |
12 "github.com/luci/gae/service/info" | 11 "github.com/luci/gae/service/info" |
| 12 "github.com/luci/gae/service/info/support" |
| 13 |
13 "golang.org/x/net/context" | 14 "golang.org/x/net/context" |
14 ) | 15 ) |
15 | 16 |
16 type giContextKeyType int | 17 type giContextKeyType int |
17 | 18 |
18 var giContextKey giContextKeyType | 19 var giContextKey giContextKeyType |
19 | 20 |
20 // validNamespace matches valid namespace names. | |
21 var validNamespace = regexp.MustCompile(`^[0-9A-Za-z._-]{0,100}$`) | |
22 | |
23 var defaultGlobalInfoData = globalInfoData{ | 21 var defaultGlobalInfoData = globalInfoData{ |
24 // versionID returns X.Y where Y is autogenerated by appengine, and X is | 22 // versionID returns X.Y where Y is autogenerated by appengine, and X is |
25 // whatever's in app.yaml. | 23 // whatever's in app.yaml. |
26 versionID: "testVersionID.1", | 24 versionID: "testVersionID.1", |
27 requestID: "test-request-id", | 25 requestID: "test-request-id", |
28 } | 26 } |
29 | 27 |
30 type globalInfoData struct { | 28 type globalInfoData struct { |
31 appID string | 29 appID string |
32 fqAppID string | 30 fqAppID string |
33 » namespace *string | 31 » namespace string |
34 versionID string | 32 versionID string |
35 requestID string | 33 requestID string |
36 } | 34 } |
37 | 35 |
38 func (gid *globalInfoData) getNamespace() (string, bool) { | |
39 if ns := gid.namespace; ns != nil { | |
40 return *ns, true | |
41 } | |
42 return "", false | |
43 } | |
44 | |
45 func curGID(c context.Context) *globalInfoData { | 36 func curGID(c context.Context) *globalInfoData { |
46 if gid, ok := c.Value(giContextKey).(*globalInfoData); ok { | 37 if gid, ok := c.Value(giContextKey).(*globalInfoData); ok { |
47 return gid | 38 return gid |
48 } | 39 } |
49 return &defaultGlobalInfoData | 40 return &defaultGlobalInfoData |
50 } | 41 } |
51 | 42 |
52 func useGID(c context.Context, f func(mod *globalInfoData)) context.Context { | 43 func useGID(c context.Context, f func(mod *globalInfoData)) context.Context { |
53 cur := curGID(c) | 44 cur := curGID(c) |
54 if cur == nil { | 45 if cur == nil { |
(...skipping 14 matching lines...) Expand all Loading... |
69 } | 60 } |
70 | 61 |
71 type giImpl struct { | 62 type giImpl struct { |
72 info.RawInterface | 63 info.RawInterface |
73 *globalInfoData | 64 *globalInfoData |
74 c context.Context | 65 c context.Context |
75 } | 66 } |
76 | 67 |
77 var _ = info.RawInterface((*giImpl)(nil)) | 68 var _ = info.RawInterface((*giImpl)(nil)) |
78 | 69 |
79 func (gi *giImpl) GetNamespace() (string, bool) { | 70 func (gi *giImpl) GetNamespace() string { return gi.namespace } |
80 » return gi.getNamespace() | |
81 } | |
82 | 71 |
83 func (gi *giImpl) Namespace(ns string) (ret context.Context, err error) { | 72 func (gi *giImpl) Namespace(ns string) (context.Context, error) { |
84 » if !validNamespace.MatchString(ns) { | 73 » if err := support.ValidNamespace(ns); err != nil { |
85 » » return nil, fmt.Errorf("appengine: namespace %q does not match /
%s/", ns, validNamespace) | 74 » » return gi.c, err |
86 } | 75 } |
87 | 76 |
88 return useGID(gi.c, func(mod *globalInfoData) { | 77 return useGID(gi.c, func(mod *globalInfoData) { |
89 » » mod.namespace = &ns | 78 » » mod.namespace = ns |
90 }), nil | 79 }), nil |
91 } | 80 } |
92 | 81 |
93 func (gi *giImpl) AppID() string { | 82 func (gi *giImpl) AppID() string { |
94 return gi.appID | 83 return gi.appID |
95 } | 84 } |
96 | 85 |
97 func (gi *giImpl) FullyQualifiedAppID() string { | 86 func (gi *giImpl) FullyQualifiedAppID() string { |
98 return gi.fqAppID | 87 return gi.fqAppID |
99 } | 88 } |
100 | 89 |
101 func (gi *giImpl) DefaultVersionHostname() string { | 90 func (gi *giImpl) DefaultVersionHostname() string { |
102 return fmt.Sprintf("%s.example.com", gi.appID) | 91 return fmt.Sprintf("%s.example.com", gi.appID) |
103 } | 92 } |
104 | 93 |
105 func (gi *giImpl) IsDevAppServer() bool { | 94 func (gi *giImpl) IsDevAppServer() bool { |
106 return true | 95 return true |
107 } | 96 } |
108 | 97 |
109 func (gi *giImpl) VersionID() string { | 98 func (gi *giImpl) VersionID() string { |
110 return curGID(gi.c).versionID | 99 return curGID(gi.c).versionID |
111 } | 100 } |
112 | 101 |
113 func (gi *giImpl) RequestID() string { | 102 func (gi *giImpl) RequestID() string { |
114 return curGID(gi.c).requestID | 103 return curGID(gi.c).requestID |
115 } | 104 } |
116 | 105 |
117 func (gi *giImpl) Testable() info.Testable { | 106 func (gi *giImpl) GetTestable() info.Testable { |
118 return gi | 107 return gi |
119 } | 108 } |
120 | 109 |
121 func (gi *giImpl) SetVersionID(v string) context.Context { | 110 func (gi *giImpl) SetVersionID(v string) context.Context { |
122 return useGID(gi.c, func(mod *globalInfoData) { | 111 return useGID(gi.c, func(mod *globalInfoData) { |
123 mod.versionID = v | 112 mod.versionID = v |
124 }) | 113 }) |
125 } | 114 } |
126 | 115 |
127 func (gi *giImpl) SetRequestID(v string) context.Context { | 116 func (gi *giImpl) SetRequestID(v string) context.Context { |
128 return useGID(gi.c, func(mod *globalInfoData) { | 117 return useGID(gi.c, func(mod *globalInfoData) { |
129 mod.requestID = v | 118 mod.requestID = v |
130 }) | 119 }) |
131 } | 120 } |
OLD | NEW |