Debian Codegraph @

samples/guestbook/inject_gcp.go

raw
1 // Copyright 2018 The Go Cloud Development Kit Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //go:build wireinject
16 // +build wireinject
17
18 package main
19
20 import (
21 "context"
22 "database/sql"
23 "fmt"
24 "net/url"
25
26 "github.com/google/wire"
27 "gocloud.dev/blob"
28 "gocloud.dev/blob/gcsblob"
29 "gocloud.dev/gcp"
30 "gocloud.dev/gcp/gcpcloud"
31 "gocloud.dev/mysql/gcpmysql"
32 "gocloud.dev/runtimevar"
33 "gocloud.dev/runtimevar/gcpruntimeconfig"
34 "gocloud.dev/server"
35 pb "google.golang.org/genproto/googleapis/cloud/runtimeconfig/v1beta1"
36 )
37
38 // This file wires the generic interfaces up to Google Cloud Platform (GCP). It
39 // won't be directly included in the final binary, since it includes a Wire
40 // injector template function (setupGCP), but the declarations will be copied
41 // into wire_gen.go when Wire is run.
42
43 // setupGCP is a Wire injector function that sets up the application using GCP.
44 func setupGCP(ctx context.Context, flags *cliFlags) (*server.Server, func(), error) {
45 // This will be filled in by Wire with providers from the provider sets in
46 // wire.Build.
47 wire.Build(
48 gcpcloud.GCP,
49 wire.Struct(new(gcpmysql.URLOpener), "CertSource"),
50 applicationSet,
51 gcpBucket,
52 gcpMOTDVar,
53 openGCPDatabase,
54 )
55 return nil, nil, nil
56 }
57
58 // gcpBucket is a Wire provider function that returns the GCS bucket based on
59 // the command-line flags.
60 func gcpBucket(ctx context.Context, flags *cliFlags, client *gcp.HTTPClient) (*blob.Bucket, func(), error) {
61 b, err := gcsblob.OpenBucket(ctx, client, flags.bucket, nil)
62 if err != nil {
63 return nil, nil, err
64 }
65 return b, func() { b.Close() }, nil
66 }
67
68 // openGCPDatabase is a Wire provider function that connects to a GCP Cloud SQL
69 // MySQL database based on the command-line flags.
70 func openGCPDatabase(ctx context.Context, opener *gcpmysql.URLOpener, id gcp.ProjectID, flags *cliFlags) (*sql.DB, func(), error) {
71 db, err := opener.OpenMySQLURL(ctx, &url.URL{
72 Scheme: "gcpmysql",
73 User: url.UserPassword(flags.dbUser, flags.dbPassword),
74 Host: string(id),
75 Path: fmt.Sprintf("/%s/%s/%s", flags.cloudSQLRegion, flags.dbHost, flags.dbName),
76 })
77 if err != nil {
78 return nil, nil, err
79 }
80 return db, func() { db.Close() }, nil
81 }
82
83 // gcpMOTDVar is a Wire provider function that returns the Message of the Day
84 // variable from Runtime Configurator.
85 func gcpMOTDVar(ctx context.Context, client pb.RuntimeConfigManagerClient, project gcp.ProjectID, flags *cliFlags) (*runtimevar.Variable, func(), error) {
86 variableKey := gcpruntimeconfig.VariableKey(project, flags.runtimeConfigName, flags.motdVar)
87 v, err := gcpruntimeconfig.OpenVariable(client, variableKey, runtimevar.StringDecoder, &gcpruntimeconfig.Options{
88 WaitDuration: flags.motdVarWaitTime,
89 })
90 if err != nil {
91 return nil, nil, err
92 }
93 return v, func() { v.Close() }, nil
94 }