| 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 prod | 5 package prod |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "time" | 8 "time" |
| 9 | 9 |
| 10 "github.com/luci/gae/service/info" | 10 "github.com/luci/gae/service/info" |
| 11 "golang.org/x/net/context" | 11 "golang.org/x/net/context" |
| 12 "google.golang.org/appengine" | 12 "google.golang.org/appengine" |
| 13 ) | 13 ) |
| 14 | 14 |
| 15 type key int |
| 16 |
| 17 var namespaceCopyKey key |
| 18 |
| 15 // useGI adds a gae.GlobalInfo implementation to context, accessible | 19 // useGI adds a gae.GlobalInfo implementation to context, accessible |
| 16 // by gae.GetGI(c) | 20 // by gae.GetGI(c) |
| 17 func useGI(c context.Context) context.Context { | 21 func useGI(c context.Context) context.Context { |
| 22 // TODO(iannucci): make a better way to get the initial namespace? |
| 23 c = context.WithValue(c, namespaceCopyKey, "") |
| 18 return info.SetFactory(c, func(ci context.Context) info.Interface { | 24 return info.SetFactory(c, func(ci context.Context) info.Interface { |
| 19 return giImpl{ci} | 25 return giImpl{ci} |
| 20 }) | 26 }) |
| 21 } | 27 } |
| 22 | 28 |
| 23 type giImpl struct{ context.Context } | 29 type giImpl struct{ context.Context } |
| 24 | 30 |
| 25 func (g giImpl) AccessToken(scopes ...string) (token string, expiry time.Time, e
rr error) { | 31 func (g giImpl) AccessToken(scopes ...string) (token string, expiry time.Time, e
rr error) { |
| 26 return appengine.AccessToken(g, scopes...) | 32 return appengine.AccessToken(g, scopes...) |
| 27 } | 33 } |
| 28 func (g giImpl) AppID() string { | 34 func (g giImpl) AppID() string { |
| 29 return appengine.AppID(g) | 35 return appengine.AppID(g) |
| 30 } | 36 } |
| 37 func (g giImpl) GetNamespace() string { |
| 38 return g.Value(namespaceCopyKey).(string) |
| 39 } |
| 31 func (g giImpl) Datacenter() string { | 40 func (g giImpl) Datacenter() string { |
| 32 return appengine.Datacenter(g) | 41 return appengine.Datacenter(g) |
| 33 } | 42 } |
| 34 func (g giImpl) DefaultVersionHostname() string { | 43 func (g giImpl) DefaultVersionHostname() string { |
| 35 return appengine.DefaultVersionHostname(g) | 44 return appengine.DefaultVersionHostname(g) |
| 36 } | 45 } |
| 37 func (g giImpl) InstanceID() string { | 46 func (g giImpl) InstanceID() string { |
| 38 return appengine.InstanceID() | 47 return appengine.InstanceID() |
| 39 } | 48 } |
| 40 func (g giImpl) IsDevAppServer() bool { | 49 func (g giImpl) IsDevAppServer() bool { |
| 41 return appengine.IsDevAppServer() | 50 return appengine.IsDevAppServer() |
| 42 } | 51 } |
| 43 func (g giImpl) IsOverQuota(err error) bool { | 52 func (g giImpl) IsOverQuota(err error) bool { |
| 44 return appengine.IsOverQuota(err) | 53 return appengine.IsOverQuota(err) |
| 45 } | 54 } |
| 46 func (g giImpl) IsTimeoutError(err error) bool { | 55 func (g giImpl) IsTimeoutError(err error) bool { |
| 47 return appengine.IsTimeoutError(err) | 56 return appengine.IsTimeoutError(err) |
| 48 } | 57 } |
| 49 func (g giImpl) ModuleHostname(module, version, instance string) (string, error)
{ | 58 func (g giImpl) ModuleHostname(module, version, instance string) (string, error)
{ |
| 50 return appengine.ModuleHostname(g, module, version, instance) | 59 return appengine.ModuleHostname(g, module, version, instance) |
| 51 } | 60 } |
| 52 func (g giImpl) ModuleName() (name string) { | 61 func (g giImpl) ModuleName() (name string) { |
| 53 return appengine.ModuleName(g) | 62 return appengine.ModuleName(g) |
| 54 } | 63 } |
| 55 func (g giImpl) Namespace(namespace string) (context.Context, error) { | 64 func (g giImpl) Namespace(namespace string) (context.Context, error) { |
| 56 » return appengine.Namespace(g, namespace) | 65 » c, err := appengine.Namespace(g, namespace) |
| 66 » if err != nil { |
| 67 » » return c, err |
| 68 » } |
| 69 » return context.WithValue(c, namespaceCopyKey, namespace), nil |
| 57 } | 70 } |
| 58 func (g giImpl) PublicCertificates() ([]info.Certificate, error) { | 71 func (g giImpl) PublicCertificates() ([]info.Certificate, error) { |
| 59 certs, err := appengine.PublicCertificates(g) | 72 certs, err := appengine.PublicCertificates(g) |
| 60 if err != nil { | 73 if err != nil { |
| 61 return nil, err | 74 return nil, err |
| 62 } | 75 } |
| 63 ret := make([]info.Certificate, len(certs)) | 76 ret := make([]info.Certificate, len(certs)) |
| 64 for i, c := range certs { | 77 for i, c := range certs { |
| 65 ret[i] = info.Certificate(c) | 78 ret[i] = info.Certificate(c) |
| 66 } | 79 } |
| 67 return ret, nil | 80 return ret, nil |
| 68 } | 81 } |
| 69 func (g giImpl) RequestID() string { | 82 func (g giImpl) RequestID() string { |
| 70 return appengine.RequestID(g) | 83 return appengine.RequestID(g) |
| 71 } | 84 } |
| 72 func (g giImpl) ServerSoftware() string { | 85 func (g giImpl) ServerSoftware() string { |
| 73 return appengine.ServerSoftware() | 86 return appengine.ServerSoftware() |
| 74 } | 87 } |
| 75 func (g giImpl) ServiceAccount() (string, error) { | 88 func (g giImpl) ServiceAccount() (string, error) { |
| 76 return appengine.ServiceAccount(g) | 89 return appengine.ServiceAccount(g) |
| 77 } | 90 } |
| 78 func (g giImpl) SignBytes(bytes []byte) (keyName string, signature []byte, err e
rror) { | 91 func (g giImpl) SignBytes(bytes []byte) (keyName string, signature []byte, err e
rror) { |
| 79 return appengine.SignBytes(g, bytes) | 92 return appengine.SignBytes(g, bytes) |
| 80 } | 93 } |
| 81 func (g giImpl) VersionID() string { | 94 func (g giImpl) VersionID() string { |
| 82 return appengine.VersionID(g) | 95 return appengine.VersionID(g) |
| 83 } | 96 } |
| OLD | NEW |