-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (100 loc) · 2.33 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
/***********************************************
File Name: routers
Author: Abby Cin
Mail: [email protected]
Created Time: 9/14/19 3:50 PM
***********************************************/
package main
import (
"blog/conf"
"blog/dbutil"
"blog/logging"
"blog/middleware"
"blog/routers"
"blog/service"
"blog/session"
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
)
var (
help bool
cfgPath string
ver bool
GitVersion string
Built string
Prefix string
)
func init() {
flag.BoolVar(&help, "h", false, "show help")
flag.StringVar(&cfgPath, "conf", "", "config file path")
flag.BoolVar(&ver, "v", false, "show version")
}
func main() {
flag.Parse()
if ver {
fmt.Println("built: ", Built)
fmt.Println("git: ", GitVersion)
os.Exit(0)
}
if help || cfgPath == "" {
flag.Usage()
os.Exit(1)
}
cfg := conf.Init(cfgPath)
logging.Init(cfg.GetLogger(), len(Prefix))
defer logging.Release()
mgr := session.NewSessionManager(cfg)
mgr.StartGC()
access := middleware.NewAccess()
r := routers.New(mgr, cfg.Common.ProxyMode)
r.ServeFile(cfg.Service.Assets, cfg.Common.Assets, cfg.Mimes)
r.AddCb(access)
dao := dbutil.NewDao(cfg.Common.DbFile)
auth := middleware.NewAuth(mgr, cfg)
service.NewImage(cfg, r, auth)
service.NewLogin(cfg, dao, r)
service.NewManage(cfg, dao, r, auth)
service.NewUser(cfg, dao, r, auth)
post := service.NewArticle(cfg, dao, r)
home := service.NewHome(cfg, dao, r)
navi := service.NewNavi(cfg, dao, r, auth)
edit := service.NewEditor(cfg, dao, r, auth)
navi.Add(post)
navi.Add(home)
edit.Add(post)
edit.Add(home)
server := &http.Server{
Addr: cfg.Common.Addr,
Handler: r,
}
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
go func() {
for {
s := <-sig
logging.Info("get signal %s", s.String())
switch s {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP:
// must shutdown http server first
err := server.Shutdown(context.Background())
if err != nil {
logging.Error("http server shutdown: %s", err)
}
dao.Close()
logging.Info("server exited")
return
default:
return
}
}
}()
err := server.ListenAndServe()
if err != nil {
logging.Fatal("ListenAndServe: %v", err)
}
}