-
Notifications
You must be signed in to change notification settings - Fork 1
/
klcontroller.go
97 lines (82 loc) · 2.01 KB
/
klcontroller.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
package koala
import (
"net/http"
"io"
"reflect"
)
type Controller struct {
Ctx *Context
methodMapping map[string]func()
}
type ControllerInterface interface {
Init()
Get()
Post()
Put()
Patch()
Delete()
Options()
URLMapping()
Match(ctx *Context)
}
func (c *Controller)Init() {
c.methodMapping = make(map[string]func(), 0)
}
func (c *Controller)Get() {
c.Ctx.Writer.WriteHeader(http.StatusNotFound)
io.WriteString(c.Ctx.Writer, `no Controller Get match`)
}
func (c *Controller)Post() {
c.Ctx.Writer.WriteHeader(http.StatusNotFound)
io.WriteString(c.Ctx.Writer, `no Controller Post match`)
}
func (c *Controller)Put() {
c.Ctx.Writer.WriteHeader(http.StatusNotFound)
io.WriteString(c.Ctx.Writer, `no Controller Put match`)
}
func (c *Controller)Patch() {
c.Ctx.Writer.WriteHeader(http.StatusNotFound)
io.WriteString(c.Ctx.Writer, `no Controller Patch match`)
}
func (c *Controller)Delete() {
c.Ctx.Writer.WriteHeader(http.StatusNotFound)
io.WriteString(c.Ctx.Writer, `no Controller Delete match`)
}
func (c *Controller)Options() {
c.Ctx.Writer.WriteHeader(http.StatusNotFound)
io.WriteString(c.Ctx.Writer, `no Controller Options match`)
}
func (c *Controller)NotSupportMethod() {
c.Ctx.Writer.WriteHeader(http.StatusNotFound)
io.WriteString(c.Ctx.Writer, `no support method`)
}
func (c *Controller)URLMapping() {
}
func (c *Controller)Mapping(method string, fn func()) {
c.methodMapping[method] = fn
}
func (c *Controller)Match(ctx *Context) {
c.Ctx = ctx
c.Ctx.ParseForm()
if _, ok := c.methodMapping[c.Ctx.Request.Method]; ok {
fn := c.methodMapping[c.Ctx.Request.Method]
fv := reflect.ValueOf(fn)
fv.Call(nil)
} else {
if c.Ctx.Request.Method == GET {
c.Get()
} else if c.Ctx.Request.Method == PUT {
c.Put()
} else if c.Ctx.Request.Method == POST {
c.Post()
} else if c.Ctx.Request.Method == PATCH {
c.Patch()
} else if c.Ctx.Request.Method == DELETE {
c.Delete()
} else if c.Ctx.Request.Method == OPTIONS {
c.Options()
} else {
c.NotSupportMethod()
}
}
}