OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // 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" | 9 "regexp" |
10 | 10 |
11 "github.com/luci/gae/impl/dummy" | 11 "github.com/luci/gae/impl/dummy" |
12 "github.com/luci/gae/service/info" | 12 "github.com/luci/gae/service/info" |
13 "golang.org/x/net/context" | 13 "golang.org/x/net/context" |
14 ) | 14 ) |
15 | 15 |
16 type giContextKeyType int | 16 type giContextKeyType int |
17 | 17 |
18 var giContextKey giContextKeyType | 18 var giContextKey giContextKeyType |
19 | 19 |
20 // validNamespace matches valid namespace names. | 20 // validNamespace matches valid namespace names. |
21 var validNamespace = regexp.MustCompile(`^[0-9A-Za-z._-]{0,100}$`) | 21 var validNamespace = regexp.MustCompile(`^[0-9A-Za-z._-]{0,100}$`) |
22 | 22 |
23 func curGID(c context.Context) *globalInfoData { | 23 func curGID(c context.Context) *globalInfoData { |
24 return c.Value(giContextKey).(*globalInfoData) | 24 return c.Value(giContextKey).(*globalInfoData) |
25 } | 25 } |
26 | 26 |
27 // useGI adds a gae.GlobalInfo context, accessible | 27 // useGI adds a gae.GlobalInfo context, accessible |
28 // by gae.GetGI(c) | 28 // by gae.GetGI(c) |
29 func useGI(c context.Context) context.Context { | 29 func useGI(c context.Context, appID string) context.Context { |
30 return info.SetFactory(c, func(ic context.Context) info.Interface { | 30 return info.SetFactory(c, func(ic context.Context) info.Interface { |
31 return &giImpl{dummy.Info(), curGID(ic), ic} | 31 return &giImpl{dummy.Info(), curGID(ic), ic} |
32 }) | 32 }) |
33 } | 33 } |
34 | 34 |
35 // globalAppID is the 'AppID' of everythin returned from this memory | |
36 // implementation (DSKeys, GlobalInfo, etc.). There's no way to modify this | |
37 // value through the API, and there are a couple bits of code where it's hard to | |
38 // route this value through to without making the internal APIs really complex. | |
39 const globalAppID = "dev~app" | |
40 | |
41 type globalInfoData struct { | 35 type globalInfoData struct { |
| 36 appid string |
42 namespace string | 37 namespace string |
43 } | 38 } |
44 | 39 |
45 type giImpl struct { | 40 type giImpl struct { |
46 info.Interface | 41 info.Interface |
47 *globalInfoData | 42 *globalInfoData |
48 c context.Context | 43 c context.Context |
49 } | 44 } |
50 | 45 |
51 var _ = info.Interface((*giImpl)(nil)) | 46 var _ = info.Interface((*giImpl)(nil)) |
52 | 47 |
53 func (gi *giImpl) GetNamespace() string { | 48 func (gi *giImpl) GetNamespace() string { |
54 return gi.namespace | 49 return gi.namespace |
55 } | 50 } |
56 | 51 |
57 func (gi *giImpl) Namespace(ns string) (ret context.Context, err error) { | 52 func (gi *giImpl) Namespace(ns string) (ret context.Context, err error) { |
58 if !validNamespace.MatchString(ns) { | 53 if !validNamespace.MatchString(ns) { |
59 return nil, fmt.Errorf("appengine: namespace %q does not match /
%s/", ns, validNamespace) | 54 return nil, fmt.Errorf("appengine: namespace %q does not match /
%s/", ns, validNamespace) |
60 } | 55 } |
61 » return context.WithValue(gi.c, giContextKey, &globalInfoData{ns}), nil | 56 » return context.WithValue(gi.c, giContextKey, &globalInfoData{gi.appid, n
s}), nil |
62 } | 57 } |
63 | 58 |
64 func (gi *giImpl) AppID() string { | 59 func (gi *giImpl) AppID() string { |
65 » return globalAppID | 60 » return gi.appid |
66 } | 61 } |
67 | 62 |
68 func (gi *giImpl) FullyQualifiedAppID() string { | 63 func (gi *giImpl) FullyQualifiedAppID() string { |
69 » return globalAppID | 64 » return gi.appid |
70 } | 65 } |
71 | 66 |
72 func (gi *giImpl) IsDevAppServer() bool { | 67 func (gi *giImpl) IsDevAppServer() bool { |
73 return true | 68 return true |
74 } | 69 } |
OLD | NEW |