-
Notifications
You must be signed in to change notification settings - Fork 0
/
aspect_handler.go
53 lines (48 loc) · 1.39 KB
/
aspect_handler.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
package wserver
import (
"net/http"
"strings"
)
/**
A handler for aop
*/
type AspectHandler interface {
ShouldAppendOn(req *http.Request) bool
Server(ServletContext, http.ResponseWriter, *http.Request) bool
BeforeOrAfter() bool
}
type DefaultAspectHandler struct {
CustomCheck func(req *http.Request) bool
Execute func(ServletContext, http.ResponseWriter, *http.Request) bool
MatchPath string
ExcludePath []string
StrictMatch bool
PositionFlg bool
}
func (defaultAspectHandler *DefaultAspectHandler) ShouldAppendOn(req *http.Request) bool {
if defaultAspectHandler.CustomCheck != nil {
return defaultAspectHandler.CustomCheck(req)
}
path := strings.Split(req.RequestURI, "?")[0]
if defaultAspectHandler.StrictMatch {
return defaultAspectHandler.MatchPath == path
} else {
if len(defaultAspectHandler.ExcludePath) != 0 {
for _, excludePath := range defaultAspectHandler.ExcludePath {
if strings.HasPrefix(path, excludePath) {
return false
}
}
}
if strings.HasPrefix(path, defaultAspectHandler.MatchPath) {
return true
}
}
return false
}
func (defaultAspectHandler *DefaultAspectHandler) BeforeOrAfter() bool {
return defaultAspectHandler.PositionFlg
}
func (defaultAspectHandler *DefaultAspectHandler) Server(serverContext ServletContext, resp http.ResponseWriter, req *http.Request) bool {
return defaultAspectHandler.Execute(serverContext, resp, req)
}