-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
50 lines (42 loc) · 1.05 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
package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/sirupsen/logrus"
api "github.com/viardant/leetcode-solutions/webapi"
)
func main() {
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
router := api.Handler(log)
api.Route(router)
server := &http.Server{
Addr: ":8888",
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
shutdownSign := make(chan os.Signal, 1)
signal.Notify(shutdownSign, os.Interrupt, syscall.SIGTERM)
serverErrors := make(chan error, 1)
go func() {
log.Infof("API Server listetning on %s", server.Addr)
serverErrors <- server.ListenAndServe()
}()
select {
case err := <-serverErrors:
log.WithError(err).Fatal("an error has occurred")
case <-shutdownSign:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.WithError(err).Fatal("server forced to stop")
}
log.Info("server stopped gracefully")
}
}