-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontroller.go
109 lines (85 loc) · 2.66 KB
/
controller.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 hfw
//手动匹配路由
import (
"net/http"
"strings"
"github.com/gorilla/websocket"
"github.com/hsyan2008/hfw/configs"
"github.com/hsyan2008/hfw/redis"
"github.com/hsyan2008/hfw/session"
)
//ControllerInterface ..
//init和finish必定会执行,而且不允许被修改
// Before和After之间是业务逻辑,所有Before也是必定会执行
//用户手动StopRun()后,中止业务逻辑,跳过After,继续Finish
type ControllerInterface interface {
Init(*HTTPContext)
Before(*HTTPContext)
After(*HTTPContext)
Finish(*HTTPContext)
NotFound(*HTTPContext)
ServerError(*HTTPContext)
}
//确认Controller实现了接口 ControllerInterface
var _ ControllerInterface = &Controller{}
//Controller ..
type Controller struct {
}
//Init 请不要实现Init方法
func (ctl *Controller) Init(httpCtx *HTTPContext) {
var err error
// logger.Debug("Controller init")
if strings.Contains(httpCtx.Request.URL.RawQuery, "format=json") {
httpCtx.IsJSON = true
} else if strings.Contains(httpCtx.Request.Header.Get("Accept"), "application/json") {
httpCtx.IsJSON = true
}
if strings.Contains(httpCtx.Request.Header.Get("Accept-Encoding"), "gzip") {
httpCtx.IsZip = true
}
// _ = httpCtx.Request.ParseMultipartForm(2 * 1024 * 1024)
//开启session,暂时只支持redis
if configs.Config.EnableSession || configs.Config.Session.IsEnable {
if redis.DefaultIns != nil {
store := session.NewSessRedisStore(redis.DefaultIns, configs.Config.Redis)
httpCtx.Session = session.NewSession(httpCtx.Request, store, configs.Config.Session)
} else {
httpCtx.Error("session enable faild: redis instance is nil")
}
}
httpCtx.ThrowCheck(500, err)
}
//Before ..
func (ctl *Controller) Before(httpCtx *HTTPContext) {
// logger.Debug("Controller Before")
}
//After ..
func (ctl *Controller) After(httpCtx *HTTPContext) {
// logger.Debug("Controller After")
if websocket.IsWebSocketUpgrade(httpCtx.Request) || httpCtx.IsCloseRender {
return
}
}
//Finish 请不要实现Finish方法
func (ctl *Controller) Finish(httpCtx *HTTPContext) {
if websocket.IsWebSocketUpgrade(httpCtx.Request) {
return
}
httpCtx.RenderResponse()
}
//NotFound ..
func (ctl *Controller) NotFound(httpCtx *HTTPContext) {
httpCtx.HTTPStatus = http.StatusNotFound
httpCtx.IsError = true
httpCtx.ErrNo = 404
httpCtx.ErrMsg = "NotFound"
}
//ServerError ..
//不要手动调用,用于捕获未知错误,手动请用Throw
//该方法不能使用StopRun,也不能panic,因为会被自动调用
func (ctl *Controller) ServerError(httpCtx *HTTPContext) {
httpCtx.HTTPStatus = http.StatusInternalServerError
httpCtx.IsError = true
httpCtx.ErrNo = 500
httpCtx.ErrMsg = "ServerError"
}