-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (123 loc) · 3.79 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
package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/ardanlabs/conf/v3"
"github.com/zmoog/shortener/app/services/shortener-api/handlers"
"github.com/zmoog/shortener/business/web/v1/debug"
"github.com/zmoog/shortener/foundation/logger"
"go.uber.org/zap"
)
/*
Need to figure ot timeouts for http service.
*/
var build = "develop"
func main() {
log, err := logger.New("shortener-api")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer log.Sync()
if err := run(log); err != nil {
log.Errorw("startup", "ERROR", err)
log.Sync()
os.Exit(1)
}
}
func run(log *zap.SugaredLogger) error {
// =========================================================================
// 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,mask"`
APIHost string `conf:"default:0.0.0.0:3000"`
DebugHost string `conf:"default:0.0.0.0:4000"`
}
}{
Version: conf.Version{
Build: build,
Desc: "Shortener API",
},
}
const prefix = "SHORTENER"
help, err := conf.Parse(prefix, &cfg)
if err != nil {
if errors.Is(err, conf.ErrHelpWanted) {
fmt.Println(help)
return nil
}
}
// =========================================================================
// App Starting
log.Infow("starting service", "version", build)
defer log.Infow("shutdown complete")
out, err := conf.String(&cfg)
if err != nil {
return fmt.Errorf("main : Parsing config : %w", err)
}
log.Infow("startup", "config", out)
// =========================================================================
// Start Debug Service
log.Infow("startup", "status", "debug v1 router started", "host", cfg.Web.DebugHost)
go func() {
// In this special case we don't have to track the goroutine because it's for debugging only.
if err := http.ListenAndServe(cfg.Web.DebugHost, debug.StandardLibraryMux()); err != nil {
log.Errorw("shutdown", "status", "debug v1 router closed", "host", cfg.Web.DebugHost, "ERROR", err)
}
}()
// =========================================================================
// =========================================================================
// GOMAXPROCS
log.Infow("startup", "GOMAXPROCS", runtime.GOMAXPROCS(0))
defer log.Infow("shutdown")
// =========================================================================
// Start API Service
log.Infow("startup", "status", "iniializing 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,
})
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()
log.Errorf("could not stop the server gracefully: %w", err)
}
}
return nil
}