-
Notifications
You must be signed in to change notification settings - Fork 59
/
main.go
65 lines (60 loc) · 1.66 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
package main
import (
"encoding/gob"
"encoding/json"
"fmt"
"github.com/beego/beego/v2/core/logs"
beego "github.com/beego/beego/v2/server/web"
"github.com/jicg/liteblog/models"
_ "github.com/jicg/liteblog/models"
_ "github.com/jicg/liteblog/routers"
"os"
"strings"
)
//go:generate bee generate routers -ctrlDir=controllers -routersFile=mygen.go -routersPkg="controllers"
func main() {
beego.BConfig.WebConfig.CommentRouterPath = "./controllers"
initLog()
initSession()
initTemplate()
beego.Run()
}
func initLog() {
if err := os.MkdirAll("data/logs", 0777); err != nil {
logs.Error(err)
return
}
logs.SetLogger("file", `{"filename":"data/logs/lyblog.log","level":7,"maxlines":1000,"maxsize":100,"daily":true,"maxdays":10}`)
logs.Async(1e3)
}
func initSession() {
gob.Register(models.User{})
//https://beego.vip/docs/mvc/controller/session.md
beego.SetStaticPath("assert", "assert")
beego.BConfig.WebConfig.Session.SessionOn = true
beego.BConfig.WebConfig.Session.SessionName = "liteblog-key"
beego.BConfig.WebConfig.Session.SessionProvider = "file"
beego.BConfig.WebConfig.Session.SessionProviderConfig = "data/session"
}
func initTemplate() {
beego.AddFuncMap("equrl", func(x, y string) bool {
s1 := strings.Trim(x, "/")
s2 := strings.Trim(y, "/")
return strings.Compare(s1, s2) == 0
})
beego.AddFuncMap("eq2", func(x, y interface{}) bool {
s1 := fmt.Sprintf("%v", x)
s2 := fmt.Sprintf("%v", y)
return strings.Compare(s1, s2) == 0
})
beego.AddFuncMap("add", func(x, y int) int {
return x + y
})
beego.AddFuncMap("json", func(obj interface{}) string {
bs, err := json.Marshal(obj)
if err != nil {
return "{id:0}"
}
return string(bs)
})
}