-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
203 lines (167 loc) · 5.37 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
package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/ardanlabs/conf/v3"
"github.com/chagasVinicius/apollo/app/services/apollo-api/handlers"
"github.com/chagasVinicius/apollo/internal/sys/database"
"github.com/chagasVinicius/apollo/internal/web/debug"
"github.com/chagasVinicius/apollo/kit/logger"
"github.com/chagasVinicius/apollo/kit/opentel"
"go.uber.org/automaxprocs/maxprocs"
"go.uber.org/zap"
)
var build = "develop"
func main() {
log, err := logger.New("apollo-api")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := run(log); err != nil {
log.Errorw("startup", "ERROR", err)
log.Sync()
os.Exit(1)
}
log.Sync()
}
//nolint:funlen
func run(log *zap.SugaredLogger) error {
// =========================================================================
// GOMAXPROCS
opt := maxprocs.Logger(log.Infof)
if _, err := maxprocs.Set(opt); err != nil {
return fmt.Errorf("maxprocs: %w", err)
}
log.Infow("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:localhost:3000"`
DebugHost string `conf:"default:localhost:4000"`
}
DB struct {
User string `conf:"default:postgres"`
Password string `conf:"default:postgres,mask"`
Host string `conf:"default:localhost"`
Name string `conf:"default:postgres"`
DisableTLS bool `conf:"default:true"`
}
Trace struct {
ReporterURI string `conf:"default:http://localhost:14268/api/traces"`
ServiceName string `conf:"default:apollo-api"`
Probability float64 `conf:"default:0.05"`
}
}{
Version: conf.Version{
Build: build,
Desc: "copyright information here",
},
}
const prefix = "apollo-api"
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.Infow("starting service", "version", build)
defer log.Infow("shutdown complete")
out, err := conf.String(&cfg)
if err != nil {
return fmt.Errorf("generating config for output: %w", err)
}
log.Infow("startup", "config", out)
// =========================================================================
// Database Support
log.Infow("startup", "status", "initializing database support", "host", cfg.DB.Host)
db, err := database.Open(database.Config{
User: cfg.DB.User,
Password: cfg.DB.Password,
Host: cfg.DB.Host,
Name: cfg.DB.Name,
DisableTLS: cfg.DB.DisableTLS,
})
if err != nil {
return fmt.Errorf("connecting to db: %w", err)
}
defer func() {
log.Infow("shutdown", "status", "stopping database support", "host", cfg.DB.Host)
db.Close()
}()
// =========================================================================
// Start Tracing Support
log.Infow("startup", "status", "initializing OT/Zipkin tracing support")
traceProvider, err := opentel.StartTracing(cfg.Trace.ServiceName, cfg.Trace.ReporterURI, cfg.Trace.Probability)
if err != nil {
return fmt.Errorf("starting tracing: %w", err)
}
defer traceProvider.Shutdown(context.Background())
tracer := traceProvider.Tracer("service")
// =========================================================================
// Start Debug Service
log.Infow("startup", "status", "debug v1 router started", "host", cfg.Web.DebugHost)
go func() {
//nolint:gosec
if err := http.ListenAndServe(cfg.Web.DebugHost, debug.Mux(build, log, db)); err != nil {
log.Errorw("shutdown", "status", "debug v1 router closed", "host", cfg.Web.DebugHost, "ERROR", err)
}
}()
// =========================================================================
// Start API Service
log.Infow("startup", "status", "initializing V1 API support")
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
apiMux := handlers.APIMux(handlers.APIMuxConfig{
Shutdown: shutdown,
Log: log,
DB: db,
Tracer: tracer,
})
api := http.Server{
Addr: cfg.Web.APIHost,
Handler: apiMux,
ReadTimeout: cfg.Web.ReadTimeout,
WriteTimeout: cfg.Web.WriteTimeout,
IdleTimeout: cfg.Web.IdleTimeout,
ErrorLog: zap.NewStdLog(log.Desugar()),
}
serverErrors := make(chan error, 1)
go func() {
log.Infow("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.Infow("shutdown", "status", "shutdown started", "signal", sig)
defer log.Infow("shutdown", "status", "shutdown complete", "signal", sig)
ctx, cancel := context.WithTimeout(context.Background(), 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
}