-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (44 loc) · 1.35 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
package main
import (
"context"
"os"
"os/signal"
"time"
"github.com/serdarkalayci/carpool/api/adapters/data/mongodb"
"github.com/serdarkalayci/carpool/api/application"
"github.com/serdarkalayci/carpool/api/adapters/comm/rest"
"github.com/nicholasjackson/env"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/serdarkalayci/carpool/api/util"
)
var bindAddress = env.String("BASE_URL", false, ":5500", "Bind address for rest server")
func main() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
util.SetConstValues()
util.SetLogLevels()
var dbContext *application.DataContext
dbContext = mongodb.NewDataContext()
s := rest.NewAPIContext(bindAddress, dbContext)
// start the http server
go func() {
log.Debug().Msgf("Starting server on %s", *bindAddress)
err := s.ListenAndServe()
if err != nil {
log.Error().Err(err).Msg("error starting rest server")
os.Exit(1)
}
}()
// trap sigterm or interrupt and gracefully shutdown the server
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, os.Kill)
// Block until a signal is received.
sig := <-c
log.Info().Msgf("Got signal: %s", sig)
// gracefully shutdown the server, waiting max 30 seconds for current operations to complete
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
s.Shutdown(ctx)
}