forked from linuxkerneltravel/lmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
136 lines (107 loc) · 2.59 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
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/signal"
"sort"
"syscall"
"time"
"github.com/linuxkerneltravel/lmp/dao/mysql"
"github.com/linuxkerneltravel/lmp/logger"
"github.com/linuxkerneltravel/lmp/modules"
"github.com/linuxkerneltravel/lmp/routes"
"github.com/linuxkerneltravel/lmp/settings"
"github.com/facebookgo/pidfile"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
)
func checkValid(ctx *cli.Context) error {
return nil
}
func showlogo() {
logo, err := ioutil.ReadFile("./misc/lmp.logo")
if err != nil {
fmt.Print(err)
}
fmt.Print(string(logo))
}
func doBeforeJob(ctx *cli.Context) error {
if err := checkValid(ctx); err != nil {
return err
}
pidfile.SetPidfilePath(os.Args[0] + ".pid")
if err := pidfile.Write(); err != nil {
fmt.Println("Pidfile write failed, err:", err)
return err
}
if err := settings.Init(); err != nil {
fmt.Println("Init settings failed, err:", err)
return err
}
if err := logger.Init(settings.Conf.LogConfig, settings.Conf.AppConfig.Mode); err != nil {
fmt.Println("Init logger failed, err:", err)
return err
}
defer zap.L().Sync()
if err := mysql.Init(settings.Conf.MySQLConfig); err != nil {
fmt.Println("Init mysql failed, err:", err)
return err
}
/*
if err := influxdb.Init(settings.Conf.InfluxdbConfig); err != nil {
fmt.Println("Init influxdb failed, err:", err)
return err
}
*/
return nil
}
func runlmp(ctx *cli.Context) error {
if err := checkValid(ctx); err != nil {
return err
}
r := routes.SetupRouter(settings.Conf.AppConfig.Mode)
srv := &http.Server{
Addr: fmt.Sprintf(":%d", settings.Conf.AppConfig.Port),
Handler: r,
}
showlogo()
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Error("listen failed :", err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
logger.Info("shutdown server ...")
ctxx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctxx); err != nil {
logger.Error("Server shutdown", err)
}
logger.Info("Server exiting")
return nil
}
func main() {
app := cli.NewApp()
app.Name = settings.Name
app.Usage = settings.LmpUsage
app.Version = settings.Version
for _, v := range modules.GetModules() {
app.Commands = append(app.Commands, v)
}
sort.Sort(cli.CommandsByName(app.Commands))
app.Before = doBeforeJob
app.Action = runlmp
defer func() {
// close mysql connection
mysql.Close()
}()
if err := app.Run(os.Args); err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
}