-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
288 lines (239 loc) · 8.74 KB
/
main.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"context"
"crypto/rsa"
"expvar"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/ardanlabs/conf"
"github.com/danielmbirochi/go-sample-service/app/services/sales-api/handlers"
"github.com/danielmbirochi/go-sample-service/business/auth"
"github.com/danielmbirochi/go-sample-service/foundation/database"
"github.com/danielmbirochi/go-sample-service/foundation/logger"
"github.com/golang-jwt/jwt/v4"
"github.com/pkg/errors"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/zipkin"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"go.uber.org/automaxprocs/maxprocs"
"go.uber.org/zap"
)
var build = "develop"
func main() {
// log := log.New(os.Stdout, "SALES: ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
log, err := logger.New("SALES-API")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer log.Sync()
if err := run(log); err != nil {
log.Errorw("main: error: ", err)
os.Exit(1)
}
}
func run(log *zap.SugaredLogger) error {
// =========================================================================
// GOMAXPROCS
// Set the correct number of threads for the service
// based on what is available either by the machine or cluster quotas.
if _, err := maxprocs.Set(); err != nil {
return fmt.Errorf("maxprocs: %w", err)
}
log.Infow("startup", "GOMAXPROCS", runtime.GOMAXPROCS(0))
// ============================================================================================
// Setup Configutarion
var cfg struct {
conf.Version
Web struct {
APIHost string `conf:"default:0.0.0.0:3000"` // noprint - this tag is used for hidding the config prop from stdout
DebugHost string `conf:"default:0.0.0.0:4000"`
ReadTimeout time.Duration `conf:"default:5s"`
WriteTimeout time.Duration `conf:"default:5s"`
ShutdownTimeout time.Duration `conf:"default:5s"`
}
Auth struct {
KeyID string `conf:"default:32bc1165-24t2-61a7-af3e-9da4agf2h1p1"`
PrivateKeyFile string `conf:"default:/app/private.pem"`
Algorithm string `conf:"default:RS256"`
}
DB struct {
User string `conf:"default:testuser"`
Password string `conf:"default:mysecretpassword,mask"`
Hostname string `conf:"default:0.0.0.0"`
Name string `conf:"default:testdb"`
DisableTLS bool `conf:"default:false"`
}
Zipkin struct {
ReporterURI string `conf:"default:http://localhost:9411/api/v2/spans"`
ServiceName string `conf:"default:sales-api"`
Probability float64 `conf:"default:0.05"`
}
}
cfg.Version.SVN = build
cfg.Version.Desc = "Go Sample service"
if err := conf.Parse(os.Args[1:], "SALES", &cfg); err != nil {
switch err {
case conf.ErrHelpWanted:
usage, err := conf.Usage("SALES", &cfg)
if err != nil {
return errors.Wrap(err, "generating config usage")
}
fmt.Println(usage)
return nil
case conf.ErrVersionWanted:
version, err := conf.VersionString("SALES", &cfg)
if err != nil {
return errors.Wrap(err, "generating config version")
}
fmt.Println(version)
return nil
}
return errors.Wrap(err, "parsing config")
}
// ============================================================================================
// App Starting
expvar.NewString("build").Set(build)
log.Infow("starting service", "version", build)
defer log.Infow("shutdown complete")
configOut, err := conf.String(&cfg)
if err != nil {
return errors.Wrap(err, "generating config for output")
}
log.Infow("startup", "config", configOut)
// ============================================================================================
// Initialize authentication support
log.Infow("startup", "status", "initializing authentication support")
privatePEM, err := ioutil.ReadFile(cfg.Auth.PrivateKeyFile)
if err != nil {
return errors.Wrap(err, "reading auth pem file")
}
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(privatePEM)
if err != nil {
return errors.Wrap(err, "parsing auth pem file")
}
// In production the Key pair set (Auth.PrivateKeyFile) would be get from external services.
// For local development env we`re using the local file system for that.
lookupKey := func(kid string) (*rsa.PublicKey, error) {
switch kid {
case cfg.Auth.KeyID:
return &privateKey.PublicKey, nil
}
return nil, errors.Errorf("no key pair found for the specified kid: %s", kid)
}
auth, err := auth.New(cfg.Auth.Algorithm, lookupKey, auth.Keys{cfg.Auth.KeyID: privateKey})
if err != nil {
return errors.Wrap(err, "initializing auth service")
}
// =========================================================================
// Start Database
log.Infow("startup", "status", "initializing database support", "host", cfg.DB.Hostname)
cfg.DB.DisableTLS = true
db, err := database.Open(database.Config{
User: cfg.DB.User,
Password: cfg.DB.Password,
Hostname: cfg.DB.Hostname,
Name: cfg.DB.Name,
DisableTLS: cfg.DB.DisableTLS,
})
if err != nil {
return errors.Wrap(err, "connecting to db")
}
defer func() {
log.Infow("shutdown", "status", "stopping database support", "host", cfg.DB.Hostname)
db.Close()
}()
// =========================================================================
// Start Tracing Support
// WARNING: The current Init settings are using defaults which may not be
// compatible with your project. Please review the documentation for
// opentelemetry.
log.Infow("startup", "status", "initializing OT/Zipkin tracing support")
exporter, err := zipkin.New(
cfg.Zipkin.ReporterURI,
zipkin.WithLogger(zap.NewStdLog(log.Desugar())),
)
if err != nil {
return errors.Wrap(err, "creating new zipkin exporter")
}
tp := trace.NewTracerProvider(
trace.WithSampler(trace.TraceIDRatioBased(cfg.Zipkin.Probability)),
trace.WithBatcher(exporter,
trace.WithMaxExportBatchSize(trace.DefaultMaxExportBatchSize),
trace.WithBatchTimeout(trace.DefaultBatchTimeout),
trace.WithMaxExportBatchSize(trace.DefaultMaxExportBatchSize),
),
trace.WithResource(
resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(cfg.Zipkin.ServiceName),
attribute.String("exporter", "zipkin"),
),
),
)
otel.SetTracerProvider(tp)
defer tp.Shutdown(context.Background())
// ============================================================================================
// Start Debug Service
log.Infow("startup", "status", "debug router started", "host", cfg.Web.DebugHost)
// The DebugStandardLibraryMux func returns a mux to listen and serve on for all
// the debug related endpoints. It includes the standard library endpoints, such as:
// /debug/pprof - Added to the default mux by importing the net/http/pprof package.
// /debug/vars - Added to the default mux by importing the expvar package.
//
// Construct the mux for debug calls.
debugMux := handlers.DebugStandardLibraryMux()
// Not concerned with shutting this down when the application is shutdown.
go func() {
if err := http.ListenAndServe(cfg.Web.DebugHost, debugMux); err != nil {
log.Errorw("shutdown", "status", "debug router closed", "host", cfg.Web.DebugHost, "ERROR", err)
}
}()
// ============================================================================================
// Start API Service
log.Infow("startup", "status", "initializing API support")
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)
api := http.Server{
Addr: cfg.Web.APIHost,
Handler: handlers.API(build, shutdown, log, auth, db),
ReadTimeout: cfg.Web.ReadTimeout,
WriteTimeout: cfg.Web.WriteTimeout,
ErrorLog: zap.NewStdLog(log.Desugar()),
}
// Make a channel for listening errors coming from the API Http listener. Use a
// buffered channel so the goroutine can exit if we don't collect this error.
serverErrors := make(chan error, 1)
go func() {
log.Infow("startup", "status", "api router started", "host", api.Addr)
serverErrors <- api.ListenAndServe()
}()
// =========================================================================
// Shutdown
// Blocking main and waiting for shutdown.
select {
case err := <-serverErrors:
return errors.Wrap(err, "server error")
case sig := <-shutdown:
log.Infow("shutdown", "status", "shutdown started", "signal", sig)
defer log.Infow("shutdown", "status", "shutdown complete", "signal", sig)
// Give outstanding requests a deadline for completion.
ctx, cancel := context.WithTimeout(context.Background(), cfg.Web.ShutdownTimeout)
defer cancel()
// Asking listener to shutdown and shed load.
if err := api.Shutdown(ctx); err != nil {
api.Close()
return errors.Wrap(err, "could not stop server gracefully!")
}
}
return nil
}