-
Notifications
You must be signed in to change notification settings - Fork 114
/
initialize.go
119 lines (101 loc) · 3.14 KB
/
initialize.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 lessgo
import (
"encoding/json"
"mime"
"path/filepath"
"time"
"github.com/henrylee2cn/lessgo/session"
)
func newLessgo() *Lessgo {
registerMime()
l := &Lessgo{
App: app,
config: Config,
home: "/",
serverEnable: true,
apiHandlers: []*ApiHandler{},
apiMiddlewares: []*ApiMiddleware{},
virtBefore: []*MiddlewareConfig{},
virtAfter: []*MiddlewareConfig{},
virtStatics: []*VirtStatic{},
virtFiles: []*VirtFile{},
virtRouter: newRootVirtRouter(),
}
// 初始化全局日志
Log.SetMsgChan(Config.Log.AsyncChan)
Log.SetLevel(Config.Log.Level)
// 设置运行模式
l.App.SetDebug(Config.Debug)
// 设置静态资源缓存
l.App.setMemoryCache(NewMemoryCache(
Config.FileCache.SingleFileAllowMB*MB,
Config.FileCache.MaxCapMB*MB,
time.Duration(Config.FileCache.CacheSecond)*time.Second),
)
// 设置渲染接口
l.App.SetRenderer(NewPongo2Render(!Config.Debug))
// 设置上传文件允许的最大尺寸
MaxMemory = Config.MaxMemoryMB * MB
// 初始化sessions管理实例
sessions, err := newSessions()
if err != nil {
Log.Error("Failed to create sessions: %v.", err)
}
if sessions == nil {
Log.Sys("Session is disable.")
} else {
go sessions.GC()
l.App.setSessions(sessions)
Log.Sys("Session is enable.")
}
return l
}
func registerMime() {
for k, v := range mimemaps {
mime.AddExtensionType(k, v)
}
}
func newSessions() (sessions *session.Manager, err error) {
if !Config.Session.SessionOn {
return
}
conf := map[string]interface{}{
"cookieName": Config.Session.SessionName,
"gclifetime": Config.Session.SessionGCMaxLifetime,
"providerConfig": filepath.ToSlash(Config.Session.SessionProviderConfig),
"secure": Config.Listen.EnableTLS,
"enableSetCookie": Config.Session.SessionAutoSetCookie,
"domain": Config.Session.SessionDomain,
"cookieLifeTime": Config.Session.SessionCookieLifeTime,
"enableSidInHttpHeader": Config.Session.EnableSidInHttpHeader,
"sessionNameInHttpHeader": Config.Session.SessionNameInHttpHeader,
"enableSidInUrlQuery": Config.Session.EnableSidInUrlQuery,
}
confBytes, _ := json.Marshal(conf)
return session.NewManager(Config.Session.SessionProvider, string(confBytes))
}
// 添加系统预设的路由操作前的中间件
func registerBefore() {
PreUse(
&MiddlewareConfig{Name: "检查服务器是否启用"},
&MiddlewareConfig{Name: "检查是否为访问主页"},
&MiddlewareConfig{Name: "系统运行日志打印"},
)
if Config.CrossDomain {
BeforeUse(&MiddlewareConfig{Name: "设置允许跨域"})
}
}
// 添加系统预设的路由操作后的中间件
func registerAfter() {
}
// 添加系统预设的静态目录虚拟路由
func registerStatics() {
Static("/uploads", UPLOADS_DIR, AutoHTMLSuffix)
Static("/static", STATIC_DIR, FilterTemplate, AutoHTMLSuffix)
Static("/biz", BIZ_VIEW_DIR, FilterTemplate, AutoHTMLSuffix)
Static("/sys", SYS_VIEW_DIR, FilterTemplate, AutoHTMLSuffix)
}
// 添加系统预设的静态文件虚拟路由
func registerFiles() {
File("/favicon.ico", IMG_DIR+"/favicon.ico")
}