forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
177 lines (147 loc) · 4.3 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2015 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package bookshelf
import (
"errors"
"log"
"os"
"gopkg.in/mgo.v2"
"github.com/gorilla/sessions"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/cloud/datastore"
"google.golang.org/cloud/pubsub"
"google.golang.org/cloud/storage"
)
var (
DB BookDatabase
OAuthConfig *oauth2.Config
StorageBucket *storage.BucketHandle
StorageBucketName string
SessionStore sessions.Store
PubsubClient *pubsub.Client
// Force import of mgo library.
_ mgo.Session
)
const PubsubTopicID = "fill-book-details"
func init() {
var err error
// To use the in-memory test database, uncomment the next line.
DB = newMemoryDB()
// [START cloudsql]
// To use MySQL, uncomment the following lines, and update the username,
// password and host.
//
// DB, err = newMySQLDB(MySQLConfig{
// Username: "",
// Password: "",
// Host: "",
// Port: 3306,
// })
// [END cloudsql]
// [START mongo]
// To use Mongo, uncomment the next lines and update the address string and
// optionally, the credentials.
//
// var cred *mgo.Credential
// DB, err = newMongoDB("localhost", cred)
// [END mongo]
// [START datastore]
// To use Cloud Datastore, uncomment the following lines and update the
// project ID.
// More options can be set, see the google package docs for details:
// http://godoc.org/golang.org/x/oauth2/google
//
// DB, err = configureDatastoreDB("<your-project-id>")
// [END datastore]
if err != nil {
log.Fatal(err)
}
// [START storage]
// To configure Cloud Storage, uncomment the following lines and update the
// bucket name.
//
// StorageBucketName = "<your-storage-bucket>"
// StorageBucket, err = configureStorage(StorageBucketName)
// [END storage]
if err != nil {
log.Fatal(err)
}
// [START auth]
// To enable user sign-in, uncomment the following lines and update the
// Client ID and Client Secret.
// You will also need to update OAUTH2_CALLBACK in app.yaml when pushing to
// production.
//
// OAuthConfig = configureOAuthClient("clientid", "clientsecret")
// [END auth]
// [START sessions]
// Configure storage method for session-wide information.
// Update "something-very-secret" with a hard to guess string or byte sequence.
cookieStore := sessions.NewCookieStore([]byte("something-very-secret"))
cookieStore.Options = &sessions.Options{
HttpOnly: true,
}
SessionStore = cookieStore
// [END sessions]
// [START pubsub]
// To configure Pub/Sub, uncomment the following lines and update the project ID.
//
// PubsubClient, err = configurePubsub("<your-project-id>")
// [END pubsub]
if err != nil {
log.Fatal(err)
}
}
func configureDatastoreDB(projectID string) (BookDatabase, error) {
ctx := context.Background()
client, err := datastore.NewClient(ctx, projectID)
if err != nil {
return nil, err
}
return newDatastoreDB(client)
}
func configureStorage(bucketID string) (*storage.BucketHandle, error) {
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return nil, err
}
return client.Bucket(bucketID), nil
}
func configurePubsub(projectID string) (*pubsub.Client, error) {
if _, ok := DB.(*memoryDB); ok {
return nil, errors.New("Pub/Sub worker doesn't work with the in-memory DB " +
"(worker does not share its memory as the main app). Configure another " +
"database in bookshelf/config.go first (e.g. MySQL, Cloud Datastore, etc)")
}
ctx := context.Background()
client, err := pubsub.NewClient(ctx, projectID)
if err != nil {
return nil, err
}
// Create the topic if it doesn't exist.
if exists, err := client.Topic(PubsubTopicID).Exists(ctx); err != nil {
return nil, err
} else if !exists {
if _, err := client.NewTopic(ctx, PubsubTopicID); err != nil {
return nil, err
}
}
return client, nil
}
func configureOAuthClient(clientID, clientSecret string) *oauth2.Config {
redirectURL := os.Getenv("OAUTH2_CALLBACK")
if redirectURL == "" {
redirectURL = "http://localhost:8080/oauth2callback"
}
return &oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURL: redirectURL,
Scopes: []string{"email", "profile"},
Endpoint: google.Endpoint,
}
}