-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
123 lines (103 loc) · 2.89 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"context"
"flag"
"fmt"
"github.com/filedrive-team/filplus-info/common"
"github.com/filedrive-team/filplus-info/jobs"
"github.com/filedrive-team/filplus-info/log"
"github.com/filedrive-team/filplus-info/models"
"github.com/filedrive-team/filplus-info/routers"
"github.com/filedrive-team/filplus-info/settings"
"github.com/gin-gonic/gin"
logger "github.com/sirupsen/logrus"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
)
var (
showV bool
configFile string
loglevel string
initTable bool
)
func printVersion() {
fmt.Printf("version: v%s.%s.%s-%s\ngithub.com/gin-gonic/gin version: %s\n",
Major, Minjor,
Patch, BuildVersion, gin.Version)
}
func Init(configFile string) {
settings.Setup(configFile)
conf := settings.AppConfig
models.Setup(conf)
// Initialize the global cache
common.InitGlobalCache()
}
func main() {
flag.BoolVar(&showV, "version", false, "print version")
flag.StringVar(&configFile, "config", "conf/app.toml", "set config file path")
flag.StringVar(&loglevel, "loglevel", "info", "set log level")
flag.BoolVar(&initTable, "init", false, "init table data")
flag.Parse()
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(fmt.Errorf("fetch work dir failed %+v", err))
}
if showV {
printVersion()
os.Exit(0)
}
err = log.InitLogger(dir, loglevel)
if err != nil {
panic(fmt.Errorf("init logger failed %+v", err))
}
Init(configFile)
defer models.CloseDB()
// Database table initialization operation
if initTable {
models.TruncateNotary()
models.TruncateClient()
models.InitNotaryList()
}
router := routers.InitRouter()
conf := settings.AppConfig
addr := fmt.Sprintf(":%d", conf.Server.HttpPort)
maxHeaderBytes := 1 << 20
srv := &http.Server{
Addr: addr,
Handler: router,
ReadTimeout: time.Duration(conf.Server.ReadTimeout * 1e9),
WriteTimeout: time.Duration(conf.Server.WriteTimeout * 1e9),
MaxHeaderBytes: maxHeaderBytes,
}
go func() {
// service connections
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Fatalf("listen: %s\n", err)
}
}()
mainCtx, mainCancel := context.WithCancel(context.Background())
syncer := jobs.NewSyncer()
go syncer.Run(mainCtx)
crawler := jobs.NewCrawler()
go crawler.Run(mainCtx)
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal, 1)
// kill (no param) default send syscall.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall.SIGKILL but can't be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
logger.Info("Shutdown Server ...")
mainCancel()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
logger.Fatal("Server Shutdown: ", err)
}
logger.Info("Server exiting")
}