forked from beego/beeweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
119 lines (99 loc) · 2.39 KB
/
init.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
package routers
import (
"errors"
"path/filepath"
"strings"
"time"
"github.com/howeyc/fsnotify"
"github.com/astaxie/beego"
"github.com/beego/compress"
"github.com/beego/i18n"
)
var (
CompressConfPath = "conf/compress.json"
)
func initLocales() {
// Initialized language type list.
langs := strings.Split(beego.AppConfig.String("lang::types"), "|")
names := strings.Split(beego.AppConfig.String("lang::names"), "|")
langTypes = make([]*langType, 0, len(langs))
for i, v := range langs {
langTypes = append(langTypes, &langType{
Lang: v,
Name: names[i],
})
}
for _, lang := range langs {
beego.Trace("Loading language: " + lang)
if err := i18n.SetMessage(lang, "conf/"+"locale_"+lang+".ini"); err != nil {
beego.Error("Fail to set message file: " + err.Error())
return
}
}
}
func settingCompress() {
setting, err := compress.LoadJsonConf(CompressConfPath, IsPro, "/")
if err != nil {
beego.Error(err)
return
}
setting.RunCommand()
if IsPro {
setting.RunCompress(true, false, true)
}
beego.AddFuncMap("compress_js", setting.Js.CompressJs)
beego.AddFuncMap("compress_css", setting.Css.CompressCss)
}
func dict(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
return nil, errors.New("invalid dict call")
}
dict := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
}
func loadtimes(t time.Time) int {
return int(time.Now().Sub(t).Nanoseconds() / 1e6)
}
func initTemplates() {
beego.AddFuncMap("dict", dict)
beego.AddFuncMap("loadtimes", loadtimes)
}
func InitApp() {
initTemplates()
initLocales()
settingCompress()
watcher, err := fsnotify.NewWatcher()
if err != nil {
panic("Failed start app watcher: " + err.Error())
}
go func() {
for {
select {
case event := <-watcher.Event:
switch filepath.Ext(event.Name) {
case ".ini":
beego.Info(event)
if err := i18n.ReloadLangs(); err != nil {
beego.Error("Conf Reload: ", err)
}
beego.Info("Config Reloaded")
case ".json":
if event.Name == CompressConfPath {
settingCompress()
beego.Info("Beego Compress Reloaded")
}
}
}
}
}()
if err := watcher.WatchFlags("conf", fsnotify.FSN_MODIFY); err != nil {
beego.Error(err)
}
}