-
Notifications
You must be signed in to change notification settings - Fork 12
/
router.go
66 lines (57 loc) · 1.39 KB
/
router.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
package hera
import (
"net/http"
"reflect"
"strings"
)
var r *Router
type Router struct {
autoRouter map[string]reflect.Type
}
func (this *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
structMethod := r.URL.Path
structType, ok := this.autoRouter[structMethod]
if !ok {
w.WriteHeader(404)
w.Write([]byte("NO REST "))
return
}
methods := strings.Split(structMethod, "/")
methodName := methods[2]
params := make(map[string]string)
for formKey, formValue := range r.Form {
params[formKey] = formValue[0]
}
method := reflect.New(structType).MethodByName(methodName)
in := make([]reflect.Value, 1)
in[0] = reflect.ValueOf(&Context{Params: params, Req: r, Res: w})
method.Call(in)
return
}
func (this *Router) Start(addr string) {
http.ListenAndServe(addr, this)
}
func (this *Router) AddRouter(i interface{}) {
reflectVal := reflect.ValueOf(i)
reflectType := reflectVal.Type()
structType := reflect.Indirect(reflectVal).Type()
structName := structType.Name()
structPrefix := strings.TrimSuffix(structName, "REST")
if structName == structPrefix {
return
}
for i := 0; i < reflectType.NumMethod(); i++ {
methodName := reflectType.Method(i).Name
key := "/" + structPrefix + "/" + methodName
this.autoRouter[key] = structType
}
}
func NewRouter() *Router {
if r == nil {
r = &Router{
autoRouter: make(map[string]reflect.Type),
}
}
return r
}