-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrest_server.go
172 lines (144 loc) · 3.92 KB
/
rest_server.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package aqua
import (
"fmt"
"github.com/gorilla/mux"
"github.com/tolexo/aero/cache"
"net/http"
"reflect"
"strings"
"time"
)
var defaults Fixture = Fixture{
Root: "",
Url: "",
Version: "*",
Pretty: "false",
Vendor: "vnd.api",
Modules: "",
Cache: "",
Ttl: "",
Auth: "",
Log: false,
}
var release string = "0.0.1"
var defaultPort int = 8090
type RestServer struct {
Fixture
http.Server
Port int
mux *mux.Router
apis map[string]endPoint
mods map[string]func(http.Handler) http.Handler
stores map[string]cache.Cacher
}
func NewRestServer() RestServer {
r := RestServer{
Fixture: defaults,
Server: http.Server{},
Port: defaultPort,
mux: mux.NewRouter(),
apis: make(map[string]endPoint),
mods: make(map[string]func(http.Handler) http.Handler),
stores: make(map[string]cache.Cacher),
}
r.AddService(&CoreService{})
return r
}
func (me *RestServer) AddNotFoundHandler(name http.HandlerFunc) {
handlerType := reflect.TypeOf(name)
if handlerType.Kind() == reflect.Func {
me.mux.NotFoundHandler = http.HandlerFunc(name)
}
}
var printed bool = false
func (me *RestServer) AddModule(name string, f func(http.Handler) http.Handler) {
// TODO: check if the same key alread exists
// TODO: AddModule must be called before AddService
me.mods[name] = f
}
func (me *RestServer) AddCache(name string, c cache.Cacher) {
// TODO: check if the same key already exists
// TODO: AddCache must be called before AddService
me.stores[name] = c
}
func (me *RestServer) AddService(svc interface{}) {
svcType := reflect.TypeOf(svc)
code := getSymbolFromType(svcType)
// validation: must be pointer
if !strings.HasPrefix(code, "*st:") {
panic("RestServer.AddService() expects address of your Service object")
}
// validation: RestService field must be present and be anonymous
rs, ok := svcType.Elem().FieldByName("RestService")
if !ok || !rs.Anonymous || !rs.Type.ConvertibleTo(reflect.TypeOf(RestService{})) {
panic("RestServer.AddService() expects object that contains anonymous RestService field")
}
objType := svcType.Elem()
obj := reflect.ValueOf(svc).Elem()
fixSvcTag := NewFixtureFromTag(svc, "RestService")
fixSvc := obj.FieldByName("RestService").FieldByName("Fixture").Interface().(Fixture)
var fix Fixture
var method string
if !printed {
fmt.Println("Loading...")
printed = true
}
for i := 0; i < objType.NumField(); i++ {
field := objType.FieldByIndex([]int{i})
method = getHttpMethod(field)
if method == "" {
continue
}
fixFldTag := NewFixtureFromTag(svc, field.Name)
fix = resolveInOrder(fixFldTag, fixSvc, fixSvcTag, me.Fixture)
if fix.Root == "" {
tmp := objType.Name()
if strings.HasSuffix(tmp, "Service") {
tmp = tmp[0 : len(tmp)-len("Service")]
}
fix.Root = toUrlCase(tmp)
}
if fix.Url == "" {
fix.Url = toUrlCase(field.Name)
}
matchUrl := cleanUrl(fix.Root, fix.Url)
serviceId := getServiceId(method, fix.Prefix, fix.Version, matchUrl)
if _, found := me.apis[serviceId]; found {
panic("Cannot load service again: " + serviceId)
}
inv := NewMethodInvoker(svc, upFirstChar(field.Name))
if inv.exists || fix.Stub != "" {
ep := NewEndPoint(inv, fix, matchUrl, method, me.mods, me.stores, serviceId)
ep.setupMuxHandlers(me.mux)
me.apis[serviceId] = ep
fmt.Printf("%s\n", serviceId)
//fmt.Println(fix)
}
}
}
func (me *RestServer) Run() {
startup(me, me.Port)
}
func (me *RestServer) RunAsync() {
go startup(me, me.Port)
// TODO: don't sleep, check for the server to come up, and panic if
// it doesn't even after 5 sec
time.Sleep(time.Millisecond * 50)
}
func (me *RestServer) RunWith(port int, sync bool) {
me.Port = port
if sync {
me.Run()
} else {
me.RunAsync()
}
}
func startup(r *RestServer, port int) {
if port > 0 {
r.Addr = fmt.Sprintf(":%d", port)
} else if r.Server.Addr == "" {
r.Addr = fmt.Sprintf(":%d", port)
}
r.Server.Handler = r.mux
fmt.Println(r.ListenAndServe())
}