-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.go
109 lines (86 loc) · 2.12 KB
/
filter.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
//Copyright
//go mvc web framework
//filter
package gomvc
import (
"strings"
)
//http filter
type FilterItem struct {
Name string //name of filter
PathPrefix string //path match
Method string //http method
PassOnResult bool //pass if result exists
Handler HttpFilter //handler
}
func (f *FilterItem) Match(ctx *HttpContext) bool {
if !strings.HasPrefix(ctx.RequestPath, f.PathPrefix) {
return false
}
if f.PassOnResult && ctx.Result != nil {
return false
}
if f.Method == "*" || f.Method == "" || strings.Contains(f.Method, ctx.Method) {
return true
}
return false
}
//filter handler interface
type HttpFilter interface {
Execute(ctx *HttpContext)
}
//wrap of func
type HandlerFunc func(ctx *HttpContext)
//wrap of func execute
func (f HandlerFunc) Execute(ctx *HttpContext) {
f(ctx)
}
/*
//http filter base
type FilterBase struct {
PassOnError bool //
PassOnResult bool //
Server *HttpServer //
}
*/
//static file fileter
type StaticFilter struct {
Server *HttpServer //
}
//
func newStaticFilter(s *HttpServer) *StaticFilter {
f := &StaticFilter{Server: s}
return f
}
//static file fileter execute
func (filter *StaticFilter) Execute(ctx *HttpContext) {
if EnableDebug {
Logger.Println("static file filter, request path is", ctx.RequestPath)
}
ctx.PhysicalPath = filter.Server.MapPath(ctx.RequestPath)
if EnableDebug {
Logger.Println("static file filter, physical path is", ctx.PhysicalPath)
}
if ctx.PhysicalPath != "" {
ctx.Result = &FileResult{Data: ctx.PhysicalPath}
}
}
//render result filter
type RenderFilter struct {
Server *HttpServer //
}
//
func newRenderFilter(s *HttpServer) *RenderFilter {
f := &RenderFilter{Server: s}
return f
}
//render result filter execute
func (filter *RenderFilter) Execute(ctx *HttpContext) {
if EnableDebug {
Logger.Println("render filter, result:", ctx.Result)
}
if ctx.Result == nil {
ctx.Result = new(NotFoundResult)
}
ctx.Result.Execute(ctx)
}