-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
57 lines (42 loc) · 985 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
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"context"
"net/http"
"os"
"os/signal"
"time"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
var logger *zap.Logger
func init() {
logger, _ = zap.NewProduction()
}
func main() {
router := gin.Default()
// add the ping-pong function
router.GET("/ping")
// add the echo function
router.POST("/echo")
srv := http.Server{
Addr: "0.0.0.0:5000",
Handler: router,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
}
logger.Info("the server is now running at port 5000")
go func() {
srv.ListenAndServe()
}()
logger.Info("if you want to stop it, please enter the Ctrl+C")
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
<-sig
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
logger.Error("shutdown http server failed", zap.Error(err))
os.Exit(1)
}
logger.Info("the http server is now stopped")
}