-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
86 lines (71 loc) · 1.94 KB
/
service.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
package fipple
import (
"net/http"
)
const (
allowOrigin = "Access-Control-Allow-Origin"
allowMethods = "Access-Control-Allow-Methods"
allowHeaders = "Access-Control-Allow-Headers"
oringinAllowed = "*"
headersAllowed = "Content-Type, X-Token"
methodsAllowed = "POST, GET, OPTIONS"
)
type Service struct {
//routes *parsedRoutes
router Router
//req *RpcRequest
}
func NewService() *Service {
return NewServiceWithRouterType(Regex)
}
func NewServiceWithRouterType(rType RouterType) *Service {
r := newRegexRouter()
//if rType == Regex {}
return &Service{r}
}
func (svc *Service) Start(port string) error {
getLogger().Info("http listen:", port)
return http.ListenAndServe(port, svc)
}
func (svc *Service) AddRoutes(route ...*route) {
//svc.routes.addAll(route)
}
func (svc *Service) AddRoute(route *route) {
//svc.routes.add(route)
svc.router.addRoute(route)
}
func (svc *Service) Get(path string, action action) {
svc.router.addRoute(GetRoute(path, action))
}
func (svc *Service) Post(path string, action action) {
svc.router.addRoute(PostRoute(path, action))
}
func (svc *Service) Delete(path string, action action) {
getLogger().Debug("add path:", path)
svc.router.addRoute(DeleteRoute(path, action))
}
func (svc *Service) FileServer() {
//todo: add file server
//http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir("./static/"))))
}
func (svc *Service) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if req.RequestURI == "*" {
if req.ProtoAtLeast(1, 1) {
rw.Header().Set("Connection", "close")
}
rw.WriteHeader(http.StatusBadRequest)
return
}
originControl(rw)
if req.Method == "OPTIONS" {
return
}
ctx := NewContext(rw, req)
svc.router.dispatch(req.URL.Path, ctx)
}
func originControl(rw http.ResponseWriter) {
//todo: config acceptable origin
rw.Header().Set(allowOrigin, oringinAllowed)
rw.Header().Set(allowMethods, methodsAllowed)
rw.Header().Set(allowHeaders, headersAllowed)
}