OLD | NEW |
1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 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 cloud | 5 package cloud |
6 | 6 |
7 import ( | 7 import ( |
| 8 "github.com/luci/gae/impl/dummy" |
| 9 ds "github.com/luci/gae/service/datastore" |
| 10 "github.com/luci/gae/service/mail" |
| 11 mc "github.com/luci/gae/service/memcache" |
| 12 "github.com/luci/gae/service/module" |
| 13 "github.com/luci/gae/service/taskqueue" |
| 14 "github.com/luci/gae/service/user" |
| 15 |
8 "cloud.google.com/go/datastore" | 16 "cloud.google.com/go/datastore" |
| 17 "github.com/bradfitz/gomemcache/memcache" |
9 | 18 |
10 "golang.org/x/net/context" | 19 "golang.org/x/net/context" |
11 ) | 20 ) |
12 | 21 |
13 // Use installs the cloud services implementation into the supplied Context. | 22 // Config is a full-stack cloud service configuration. A user can selectively |
| 23 // populate its fields, and services for the populated fields will be installed |
| 24 // in the Context and available. |
14 // | 25 // |
15 // This includes: | 26 // Because the "impl/cloud" service collection is a composite set of cloud |
16 //» - github.com/luci/gae/service/info | 27 // services, the user can choose services based on their configuration. |
17 //» - github.com/luci/gae/service/datastore | 28 type Config struct { |
18 // | 29 » // DS is the cloud datastore client. If populated, the datastore service
will |
19 // This is built around the ability to use cloud datastore. | 30 » // be installed. |
20 func Use(c context.Context, client *datastore.Client) context.Context { | 31 » DS *datastore.Client |
21 » return UseDS(useInfo(c), client) | 32 |
| 33 » // MC is the memcache service client. If populated, the memcache service
will |
| 34 » // be installed. |
| 35 » MC *memcache.Client |
22 } | 36 } |
23 | 37 |
24 // UseDS installs the cloud datastore implementation into the supplied Context. | 38 // Use installs the Config into the supplied Context. Services will be installed |
25 func UseDS(c context.Context, client *datastore.Client) context.Context { | 39 // based on the fields that are populated in Config. |
26 » cds := cloudDatastore{ | 40 // |
27 » » client: client, | 41 // Any services that are missing will have "impl/dummy" stubs installed. These |
| 42 // stubs will panic if called. |
| 43 func (cfg Config) Use(c context.Context) context.Context { |
| 44 » // Dummy services that we don't support. |
| 45 » c = mail.Set(c, dummy.Mail()) |
| 46 » c = module.Set(c, dummy.Module()) |
| 47 » c = taskqueue.SetRaw(c, dummy.TaskQueue()) |
| 48 » c = user.Set(c, dummy.User()) |
| 49 |
| 50 » c = useInfo(c) |
| 51 |
| 52 » // datastore service |
| 53 » if cfg.DS != nil { |
| 54 » » cds := cloudDatastore{ |
| 55 » » » client: cfg.DS, |
| 56 » » } |
| 57 » » c = cds.use(c) |
| 58 » } else { |
| 59 » » c = ds.SetRaw(c, dummy.Datastore()) |
28 } | 60 } |
29 » return cds.use(c) | 61 |
| 62 » // memcache service |
| 63 » if cfg.MC != nil { |
| 64 » » mc := memcacheClient{ |
| 65 » » » client: cfg.MC, |
| 66 » » } |
| 67 » » c = mc.use(c) |
| 68 » } else { |
| 69 » » c = mc.SetRaw(c, dummy.Memcache()) |
| 70 » } |
| 71 |
| 72 » return c |
30 } | 73 } |
OLD | NEW |