-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
46 lines (37 loc) · 959 Bytes
/
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
package main
import (
"fmt"
"os"
"time"
"context"
"log"
"net/http"
"os/signal"
"syscall"
"github.com/authfun/gauthfun/config"
"github.com/authfun/gauthfun/router"
)
// Graceful shutdown
// https://github.com/gin-gonic/gin#graceful-shutdown-or-restart
// https://github.com/gin-gonic/examples/tree/master/graceful-shutdown
func main() {
srv := &http.Server{
Addr: fmt.Sprintf(":%d", config.AppConfig.Service.Port),
Handler: router.NewRouter(),
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server forced to shutdown: ", err)
}
log.Println("Server exiting")
}