-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.go
88 lines (77 loc) · 1.62 KB
/
serve.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
package rider
import (
"html/template"
"net/http"
"runtime/debug"
"sync"
"github.com/hypwxm/rider/logger"
)
type HttpServer struct {
openRender bool
tplDir string
tplExtName string
funcMap template.FuncMap
tplsRender BaseRender
logger *logger.LogQueue
accessControl func(Context) *AccessControl
}
type ErrorHandler interface {
ErrorHandle(c *Context, err string, code int)
}
type pool struct {
request *sync.Pool
response *sync.Pool
context *sync.Pool
}
func newHttpServer() *HttpServer {
return &HttpServer{
tplsRender: newRender(),
openRender: false,
}
}
//全局的pool
var basePool *pool = &pool{
response: &sync.Pool{
New: func() interface{} {
return &Response{}
},
},
request: &sync.Pool{
New: func() interface{} {
return &Request{}
},
},
context: &sync.Pool{
New: func() interface{} {
return &context{}
},
},
}
func (h *HttpServer) NewHttpServer() *HttpServer {
return &HttpServer{}
}
var ErrorHandle func(c Context, err string, code int)
func HttpError(c Context, err string, code int) {
ErrorHandle(c, err, code)
}
//全局的错误处理,创建服务可以直接重写该方法
func init() {
ErrorHandle = func(c Context, err string, code int) {
errMsg := &Error{
StatusCode: code,
StatusText: http.StatusText(code),
Error: err,
}
if GlobalENV == ENV_Production {
errMsg.Stack = ""
}
if GlobalENV == ENV_Development {
errMsg.Stack = string(debug.Stack())
}
if GlobalENV == ENV_Debug {
c.Logger().DEBUG(err, "\r\n", string(debug.Stack()))
errMsg.Stack = string(debug.Stack())
}
c.SendJson(code, errMsg)
}
}