Debian Codegraph @

samples/guestbook/inject_azure.go

raw
1 // Copyright 2019 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 "go.opentelemetry.io/otel/metric"
23 "go.opentelemetry.io/otel/propagation"
24 "go.opentelemetry.io/otel/trace"
25
26 azcontainer "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
27 "github.com/google/wire"
28 "gocloud.dev/blob"
29 "gocloud.dev/blob/azureblob"
30 "gocloud.dev/runtimevar"
31 "gocloud.dev/runtimevar/blobvar"
32 "gocloud.dev/server"
33 "gocloud.dev/server/requestlog"
34 )
35
36 // This file wires the generic interfaces up to Microsoft Azure. It
37 // won't be directly included in the final binary, since it includes a Wire
38 // injector template function (setupAzure), but the declarations will be copied
39 // into wire_gen.go when Wire is run.
40
41 // setupAzure is a Wire injector function that sets up the application using
42 // Azure.
43 func setupAzure(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 wire.InterfaceValue(new(requestlog.Logger), requestlog.Logger(nil)),
48 azureblob.NewDefaultServiceURLOptions,
49 azureblob.NewDefaultClient,
50 azureblob.NewServiceURL,
51 applicationSet,
52 bucketName,
53 azureBucket,
54 azureMOTDVar,
55 server.Set,
56 wire.InterfaceValue(new(propagation.TextMapPropagator), propagation.TextMapPropagator(nil)),
57 wire.InterfaceValue(new(trace.TracerProvider), trace.TracerProvider(nil)),
58 wire.InterfaceValue(new(metric.MeterProvider), metric.MeterProvider(nil)),
59 dialLocalSQL,
60 )
61 return nil, nil, nil
62 }
63
64 func bucketName(flags *cliFlags) azureblob.ContainerName {
65 return azureblob.ContainerName(flags.bucket)
66 }
67
68 // azureBucket is a Wire provider function that returns the Azure bucket based
69 // on the command-line flags.
70 func azureBucket(ctx context.Context, client *azcontainer.Client, flags *cliFlags) (*blob.Bucket, func(), error) {
71 b, err := azureblob.OpenBucket(ctx, client, nil)
72 if err != nil {
73 return nil, nil, err
74 }
75 return b, func() { b.Close() }, nil
76 }
77
78 // azureMOTDVar is a Wire provider function that returns the Message of the Day
79 // variable read from a blob stored in Azure.
80 func azureMOTDVar(ctx context.Context, b *blob.Bucket, flags *cliFlags) (*runtimevar.Variable, error) {
81 return blobvar.OpenVariable(b, flags.motdVar, runtimevar.StringDecoder, &blobvar.Options{
82 WaitDuration: flags.motdVarWaitTime,
83 })
84 }