-
Notifications
You must be signed in to change notification settings - Fork 66
/
main.go
109 lines (88 loc) · 2.75 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
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
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
"github.com/tommy351/gin-sessions"
"strconv"
"os"
"./controller"
"./entity"
"./restgo"
)
func registerRouter(router *gin.Engine) {
new(controller.PageController).Router(router)
new(controller.UserController).Router(router)
new(controller.OpenController).Router(router)
new(controller.RoleController).Router(router)
new(controller.ResController).Router(router)
new(controller.ConfigController).Router(router)
}
func main() {
cfg := new(restgo.Config)
cfg.Parse("config/app.properties")
fmt.Println("[ok] load config ")
restgo.SetCfg(cfg)
restgo.Configuration(cfg.Logger["filepath"])
gin.SetMode(cfg.App["mode"])
for k, ds := range cfg.Datasource {
e, err := xorm.NewEngine(ds["driveName"], ds["dataSourceName"])
if err != nil {
fmt.Println("data source init error", err.Error())
return
}
fmt.Println("initt data source %s", ds["dataSourceName"])
e.ShowSQL(ds["showSql"] == "true")
n, _ := strconv.Atoi(ds["maxIdle"])
e.SetMaxIdleConns(n)
n, _ = strconv.Atoi(ds["maxOpen"])
e.SetMaxOpenConns(n)
//判断init文件是否存在
_, err = os.Stat("inited")
//如果不存在
if !(err == nil || !os.IsNotExist(err)){
fmt.Println("init table and passwd")
//创建表
err = e.Sync2(new(entity.User), new(entity.Config), new(entity.RefRoleRes), new(entity.Resource), new(entity.Role))
if err != nil {
fmt.Println("data source init error", err.Error())
return
}
//初始化sql语句
initsql := "INSERT INTO `user`(id,account,mobile,passwd,create_at,nick_name,ticket,role_id,email,stat) VALUES (1,'admin','18600000000','d060812a3a1af12643a74a4d3b6d492d','2018-02-23 11:32:32','winlion',NULL,1,'admin@qq.com',1)";
_,err := e.Query(initsql)
if err!=nil{
fmt.Println("data source init error", err.Error())
return
}
//创建一个文件
os.Create("inited")
}
restgo.SetEngin(k, e)
}
fmt.Println("[ok] init datasource")
router := gin.Default()
for k, v := range cfg.Static {
router.Static(k, v)
}
for k, v := range cfg.StaticFile {
router.StaticFile(k, v)
}
router.SetFuncMap(restgo.GetFuncMap())
router.NoRoute(restgo.NoRoute)
router.NoMethod(restgo.NoMethod)
router.LoadHTMLGlob(cfg.View["path"] + "/**/*")
router.Delims(cfg.View["deliml"], cfg.View["delimr"])
store := sessions.NewCookieStore([]byte(cfg.Session["name"]))
router.Use(sessions.Middleware(cfg.Session["name"], store))
router.Use(restgo.Auth())
registerRouter(router)
err := http.ListenAndServe(cfg.App["addr"]+":"+cfg.App["port"], router)
if err!=nil{
fmt.Println(err.Error())
}else{
fmt.Println("[ok] app run", cfg.App["addr"]+":"+cfg.App["port"])
}
}