-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (48 loc) · 1.07 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
package main
import (
"context"
_ "embed"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"golang.org/x/exp/slog"
)
func main() {
cfg, err := initConfig()
if err != nil {
slog.Error("failed to init config", err)
os.Exit(1)
}
handlers, err := initHandlers(cfg)
if err != nil {
slog.Error("failed to init handlers", err)
os.Exit(1)
}
router := newRouter()
router.post("/api/v1/upload", handlers.Upload)
router.get("/api/v1/health", handlers.Health)
srv := &http.Server{
Addr: fmt.Sprintf("0.0.0.0:%s", cfg.HTTP.Port),
Handler: router.r,
}
// Run our server in a goroutine so that it doesn't block.
go func() {
if err := srv.ListenAndServe(); err != nil {
slog.Error("failed to start HTTP server", err)
}
}()
slog.Info("server running", "port", cfg.HTTP.Port)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
slog.Error("server shutdown", err)
os.Exit(1)
}
slog.Info("shutting down")
os.Exit(0)
}