Debian Codegraph @

samples/guestbook/inject_local.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
24 "github.com/go-sql-driver/mysql"
25 "github.com/google/wire"
26 "gocloud.dev/blob"
27 "gocloud.dev/blob/fileblob"
28 "gocloud.dev/runtimevar"
29 "gocloud.dev/runtimevar/filevar"
30 "gocloud.dev/server"
31 "gocloud.dev/server/requestlog"
32 )
33
34 // This file wires the generic interfaces up to local implementations. It won't
35 // be directly included in the final binary, since it includes a Wire injector
36 // template function (setupLocal), but the declarations will be copied into
37 // wire_gen.go when Wire is run.
38
39 // setupLocal is a Wire injector function that sets up the application using
40 // local implementations.
41 func setupLocal(ctx context.Context, flags *cliFlags) (*server.Server, func(), error) {
42 // This will be filled in by Wire with providers from the provider sets in
43 // wire.Build.
44 wire.Build(
45 wire.InterfaceValue(new(requestlog.Logger), requestlog.Logger(nil)),
46 server.Set,
47 newPropagationTextMap,
48 newTraceSampler,
49 newTraceExporter,
50 otelTracesProviderSet,
51 otelMetricsProviderSet,
52 newMetricsReader,
53 applicationSet,
54 dialLocalSQL,
55 localBucket,
56 localRuntimeVar,
57 )
58 return nil, nil, nil
59 }
60
61 // localBucket is a Wire provider function that returns a directory-based bucket
62 // based on the command-line flags.
63 func localBucket(flags *cliFlags) (*blob.Bucket, error) {
64 return fileblob.OpenBucket(flags.bucket, nil)
65 }
66
67 // dialLocalSQL is a Wire provider function that connects to a MySQL database
68 // (usually on localhost).
69 func dialLocalSQL(flags *cliFlags) (*sql.DB, error) {
70 cfg := &mysql.Config{
71 Net: "tcp",
72 Addr: flags.dbHost,
73 DBName: flags.dbName,
74 User: flags.dbUser,
75 Passwd: flags.dbPassword,
76 AllowNativePasswords: true,
77 }
78 return sql.Open("mysql", cfg.FormatDSN())
79 }
80
81 // localRuntimeVar is a Wire provider function that returns the Message of the
82 // Day variable based on a local file.
83 func localRuntimeVar(flags *cliFlags) (*runtimevar.Variable, func(), error) {
84 v, err := filevar.OpenVariable(flags.motdVar, runtimevar.StringDecoder, &filevar.Options{
85 WaitDuration: flags.motdVarWaitTime,
86 })
87 if err != nil {
88 return nil, nil, err
89 }
90 return v, func() { v.Close() }, nil
91 }