This repository has been archived by the owner on Mar 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
79 lines (63 loc) · 1.8 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
//"runtime"
_ "github.com/go-sql-driver/mysql"
"github.com/lunny/config"
"github.com/lunny/xorm"
"github.com/lunny/xweb"
. "github.com/govc/godaily/actions"
)
const APP_VER = "0.0.2 Beta"
func main() {
//runtime.GOMAXPROCS(2)
// load config
var err error
cfg, err := config.Load("config.ini")
if err != nil {
fmt.Println(err)
return
}
cfgs := cfg.Map()
// create Orm
var orm *xorm.Engine
orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%v:%v@%v/%v?charset=utf8",
cfgs["dbuser"], cfgs["dbpasswd"], cfgs["dbhost"], cfgs["dbname"]))
if err != nil {
fmt.Println(err)
return
}
orm.ShowSQL, _ = cfg.GetBool("showSql")
orm.ShowDebug, _ = cfg.GetBool("showDebug")
err = orm.Sync(&User{}, &Question{},
&QuestionFollow{}, &UserFollow{}, &Answer{}, &AnswerUp{},
&QuestionComment{}, &AnswerComment{}, &Tag{}, &QuestionTag{},
&Message{}, &Topic{}, &QuestionTopic{}, &TopicFollow{}, &News{})
if err != nil {
fmt.Println(err)
return
}
server := xweb.MainServer()
app := xweb.RootApp()
app.SetConfig("Orm", orm)
if useCache, _ := cfg.GetBool("useCache"); useCache {
server.Info("using orm cache system ...")
cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
orm.SetDefaultCacher(cacher)
}
// add actions
xweb.AddAction(&HomeAction{})
xweb.AutoAction(&ExerciseAction{}, &QuestionAction{}, &NewsAction{})
xweb.AddAction(&UserAction{})
// add login filter
loginFilter := xweb.NewLoginFilter(app, USER_ID_TAG, "/login")
loginFilter.AddAnonymousUrls("/", "/exercise", "/exercise/compile",
"/news", "/login", "/about", "/register")
app.AddFilter(loginFilter)
// add func or var app scope
app.AddTmplVar("AppVer", func() string {
return "v" + APP_VER
})
// run the web server
xweb.Run(fmt.Sprintf("%v:%v", cfgs["address"], cfgs["port"]))
}