-
Notifications
You must be signed in to change notification settings - Fork 619
/
main.go
271 lines (216 loc) · 7.63 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
package main
import (
"context"
"errors"
"expvar"
"fmt"
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/ardanlabs/conf/v3"
"github.com/ardanlabs/service/api/cmd/services/sales/build/all"
"github.com/ardanlabs/service/api/cmd/services/sales/build/crud"
"github.com/ardanlabs/service/api/cmd/services/sales/build/reporting"
"github.com/ardanlabs/service/api/sdk/http/debug"
"github.com/ardanlabs/service/api/sdk/http/mux"
"github.com/ardanlabs/service/app/sdk/authclient"
"github.com/ardanlabs/service/business/sdk/sqldb"
"github.com/ardanlabs/service/foundation/logger"
"github.com/ardanlabs/service/foundation/tracer"
"github.com/ardanlabs/service/foundation/web"
)
/*
Need to figure out timeouts for http service.
*/
var build = "develop"
var routes = "all" // go build -ldflags "-X main.routes=crud"
func main() {
var log *logger.Logger
events := logger.Events{
Error: func(ctx context.Context, r logger.Record) {
log.Info(ctx, "******* SEND ALERT *******")
},
}
traceIDFn := func(ctx context.Context) string {
return web.GetTraceID(ctx)
}
log = logger.NewWithEvents(os.Stdout, logger.LevelInfo, "SALES", traceIDFn, events)
// -------------------------------------------------------------------------
ctx := context.Background()
if err := run(ctx, log); err != nil {
log.Error(ctx, "startup", "msg", err)
os.Exit(1)
}
}
func run(ctx context.Context, log *logger.Logger) error {
// -------------------------------------------------------------------------
// GOMAXPROCS
log.Info(ctx, "startup", "GOMAXPROCS", runtime.GOMAXPROCS(0))
// -------------------------------------------------------------------------
// Configuration
cfg := struct {
conf.Version
Web struct {
ReadTimeout time.Duration `conf:"default:5s"`
WriteTimeout time.Duration `conf:"default:10s"`
IdleTimeout time.Duration `conf:"default:120s"`
ShutdownTimeout time.Duration `conf:"default:20s"`
APIHost string `conf:"default:0.0.0.0:3000"`
DebugHost string `conf:"default:0.0.0.0:3010"`
CORSAllowedOrigins []string `conf:"default:*"`
}
Auth struct {
Host string `conf:"default:http://auth-service.sales-system.svc.cluster.local:6000"`
}
DB struct {
User string `conf:"default:postgres"`
Password string `conf:"default:postgres,mask"`
Host string `conf:"default:database-service.sales-system.svc.cluster.local"`
Name string `conf:"default:postgres"`
MaxIdleConns int `conf:"default:0"`
MaxOpenConns int `conf:"default:0"`
DisableTLS bool `conf:"default:true"`
}
Tempo struct {
Host string `conf:"default:tempo.sales-system.svc.cluster.local:4317"`
ServiceName string `conf:"default:sales"`
Probability float64 `conf:"default:0.05"`
// Shouldn't use a high Probability value in non-developer systems.
// 0.05 should be enough for most systems. Some might want to have
// this even lower.
}
}{
Version: conf.Version{
Build: build,
Desc: "Sales",
},
}
const prefix = "SALES"
help, err := conf.Parse(prefix, &cfg)
if err != nil {
if errors.Is(err, conf.ErrHelpWanted) {
fmt.Println(help)
return nil
}
return fmt.Errorf("parsing config: %w", err)
}
// -------------------------------------------------------------------------
// App Starting
log.Info(ctx, "starting service", "version", cfg.Build)
defer log.Info(ctx, "shutdown complete")
out, err := conf.String(&cfg)
if err != nil {
return fmt.Errorf("generating config for output: %w", err)
}
log.Info(ctx, "startup", "config", out)
expvar.NewString("build").Set(cfg.Build)
// -------------------------------------------------------------------------
// Database Support
log.Info(ctx, "startup", "status", "initializing database support", "hostport", cfg.DB.Host)
db, err := sqldb.Open(sqldb.Config{
User: cfg.DB.User,
Password: cfg.DB.Password,
Host: cfg.DB.Host,
Name: cfg.DB.Name,
MaxIdleConns: cfg.DB.MaxIdleConns,
MaxOpenConns: cfg.DB.MaxOpenConns,
DisableTLS: cfg.DB.DisableTLS,
})
if err != nil {
return fmt.Errorf("connecting to db: %w", err)
}
defer db.Close()
// -------------------------------------------------------------------------
// Initialize authentication support
log.Info(ctx, "startup", "status", "initializing authentication support")
authClient := authclient.New(log, cfg.Auth.Host)
// -------------------------------------------------------------------------
// Start Tracing Support
log.Info(ctx, "startup", "status", "initializing tracing support")
traceProvider, err := tracer.InitTracing(tracer.Config{
Log: log,
ServiceName: cfg.Tempo.ServiceName,
Host: cfg.Tempo.Host,
ExcludedRoutes: map[string]struct{}{
"/v1/liveness": {},
"/v1/readiness": {},
},
Probability: cfg.Tempo.Probability,
})
if err != nil {
return fmt.Errorf("starting tracing: %w", err)
}
defer traceProvider.Shutdown(context.Background())
tracer := traceProvider.Tracer(cfg.Tempo.ServiceName)
// -------------------------------------------------------------------------
// Start Debug Service
go func() {
log.Info(ctx, "startup", "status", "debug v1 router started", "host", cfg.Web.DebugHost)
if err := http.ListenAndServe(cfg.Web.DebugHost, debug.Mux()); err != nil {
log.Error(ctx, "shutdown", "status", "debug v1 router closed", "host", cfg.Web.DebugHost, "msg", err)
}
}()
// -------------------------------------------------------------------------
// Start API Service
log.Info(ctx, "startup", "status", "initializing V1 API support")
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
cfgMux := mux.Config{
Build: build,
Log: log,
AuthClient: authClient,
DB: db,
Tracer: tracer,
}
api := http.Server{
Addr: cfg.Web.APIHost,
Handler: mux.WebAPI(cfgMux, buildRoutes(), mux.WithCORS(cfg.Web.CORSAllowedOrigins)),
ReadTimeout: cfg.Web.ReadTimeout,
WriteTimeout: cfg.Web.WriteTimeout,
IdleTimeout: cfg.Web.IdleTimeout,
ErrorLog: logger.NewStdLogger(log, logger.LevelError),
}
serverErrors := make(chan error, 1)
go func() {
log.Info(ctx, "startup", "status", "api router started", "host", api.Addr)
serverErrors <- api.ListenAndServe()
}()
// -------------------------------------------------------------------------
// Shutdown
select {
case err := <-serverErrors:
return fmt.Errorf("server error: %w", err)
case sig := <-shutdown:
log.Info(ctx, "shutdown", "status", "shutdown started", "signal", sig)
defer log.Info(ctx, "shutdown", "status", "shutdown complete", "signal", sig)
ctx, cancel := context.WithTimeout(ctx, cfg.Web.ShutdownTimeout)
defer cancel()
if err := api.Shutdown(ctx); err != nil {
api.Close()
return fmt.Errorf("could not stop server gracefully: %w", err)
}
}
return nil
}
func buildRoutes() mux.RouteAdder {
// The idea here is that we can build different versions of the binary
// with different sets of exposed web APIs. By default we build a single
// an instance with all the web APIs.
//
// Here is the scenario. It would be nice to build two binaries, one for the
// transactional APIs (CRUD) and one for the reporting APIs. This would allow
// the system to run two instances of the database. One instance tuned for the
// transactional database calls and the other tuned for the reporting calls.
// Tuning meaning indexing and memory requirements. The two databases can be
// kept in sync with replication.
switch routes {
case "crud":
return crud.Routes()
case "reporting":
return reporting.Routes()
}
return all.Routes()
}