-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathmain.go
98 lines (85 loc) · 2.3 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
/*
#cgo CFLAGS: -I ./ -I./lib
#cgo CXXFLAGS: -I./
#cgo LDFLAGS: -L./lib -lWeWorkFinanceSdk_C -ldl
#include "./lib/WeWorkFinanceSdk_C.h"
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"github.com/gin-gonic/gin"
val "github.com/go-playground/validator/v10"
_ "github.com/pkg/errors"
"golang.org/x/net/context"
"msg/common/id_generator"
"msg/common/log"
"msg/common/storage"
"msg/common/validator"
"msg/conf"
"msg/constants"
"msg/controller"
"msg/models"
"msg/services"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
// 调用微信的动态链接库,出现无法链接的错误需要手动添加库路径
// export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:<path-to-so>
func validateConfig(c interface{}) {
if err := validator.NewCustomValidator().ValidateStruct(c); err != nil {
panic(err.(val.ValidationErrors))
}
}
func init() {
err := conf.SetupSetting()
if err != nil {
panic(err)
}
log.SetupLogger(conf.Settings.App.Env)
validateConfig(conf.Settings)
models.DB = models.InitDB(conf.Settings.DB)
id_generator.SetupIDGenerator()
storage.Setup(conf.Settings.Storage)
}
func main() {
arch := services.NewMsgArch()
arch.Init()
err := arch.Sync(conf.Settings.WeWork.ExtCorpID)
if err != nil {
panic(err)
}
msgArch := controller.NewMsgArch()
r := gin.New()
apiV1 := r.Group("/api/v1")
apiV1.POST(constants.MsgArchSrvPathSync, msgArch.Sync)
apiV1.GET(constants.MsgArchSrvPathSessions, msgArch.QuerySessions)
apiV1.GET(constants.MsgArchSrvPathMsgs, msgArch.QueryChatMsgs)
apiV1.GET(constants.MsgArchSrvSearchMsgs, msgArch.SearchMsgs)
s := &http.Server{
Addr: fmt.Sprintf(":%d", conf.Settings.Server.MsgArchHttpPort),
Handler: r,
ReadTimeout: conf.Settings.Server.ReadTimeout,
WriteTimeout: conf.Settings.Server.WriteTimeout,
MaxHeaderBytes: 1 << 20,
}
go func() {
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Sugar.Fatalf("s.ListenAndServe err: %v", err)
}
}()
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
fmt.Println("Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.Shutdown(ctx); err != nil {
log.Sugar.Fatalf("server forced to shutdown: %v", err)
}
log.Sugar.Info("Server exited")
}