-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
51 lines (47 loc) · 1.24 KB
/
server.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
package main
import (
"context"
"github.com/gin-gonic/gin"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
)
func serve(r *gin.Engine) {
Addr := os.Getenv("GIN_Addr") + ":" + os.Getenv("GIN_PORT")
ShutdownTimeoutStr := os.Getenv("GIN_Shutdown_Timeout")
ShutdownTimeout, _ := strconv.Atoi(ShutdownTimeoutStr)
srv := &http.Server{
Addr: Addr,
Handler: r,
ReadTimeout: 3600 * time.Second,
WriteTimeout: 3600 * time.Second,
MaxHeaderBytes: 1 << 20,
}
go func() {
// service connections
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(ShutdownTimeout)*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
// catching ctx.Done(). timeout of 5 seconds.
select {
case <-ctx.Done():
log.Println("timeout of " + ShutdownTimeoutStr + " seconds.")
}
log.Println("Server exiting")
}