/
lessgo.go
executable file
·148 lines (112 loc) · 3.53 KB
/
lessgo.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Title:逻辑流程
//
// Description:
//
// Author:black
//
// Createtime:2013-08-06 14:15
//
// Version:1.0
//
// 修改历史:版本号 修改日期 修改人 修改说明
//
// 1.0 2013-08-06 14:15 black 创建文档
package lessgo
import (
"encoding/xml"
"fmt"
"github.com/Unknwon/goconfig"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/moovweb/log4go"
"io/ioutil"
"net/http"
)
var (
tmplog log4go.Logger
Log *MyLogger //提供公用的日志方式
Config *goconfig.ConfigFile
entityList entitys
urlList urls
Store *sessions.CookieStore
SiteName string
SiteIcon string
)
func init() {
Config, _ = goconfig.LoadConfigFile("../etc/config.ini")
logFilePath, _ := Config.GetValue("lessgo", "logFilePath")
tmplog = make(log4go.Logger)
tmplog.AddFilter("stdout", log4go.DEBUG, log4go.NewConsoleLogWriter())
//单位是字节
fw := log4go.NewFileLogWriter(logFilePath, false).SetRotateSize(10 * 1024 * 1024).SetRotate(true)
tmplog.AddFilter("log", log4go.INFO, fw)
Log = new(MyLogger)
cookieSecret, _ := Config.GetValue("lessgo", "cookieSecret")
Store = sessions.NewCookieStore([]byte(cookieSecret))
SiteName, _ = Config.GetValue("lessgo", "siteName")
SiteIcon, _ = Config.GetValue("lessgo", "siteIcon")
}
//解析配置文件内容至内存中
func analyse() error {
content, err := ioutil.ReadFile("../etc/entity.xml")
err = xml.Unmarshal(content, &entityList)
if err != nil {
Log.Error(err)
return err
}
content, err = ioutil.ReadFile("../etc/url.xml")
err = xml.Unmarshal(content, &urlList)
if err != nil {
Log.Error(err)
return err
}
return nil
}
//启动应用
func ConfigLessgo() *mux.Router {
err := analyse()
if err != nil {
panic(err)
}
err = checkLib()
if err != nil {
panic(err)
}
http.Handle("/lessgo/", http.FileServer(http.Dir("../")))
http.Handle("/tmp/", http.FileServer(http.Dir("../")))
http.Handle("/imageupload/", http.FileServer(http.Dir("../")))
r := mux.NewRouter()
r.HandleFunc("/", homeAction)
r.HandleFunc("/loginOut", loginOutAction)
//这里的把每个实体的url规约好,暂时不去改变,将来再考虑配置 FIXME
for _, terminal := range urlList.Terminals {
r.HandleFunc("/"+terminal, commonAction)
r.HandleFunc("/"+terminal+"/index.html", commonAction)
r.HandleFunc("/"+terminal+"/login.html", loginAction)
for _, entity := range entityList.Entitys {
r.HandleFunc("/"+terminal+"/"+entity.Id+"/index.html", commonAction)
r.HandleFunc("/"+terminal+"/"+entity.Id, commonAction)
r.HandleFunc("/"+terminal+"/"+entity.Id+"/{id:[0-9]+}", commonAction)
r.HandleFunc("/"+terminal+"/"+entity.Id+"/add", commonAction)
r.HandleFunc("/"+terminal+"/"+entity.Id+"/modify", commonAction)
r.HandleFunc("/"+terminal+"/"+entity.Id+"/save", commonAction)
r.HandleFunc("/"+terminal+"/"+entity.Id+"/page", commonAction)
r.HandleFunc("/"+terminal+"/"+entity.Id+"/alldata", commonAction)
r.HandleFunc("/"+terminal+"/"+entity.Id+"/load"+"/{id:[0-9]+}", commonAction)
r.HandleFunc("/"+terminal+"/"+entity.Id+"/delete", commonAction)
}
}
for _, url := range urlList.Urls {
r.HandleFunc(url.Path, independentAction)
}
r.HandleFunc("/queryMenus", QueryMenusAction)
r.HandleFunc("/region/regions", regions)
r.HandleFunc("/timedim/years", years)
r.HandleFunc("/timedim/months", months)
r.HandleFunc("/timedim/weeks", weeks)
r.HandleFunc("/imgageuplaod", imageUpload)
r.HandleFunc("/kindeditorImageUpload", kindeditorImageUpload)
// http.Handle("/", r)
fmt.Println("lessgo配置完成")
return r
}