Debian Codegraph @

samples/guestbook/inject_aws.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 "net/url"
24
25 "github.com/aws/aws-sdk-go-v2/service/s3"
26 "github.com/aws/aws-sdk-go-v2/service/ssm"
27 "github.com/google/wire"
28 "gocloud.dev/aws/awscloud"
29 "gocloud.dev/blob"
30 "gocloud.dev/blob/s3blob"
31 "gocloud.dev/mysql/awsmysql"
32 "gocloud.dev/runtimevar"
33 "gocloud.dev/runtimevar/awsparamstore"
34 "gocloud.dev/server"
35 )
36
37 // This file wires the generic interfaces up to Amazon Web Services (AWS). It
38 // won't be directly included in the final binary, since it includes a Wire
39 // injector template function (setupAWS), but the declarations will be copied
40 // into wire_gen.go when Wire is run.
41
42 // setupAWS is a Wire injector function that sets up the application using AWS.
43 func setupAWS(ctx context.Context, flags *cliFlags) (*server.Server, func(), error) {
44 // This will be filled in by Wire with providers from the provider sets in
45 // wire.Build.
46 wire.Build(
47 awscloud.AWS,
48 wire.Struct(new(awsmysql.URLOpener), "CertSource"),
49 applicationSet,
50 awsBucket,
51 awsMOTDVar,
52 openAWSDatabase,
53 )
54 return nil, nil, nil
55 }
56
57 // awsBucket is a Wire provider function that returns the S3 bucket based on the
58 // command-line flags.
59 func awsBucket(ctx context.Context, client *s3.Client, flags *cliFlags) (*blob.Bucket, func(), error) {
60 b, err := s3blob.OpenBucketV2(ctx, client, flags.bucket, nil)
61 if err != nil {
62 return nil, nil, err
63 }
64 return b, func() { b.Close() }, nil
65 }
66
67 // openAWSDatabase is a Wire provider function that connects to an AWS RDS
68 // MySQL database based on the command-line flags.
69 func openAWSDatabase(ctx context.Context, opener *awsmysql.URLOpener, flags *cliFlags) (*sql.DB, func(), error) {
70 db, err := opener.OpenMySQLURL(ctx, &url.URL{
71 Scheme: "awsmysql",
72 User: url.UserPassword(flags.dbUser, flags.dbPassword),
73 Host: flags.dbHost,
74 Path: "/" + flags.dbName,
75 })
76 if err != nil {
77 return nil, nil, err
78 }
79 return db, func() { db.Close() }, nil
80 }
81
82 // awsMOTDVar is a Wire provider function that returns the Message of the Day
83 // variable from SSM Parameter Store.
84 func awsMOTDVar(ctx context.Context, client *ssm.Client, flags *cliFlags) (*runtimevar.Variable, error) {
85 return awsparamstore.OpenVariableV2(client, flags.motdVar, runtimevar.StringDecoder, &awsparamstore.Options{
86 WaitDuration: flags.motdVarWaitTime,
87 })
88 }