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 "net/http" | 8 "net/http" |
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 gOAuth "golang.org/x/oauth2/google" |
12 "google.golang.org/appengine" | 13 "google.golang.org/appengine" |
| 14 "google.golang.org/appengine/remote_api" |
13 ) | 15 ) |
14 | 16 |
15 type key int | 17 type key int |
16 | 18 |
17 var ( | 19 var ( |
18 prodContextKey key | 20 prodContextKey key |
19 prodContextNoTxnKey key = 1 | 21 prodContextNoTxnKey key = 1 |
20 probeCacheKey key = 2 | 22 probeCacheKey key = 2 |
21 ) | 23 ) |
22 | 24 |
(...skipping 17 matching lines...) Expand all Loading... |
40 aeCtx, err := appengine.Namespace(aeCtx, info.Get(c).GetNamespace()) | 42 aeCtx, err := appengine.Namespace(aeCtx, info.Get(c).GetNamespace()) |
41 if err != nil { | 43 if err != nil { |
42 panic(err) | 44 panic(err) |
43 } | 45 } |
44 if deadline, ok := c.Deadline(); ok { | 46 if deadline, ok := c.Deadline(); ok { |
45 aeCtx, _ = context.WithDeadline(aeCtx, deadline) | 47 aeCtx, _ = context.WithDeadline(aeCtx, deadline) |
46 } | 48 } |
47 return aeCtx | 49 return aeCtx |
48 } | 50 } |
49 | 51 |
| 52 func setupAECtx(c, aeCtx context.Context) context.Context { |
| 53 c = context.WithValue(c, prodContextKey, aeCtx) |
| 54 c = context.WithValue(c, prodContextNoTxnKey, aeCtx) |
| 55 return useMail(useUser(useURLFetch(useRDS(useMC(useTQ(useGI(useLogging(c
)))))))) |
| 56 } |
| 57 |
50 // Use adds production implementations for all the gae services to the | 58 // Use adds production implementations for all the gae services to the |
51 // context. | 59 // context. |
52 // | 60 // |
53 // The services added are: | 61 // The services added are: |
54 // - github.com/luci-go/common/logging | 62 // - github.com/luci-go/common/logging |
55 // - github.com/luci/gae/service/datastore | 63 // - github.com/luci/gae/service/datastore |
56 // - github.com/luci/gae/service/info | 64 // - github.com/luci/gae/service/info |
57 // - github.com/luci/gae/service/mail | 65 // - github.com/luci/gae/service/mail |
58 // - github.com/luci/gae/service/memcache | 66 // - github.com/luci/gae/service/memcache |
59 // - github.com/luci/gae/service/taskqueue | 67 // - github.com/luci/gae/service/taskqueue |
60 // - github.com/luci/gae/service/urlfetch | 68 // - github.com/luci/gae/service/urlfetch |
61 // - github.com/luci/gae/service/user | 69 // - github.com/luci/gae/service/user |
62 // | 70 // |
63 // These can be retrieved with the <service>.Get functions. | 71 // These can be retrieved with the <service>.Get functions. |
64 // | 72 // |
65 // The implementations are all backed by the real appengine SDK functionality, | 73 // The implementations are all backed by the real appengine SDK functionality, |
66 func Use(c context.Context, r *http.Request) context.Context { | 74 func Use(c context.Context, r *http.Request) context.Context { |
67 » aeCtx := appengine.NewContext(r) | 75 » return setupAECtx(c, appengine.NewContext(r)) |
68 » c = context.WithValue(c, prodContextKey, aeCtx) | |
69 » c = context.WithValue(c, prodContextNoTxnKey, aeCtx) | |
70 » return useMail(useUser(useURLFetch(useRDS(useMC(useTQ(useGI(useLogging(c
)))))))) | |
71 } | 76 } |
| 77 |
| 78 // UseRemote is the same as Use, except that it lets you attach a context to |
| 79 // a remote host using the Remote API feature. See the docs for the |
| 80 // prerequisites. |
| 81 // |
| 82 // docs: https://cloud.google.com/appengine/docs/go/tools/remoteapi |
| 83 // |
| 84 // If client is nil, this will use a default Google OAuth2 client. Otherwise the |
| 85 // client must be configured to have the following OAuth2 scopes: |
| 86 // "https://www.googleapis.com/auth/appengine.apis" |
| 87 // "https://www.googleapis.com/auth/userinfo.email" |
| 88 // "https://www.googleapis.com/auth/cloud.platform" |
| 89 func UseRemote(c context.Context, host string, client *http.Client) (context.Con
text, error) { |
| 90 err := error(nil) |
| 91 if client == nil { |
| 92 client, err = gOAuth.DefaultClient(context.Background(), |
| 93 "https://www.googleapis.com/auth/appengine.apis", |
| 94 "https://www.googleapis.com/auth/userinfo.email", |
| 95 "https://www.googleapis.com/auth/cloud.platform", |
| 96 ) |
| 97 if err != nil { |
| 98 return nil, err |
| 99 } |
| 100 } |
| 101 |
| 102 aeCtx, err := remote_api.NewRemoteContext(host, client) |
| 103 if err != nil { |
| 104 return nil, err |
| 105 } |
| 106 return setupAECtx(c, aeCtx), nil |
| 107 } |
OLD | NEW |