-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (55 loc) · 1.71 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
package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"buf.build/gen/go/mocha/remcall/connectrpc/go/donut/v1/donutv1connect"
"github.com/rs/zerolog/log"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)
func main() {
cfg, err := Get()
if err != nil {
log.Fatal().Err(err).Msg("failed to get config")
}
db, err := NewDatabaseInstance(cfg)
if err != nil {
log.Fatal().Err(err).Msg("failed to get database instance")
}
mux := http.NewServeMux()
// Create instances
repo := NewDonutRepository(db)
donut := NewDonutCall(repo)
handler := NewHandler(donut)
mmPath, mmHandler := donutv1connect.NewMatchMakerServiceHandler(handler)
pPath, pHandler := donutv1connect.NewPeopleServiceHandler(handler)
mux.Handle(mmPath, mmHandler)
mux.Handle(pPath, pHandler)
server := &http.Server{
Addr: cfg.ApplicationConfig.Address(),
Handler: h2c.NewHandler(mux, &http2.Server{}),
}
log.Info().Msgf("server is listening on %s", cfg.ApplicationConfig.Address())
// Run the server in a goroutine so that it doesn't block
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal().Err(err).Msg("failed to serve")
}
}()
// Wait for interrupt signal to gracefully shutdown the server
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
<-quit
log.Info().Msg("server is shutting down")
// Create a context with a timeout
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(cfg.ApplicationConfig.GracefulShutdownTimeout))
defer cancel()
// Shutdown the server gracefully
if err := server.Shutdown(ctx); err != nil {
log.Fatal().Err(err).Msg("failed to shutdown server gracefully")
}
}