-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
160 lines (135 loc) · 4.81 KB
/
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
package oauth2
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/Sirupsen/logrus"
"github.com/phuc0302/go-oauth2/util"
)
// Server describes server object.
type Server struct {
sandbox bool
router *ServerRouter
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Run will start server on http port.
func (s *Server) Run() {
address := fmt.Sprintf("%s:%d", Cfg.Host, Cfg.Port)
server := &http.Server{
Addr: address,
ReadTimeout: Cfg.ReadTimeout,
WriteTimeout: Cfg.WriteTimeout,
MaxHeaderBytes: Cfg.HeaderSize,
Handler: s,
}
logrus.Infof("listening on %s", address)
logrus.Fatal(server.ListenAndServe())
}
// RunTLS will start server on https port.
func (s *Server) RunTLS(certFile string, keyFile string) {
address := fmt.Sprintf("%s:%d", Cfg.Host, Cfg.TLSPort)
server := &http.Server{
Addr: address,
ReadTimeout: Cfg.ReadTimeout,
WriteTimeout: Cfg.WriteTimeout,
MaxHeaderBytes: Cfg.HeaderSize,
Handler: s,
}
logrus.Infof("listening on %s\n", address)
logrus.Fatal(server.ListenAndServeTLS(certFile, keyFile))
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// GroupRoles binds user's roles to all url with same prefix.
func (s *Server) GroupRoles(groupPath string, roles ...string) {
s.router.groupRoles(groupPath, roles...)
}
//// BindRoles an url pattern with user's roles.
//func (s *Server) BindRoles(httpMethod string, urlPattern string, roles ...string) {
// s.router.BindRoles(httpMethod, urlPattern, roles...)
//}
// GroupRoute routes all url with same prefix.
func (s *Server) GroupRoute(urlPrefix string, handler GroupHandler) {
s.router.groupRoute(s, urlPrefix, handler)
}
// Copy routes copy request to registered handler.
func (s *Server) Copy(urlPattern string, handler ContextHandler) {
s.router.bindRoute(Copy, urlPattern, handler)
}
// Delete routes delete request to registered handler.
func (s *Server) Delete(urlPattern string, handler ContextHandler) {
s.router.bindRoute(Delete, urlPattern, handler)
}
// Get routes get request to registered handler.
func (s *Server) Get(urlPattern string, handler ContextHandler) {
s.router.bindRoute(Get, urlPattern, handler)
}
// Head routes head request to registered handler.
func (s *Server) Head(urlPattern string, handler ContextHandler) {
s.router.bindRoute(Head, urlPattern, handler)
}
// Link routes link request to registered handler.
func (s *Server) Link(urlPattern string, handler ContextHandler) {
s.router.bindRoute(Link, urlPattern, handler)
}
// Options routes options request to registered handler.
func (s *Server) Options(urlPattern string, handler ContextHandler) {
s.router.bindRoute(Options, urlPattern, handler)
}
// Patch routes patch request to registered handler.
func (s *Server) Patch(urlPattern string, handler ContextHandler) {
s.router.bindRoute(Patch, urlPattern, handler)
}
// Post routes post request to registered handler.
func (s *Server) Post(urlPattern string, handler ContextHandler) {
s.router.bindRoute(Post, urlPattern, handler)
}
// Purge routes purge request to registered handler.
func (s *Server) Purge(urlPattern string, handler ContextHandler) {
s.router.bindRoute(Purge, urlPattern, handler)
}
// Put routes put request to registered handler.
func (s *Server) Put(urlPattern string, handler ContextHandler) {
s.router.bindRoute(Put, urlPattern, handler)
}
// Unlink routes unlink request to registered handler.
func (s *Server) Unlink(urlPattern string, handler ContextHandler) {
s.router.bindRoute(Unlink, urlPattern, handler)
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// ServeHTTP handle HTTP request and HTTP response.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
request := createRequestContext(r, w)
oauth := createOAuthContext(request)
// Handle error
defer recovery(request, s.sandbox)
/* Condition validation: validate request method */
if !methodsValidation.MatchString(request.Method) {
panic(util.Status405())
}
// Should redirect request to static folder or not?
if request.Method == Get && len(Cfg.StaticFolders) > 0 {
for prefix, folder := range Cfg.StaticFolders {
if path := request.Path; strings.HasPrefix(path, prefix) {
path = strings.Replace(path, prefix, folder, 1)
if file, err := os.Open(path); err == nil {
defer file.Close()
if info, _ := file.Stat(); !info.IsDir() {
http.ServeContent(w, r, path, info.ModTime(), file)
return
}
}
panic(util.Status404())
}
}
}
// Find route to handle request
if route, pathParams := s.router.matchRoute(request, oauth); route != nil {
if pathParams != nil {
request.PathParams = pathParams
}
route.invokeHandler(request, oauth)
} else {
panic(util.Status503())
}
}