-
Notifications
You must be signed in to change notification settings - Fork 0
/
Panizza.go
320 lines (274 loc) · 7.42 KB
/
Panizza.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package Panizza
import (
"bufio"
"container/list"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"reflect"
"runtime/debug"
"strconv"
"strings"
"time"
)
var panizzaInstance *Panizza
//全局配置实例
var AppConfig Configer
type Configer map[string]interface{}
//通过key获取配置文件中的配置
func (c *Configer) GetConfiger(key string) (interface{}, bool) {
val, ok := (*c)[key]
if !ok {
return nil, false
}
return val, true
}
type Panizza struct {
Configer
controller
fileServe
Filter
*Ioc
}
//实例化panizza,首先寻找项目中的配置文件application.conf并加载配置
func New() *Panizza {
fmt.Println(miniLogo)
propertiesPath := FindPropertiesFile("application.conf")
AppConfig = ReadConfigFromProperties(propertiesPath)
log.Println("AppConfig:", AppConfig)
//把配置文件的配置全部放入ioc容器
for key, val := range AppConfig {
IocInstance.AddBeen(key, val)
}
//初始化静态资源服务
fs := fileServe{}
fs.Static()
panizza := &Panizza{
AppConfig,
controllerInstence,
fs,
&FilterImpl{
[]Handle{},
},
IocInstance,
}
panizzaInstance = panizza
return panizza
}
//寻找application.conf配置文件
func FindPropertiesFile(confFileName string) string {
dir, _ := os.Getwd()
var files []string
FindFilesFromStartPath(dir, confFileName, &files)
if len(files) == 0 {
panic(errors.New("can not find" + confFileName + "file from project!"))
}
return files[0]
}
func ReadConfigFromProperties(propertiesPath string) map[string]interface{} {
file, err := os.Open(propertiesPath)
if err != nil {
panic(errors.New("Opne" + propertiesPath + "error!"))
}
defer file.Close()
buf := bufio.NewReader(file)
config := make(map[string]interface{})
lineIndex := 0 //解析到的行号
for {
line, err := buf.ReadString('\n')
line = strings.TrimSpace(line)
lineIndex++
//判断是否为map
if len(line) != 0 && strings.Contains(line, "=") && !strings.Contains(line, "#") && strings.Contains(line, "{") {
list := strings.Split(line, "=")
key := list[0]
val := strings.Split(list[1][1:len(list[1])-1], ",")
valsMap := make(map[string]string)
for _, v := range val {
result := strings.Split(v, ":")
if len(result) != 2 {
panic(errors.New("Parse map config err at line:" + string(lineIndex)))
}
valsMap[result[0]] = result[1]
}
config[key] = valsMap
//fmt.Println("valsMap:",valsMap)
} else if len(line) != 0 && strings.Contains(line, "=") && !strings.Contains(line, "#") && strings.Contains(line, "[") {
//按照数组解析
list := strings.Split(line, "=")
key := list[0]
val := strings.Split(list[1][1:len(list[1])-1], ",")
valsSlice := []string{}
for _, v := range val {
valsSlice = append(valsSlice, v)
}
config[key] = valsSlice
//fmt.Println("valsSlice:",valsSlice)
} else if len(line) != 0 && strings.Contains(line, "=") && !strings.Contains(line, "#") {
//按照key-val等式解析
list := strings.Split(line, "=")
config[list[0]] = list[1]
}
if err != nil {
if err == io.EOF {
break
} else {
panic(errors.New("read file err at line:" + string(lineIndex)))
}
}
}
return config
}
//开启panizza服务,如果没有配置PORT则默认监听8080端口
func (pz *Panizza) StartServer() {
port, ok := AppConfig.GetConfiger("PORT")
if !ok {
port = "8080"
pz.AddBeen("PORT", 8080)
}
if err := http.ListenAndServe(":"+(port).(string), pz); err != nil {
panic(err)
}
}
//全局注册过得url都会放在这里
var registUrl = []string{}
func (pz *Panizza) ServeHTTP(w http.ResponseWriter, r *http.Request) {
//初始化上下文实例,对应到每一个请求都有一个上下文
var ctx = NewHandleContext(&w, r)
defer UseTimePrint(ctx)()
if !IsOpenController && !pz.IsOpenFileService {
w.Header().Add("Content-Type", "text/html;charset=utf-8")
fmt.Fprint(w, PanizzaWelcomePage())
return
}
pz.Filter.RunFilter(ctx)
if ctx.CloseHttp {
log.Println("中间件提前断开了请求!")
return
}
//通过url取得get对应方法并执行
if r.Method == "GET" {
//静态资源访问支持
if pz.IsOpenFileService {
//fmt.Println("file server isOpen")
file := pz.StaticPath + r.URL.Path
file = strings.Replace(file, "\\", "/", -1)
indexHtml := file + "index.html"
if IsDir(file) && Existe(indexHtml) {
http.ServeFile(w, r, indexHtml)
log.Println("end file index.html!")
return
} else if Existe(file) && !IsDir(file) {
//执行静态资源过滤器
var flag bool = true
for _, filter := range pz.FilterFunList {
flag = filter(w, r, ctx.Data)
if !flag {
//log.Fatalln("文件过滤器过滤请求:", r.URL.Path)
return
}
}
log.Println("file request:", file)
start := time.Now().UnixNano()
http.ServeFile(w, r, file)
end := time.Now().UnixNano()
Milliseconds := float64((end - start) / 100000)
log.Print("Panizza [\tend file server:\t", file+"\t", Milliseconds, "ms\t]\n")
return
}
}
}
nodeList, has := pz.controller.methodTree[r.Method]
if !has {
handle404((*ctx.ResponseWriter), ctx.Request)
return
}
routerHandle(ctx, pz, *nodeList)
return
}
//未匹配404
func handle404(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
fmt.Fprint(w, "page is not found!")
}
//遍历list寻path找对应的node
func getNodeByPath(handlerList list.List, path string) *node {
for e := handlerList.Front(); e != nil; e = e.Next() {
n := e.Value.(*node)
if n.path == path {
return n
}
}
return nil
}
func routerHandle(ctx *HandleContext, pz *Panizza, handlerList list.List) {
n := getNodeByPath(handlerList, ctx.Request.URL.Path)
var aspect interface{}
if n != nil {
if n.HasAspect {
aspect, _ = Aspecters.Load(n.HandleName)
RecoverAspectHandle(n.HandlerFunc, aspect, n.HandleName)(ctx)
return
} else {
RecoverHandle(n.HandlerFunc)(ctx)
return
}
}
for e := handlerList.Front(); e != nil; e = e.Next() {
n1 := e.Value.(*node)
if ctx.Check(n1.path, ctx.Request.URL.Path) {
ctx.GetParams(n1.path, ctx.Request.URL.Path)
if n1.HasAspect {
aspect, _ = Aspecters.Load(n1.HandleName)
RecoverAspectHandle(n1.HandlerFunc, aspect, n1.HandleName)(ctx)
return
} else {
RecoverHandle(n1.HandlerFunc)(ctx)
return
}
}
}
handle404(*ctx.ResponseWriter, ctx.Request)
}
//异常处理和切面执行
func RecoverAspectHandle(handle Handle, aspect interface{}, HandleName string) Handle {
return func(ctx *HandleContext) {
defer func() {
if pr := recover(); pr != nil {
fmt.Printf("panic recover: %v\r\n", pr)
debug.PrintStack()
((aspect).(Aspect)).AfterPanic(pr, ctx, HandleName)
}
}()
((aspect).(Aspect)).Before(ctx, HandleName)
handle(ctx)
((aspect).(Aspect)).After(ctx, HandleName)
}
}
//异常处理
func RecoverHandle(handle Handle) Handle {
return func(ctx *HandleContext) {
defer func() {
if pr := recover(); pr != nil {
fmt.Printf("panic recover: %v\r\n", pr)
debug.PrintStack()
}
}()
handle(ctx)
}
}
//用于打印程序执行时间
func UseTimePrint(ctx *HandleContext) func() {
start := time.Now().UnixNano()
return func() {
end := time.Now().UnixNano()
Milliseconds := float64((end - start) / 100000)
status := reflect.ValueOf(*ctx.ResponseWriter).Elem().FieldByName("status")
code := status.Int()
string := strconv.FormatInt(code, 10)
log.Print("Panizza\t[\t", ctx.Request.Method, "\t", ctx.Request.URL.Path+"\t", string, "\t", Milliseconds, "ms\t", "]\n")
}
}