/
outboy.go
68 lines (57 loc) · 1.34 KB
/
outboy.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
package outboy
import (
"fmt"
"github.com/evil-go/fall"
"net/http"
"reflect"
"strings"
)
var (
rwt = reflect.TypeOf((*http.ResponseWriter)(nil)).Elem()
rt = reflect.TypeOf((*http.Request)(nil))
)
type regInfo struct {
controller interface{}
paths map[string]string
}
type outboy struct {
registrations []regInfo
}
var ob = &outboy{}
func (o *outboy) register() {
for _, v := range o.registrations {
i := v.controller
paths := v.paths
t := reflect.TypeOf(i)
vi := reflect.ValueOf(i)
for k, v := range paths {
if m, ok := t.MethodByName(k); ok {
f := m.Func
ft := f.Type()
if ft.NumIn() != 3 || ft.NumOut() != 0 || ft.In(0) != t || ft.In(1) != rwt || ft.In(2) != rt {
panic("Invalid method " + f.String())
}
fmt.Println("registering endpoint", m.Name, "at", v)
http.HandleFunc(v, func(rw http.ResponseWriter, req *http.Request) {
if strings.EqualFold(m.Name[:len(req.Method)], req.Method) {
f.Call([]reflect.Value{vi, reflect.ValueOf(rw), reflect.ValueOf(req)})
}
})
}
}
}
}
var port = ":8080"
func Port(p int) {
port = fmt.Sprintf(":%d", p)
}
func Register(i interface{}, paths map[string]string) {
ob.registrations = append(ob.registrations, regInfo{i, paths})
}
func (o *outboy) InitLast() {
o.register()
http.ListenAndServe(port, nil)
}
func init() {
fall.Register(ob)
}