-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.go
149 lines (122 loc) · 3.22 KB
/
bootstrap.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
137
138
139
140
141
142
143
144
145
146
147
148
149
package fastgo
import (
"github.com/spf13/viper"
"os"
"github.com/phachon/go-logger"
"strings"
"github.com/valyala/fasthttp"
"path"
"github.com/phachon/fasthttpsession"
"flag"
"github.com/phachon/fasthttpsession/memory"
)
var (
Version = "v0.1"
Conf = viper.New()
Log = go_logger.NewLogger()
AppPath = ""
RootPath = ""
ViewPath = ""
StaticPath = ""
TemplateSuffix = ".html"
Session = fasthttpsession.NewSession(fasthttpsession.NewDefaultConfig())
Route = NewRouter()
)
var (
confType = flag.String("conf-type", "toml", "please input config type!")
conf = flag.String("conf", "config.toml", "please input config file!")
)
// start init
func init() {
initFlag()
initPath()
initConf()
initLog()
initSession()
}
// init flag
func initFlag() {
flag.Parse()
}
// init dir and path
func initPath() {
AppPath, _ = os.Getwd()
RootPath = strings.Replace(AppPath, "app", "", 1)
SetViewsPath("views")
SetStaticPath("static")
}
// init config
func initConf() {
Conf.SetConfigType(*confType)
Conf.SetConfigFile(*conf)
err := Conf.ReadInConfig()
if err != nil {
Log.Error("Fatal error config file: "+err.Error())
os.Exit(1)
}
file := Conf.ConfigFileUsed()
if file != "" {
Log.Info("Use config file: " + file)
}
}
// init log
func initLog() {
Log.Detach("console")
// console adapter config
consoleLevelStr := Conf.GetString("log.console.level")
consoleConfig := &go_logger.ConsoleConfig{
Color: Conf.GetBool("log.console.color"), // show text color
JsonFormat: Conf.GetBool("log.console.jsonFormat"), // text json format
Format: Conf.GetString("log.console.format"),
}
Log.Attach("console", Log.LoggerLevel(consoleLevelStr), consoleConfig)
// file adapter config
fileLevelStr := Conf.GetString("log.file.level")
levelFilenameConf := Conf.GetStringMapString("log.file.levelFilename")
levelFilename := map[int]string{}
if len(levelFilenameConf) > 0 {
for levelStr, levelFile := range levelFilenameConf {
level := Log.LoggerLevel(levelStr)
levelFilename[level] = levelFile
}
}
fileConfig := &go_logger.FileConfig{
Filename: Conf.GetString("log.file.filename"),
LevelFileName : levelFilename,
MaxSize: Conf.GetInt64("log.file.maxSize"),
MaxLine: Conf.GetInt64("log.file.maxLine"),
DateSlice: Conf.GetString("log.file.dateSlice"),
JsonFormat: Conf.GetBool("log.file.jsonFormat"),
Format: Conf.GetString("log.file.format"),
}
Log.Attach("file", Log.LoggerLevel(fileLevelStr), fileConfig)
}
func initSession() {
err := Session.SetProvider("memory", &memory.Config{})
if err != nil {
Log.Error("Init session error, "+err.Error())
os.Exit(1)
}
}
func SetViewsPath(view string) {
ViewPath = RootPath + "/" +view
}
func SetStaticPath(static string) {
StaticPath = RootPath + "/" +static
}
func SetTemplateSuffix(suffix string) {
TemplateSuffix = suffix
}
func ListenAndServe(addr string, route *Router) {
route.Add("GET", "/"+path.Base(StaticPath)+"/*path", NewController(), "Static")
Log.Infof("start listen server %s", addr)
err := fasthttp.ListenAndServe(addr, route.fastHttpRouter.Handler)
if err != nil {
Log.Infof("listen server %s error: %s", addr, err.Error())
}
}
func Run() {
// start listen server
server := Conf.GetString("listen.server")
ListenAndServe(server, Route)
}