forked from LockGit/gochat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.go
67 lines (59 loc) · 1.35 KB
/
chat.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
59
60
61
62
63
64
65
66
67
/**
* Created by lock
* Date: 2019-08-12
* Time: 11:17
*/
package api
import (
"context"
"flag"
"fmt"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"gochat/api/router"
"gochat/api/rpc"
"gochat/config"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
type Chat struct {
}
func New() *Chat {
return &Chat{}
}
//api server,Also, you can use gin,echo ... framework wrap
func (c *Chat) Run() {
//init rpc client
rpc.InitLogicRpcClient()
r := router.Register()
runMode := config.GetGinRunMode()
logrus.Info("server start , now run mode is ", runMode)
gin.SetMode(runMode)
apiConfig := config.Conf.Api
port := apiConfig.ApiBase.ListenPort
flag.Parse()
srv := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: r,
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logrus.Errorf("start listen : %s\n", err)
}
}()
// if have two quit signal , this signal will priority capture ,also can graceful shutdown
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
<-quit
logrus.Infof("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
logrus.Errorf("Server Shutdown:", err)
}
logrus.Infof("Server exiting")
os.Exit(0)
}