/
bootstrap.go
272 lines (235 loc) · 6.69 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package app
import (
"flag"
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
"github.com/fatih/color"
"github.com/go-ego/riot/types"
"github.com/phachon/mm-wiki/app/models"
"github.com/phachon/mm-wiki/app/utils"
"github.com/phachon/mm-wiki/app/work"
"github.com/phachon/mm-wiki/global"
"github.com/snail007/go-activerecord/mysql"
"log"
"os"
"path"
"path/filepath"
"time"
)
var (
defaultConf = "conf/mm-wiki.conf"
confPath = flag.String("conf", "", "please set mm-wiki conf path")
version = flag.Bool("version", false, "mm-wiki version")
upgrade = flag.Bool("upgrade", false, "mm-wiki upgrade")
Version = global.SYSTEM_VERSION
CopyRight = beego.Str2html(global.SYSTEM_COPYRIGHT)
StartTime = int64(0)
RootDir = ""
DocumentAbsDir = ""
MarkdownAbsDir = ""
ImageAbsDir = ""
AttachmentAbsDir = ""
SearchIndexAbsDir = ""
)
func init() {
initFlag()
poster()
initConfig()
initDB()
checkUpgrade()
initDocumentDir()
//initSearch()
//initWork()
StartTime = time.Now().Unix()
}
// init flag
func initFlag() {
flag.Parse()
// --version
if *version == true {
fmt.Printf(Version)
os.Exit(0)
}
}
// poster logo
func poster() {
fg := color.New(color.FgBlue)
logo := `
_ _ _
_ __ ___ _ __ ___ __ __ (_) | | __ (_)
| '_ ' _ \ | '_ ' _ \ _____ \ \ /\ / / | | | |/ / | |
| | | | | | | | | | | | |_____| \ V V / | | | < | |
|_| |_| |_| |_| |_| |_| \_/\_/ |_| |_|\_\ |_|
` +
"Author: phachon\r\n" +
"Version: " + Version + "\r\n" +
"Link: https://github.com/phachon/mm-wiki"
fg.Println(logo)
}
// init beego config
func initConfig() {
RootDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Println("init config error: " + err.Error())
os.Exit(1)
}
confFile := *confPath
if *confPath == "" {
confFile = filepath.Join(RootDir, defaultConf)
}
ok, _ := utils.NewFile().PathIsExists(confFile)
if ok == false {
log.Println("conf file " + confFile + " not exists!")
os.Exit(1)
}
// init config file
beego.LoadAppConfig("ini", confFile)
// init name
beego.AppConfig.Set("sys.name", "mm-wiki")
beego.BConfig.AppName = beego.AppConfig.String("sys.name")
beego.BConfig.ServerName = beego.AppConfig.String("sys.name")
// set static path
beego.SetStaticPath("/static/", filepath.Join(RootDir, "./static"))
// views path
beego.BConfig.WebConfig.ViewsPath = filepath.Join(RootDir, "./views/")
// session
//beego.BConfig.WebConfig.Session.SessionProvider = "memory"
//beego.BConfig.WebConfig.Session.SessionProviderConfig = ".session"
//beego.BConfig.WebConfig.Session.SessionName = "mmwikissid"
//beego.BConfig.WebConfig.Session.SessionOn = true
// log
logConfigs, err := beego.AppConfig.GetSection("log")
if err != nil {
log.Println(err.Error())
os.Exit(1)
}
for adapter, config := range logConfigs {
logs.SetLogger(adapter, config)
}
logs.SetLogFuncCall(true)
}
//init db
func initDB() {
host := beego.AppConfig.String("db::host")
port, _ := beego.AppConfig.Int("db::port")
user := beego.AppConfig.String("db::user")
pass := beego.AppConfig.String("db::pass")
dbname := beego.AppConfig.String("db::name")
dbTablePrefix := beego.AppConfig.String("db::table_prefix")
maxIdle, _ := beego.AppConfig.Int("db::conn_max_idle")
maxConn, _ := beego.AppConfig.Int("db::conn_max_connection")
models.G = mysql.NewDBGroup("default")
cfg := mysql.NewDBConfigWith(host, port, dbname, user, pass)
cfg.SetMaxIdleConns = maxIdle
cfg.SetMaxOpenConns = maxConn
cfg.TablePrefix = dbTablePrefix
cfg.TablePrefixSqlIdentifier = "__PREFIX__"
err := models.G.Regist("default", cfg)
if err != nil {
logs.Error(fmt.Errorf("database error:%s,with config : %v", err, cfg))
os.Exit(1)
}
}
// init document dir
func initDocumentDir() {
docRootDir := beego.AppConfig.String("document::root_dir")
if docRootDir == "" {
logs.Error("document root dir " + docRootDir + " is not empty!")
os.Exit(1)
}
ok, _ := utils.File.PathIsExists(docRootDir)
if !ok {
logs.Error("document root dir " + docRootDir + " is not exists!")
os.Exit(1)
}
documentAbsDir, err := filepath.Abs(docRootDir)
if err != nil {
logs.Error("document root dir " + docRootDir + " is error!")
os.Exit(1)
}
DocumentAbsDir = documentAbsDir
// markdown save dir
markDownAbsDir := path.Join(documentAbsDir, "markdowns")
// image save dir
imagesAbsDir := path.Join(documentAbsDir, "images")
// attachment save dir
attachmentAbsDir := path.Join(documentAbsDir, "attachment")
// search index dir
searchIndexAbsDir := path.Join(documentAbsDir, "search-index")
MarkdownAbsDir = markDownAbsDir
ImageAbsDir = imagesAbsDir
AttachmentAbsDir = attachmentAbsDir
SearchIndexAbsDir = searchIndexAbsDir
dirList := []string{MarkdownAbsDir, ImageAbsDir, AttachmentAbsDir, SearchIndexAbsDir}
// create dir
for _, dir := range dirList {
ok, _ = utils.File.PathIsExists(dir)
if !ok {
err := os.Mkdir(dir, 0777)
if err != nil {
logs.Error("create document dir "+dir+" error=%s", err.Error())
os.Exit(1)
}
}
}
// utils document
utils.Document.MarkdownAbsDir = markDownAbsDir
utils.Document.DocumentAbsDir = documentAbsDir
beego.SetStaticPath("/images/", ImageAbsDir)
// todo
beego.SetStaticPath("/images/:space_id/:document_id/", ImageAbsDir)
}
// check upgrade
func checkUpgrade() {
if *upgrade == true {
logs.Info("Start checking whether MM-Wiki needs upgrading.")
var versionDb = "v0.0.0"
versionConf := models.ConfigModel.GetConfigValueByKey(models.ConfigKeySystemVersion, "v0.0.0")
if versionConf != "" {
versionDb = versionConf
}
logs.Info("MM-Wiki Database version:" + versionDb)
logs.Info("MM-Wiki Now version: " + Version)
if versionDb == Version {
logs.Info("MM-Wiki does not need updating.")
} else {
logs.Info("MM-Wiki start upgrading.")
err := models.UpgradeModel.Start(versionDb)
if err != nil {
logs.Error("MM-Wiki upgrade failed.")
os.Exit(1)
}
logs.Info("MM-Wiki upgrade finish.")
}
os.Exit(0)
}
}
func initSearch() {
gseFile := filepath.Join(RootDir, "docs/search_dict/dictionary.txt")
stopFile := filepath.Join(RootDir, "docs/search_dict/stop_tokens.txt")
ok, _ := utils.File.PathIsExists(gseFile)
if !ok {
logs.Error("search dict file " + gseFile + " is not exists!")
os.Exit(1)
}
ok, _ = utils.File.PathIsExists(stopFile)
if !ok {
logs.Error("search stop dict file " + stopFile + " is not exists!")
os.Exit(1)
}
global.DocSearcher.Init(types.EngineOpts{
UseStore: true,
StoreFolder: SearchIndexAbsDir,
Using: 3,
//GseDict: "zh",
GseDict: gseFile,
StopTokenFile: stopFile,
IndexerOpts: &types.IndexerOpts{
IndexType: types.LocsIndex,
},
})
}
func initWork() {
work.DocSearchWorker.Start()
}