-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
171 lines (152 loc) · 4.34 KB
/
rest.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
package knox
import (
"encoding/json"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"net/http"
"os"
"time"
)
/**
* Error Format for all Action
*/
type Error struct {
Status int `json:"status"`
Message string `json:"message"`
}
/**
* Format and send error
*/
func (e Error) Send(w http.ResponseWriter, code int) {
log.Warning(e.Message)
w.Header().Add("Content-type", "application/json; charset=utf-8")
w.WriteHeader(code)
json.NewEncoder(w).Encode(e)
}
type Response struct {
Status int `json:"status"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
/**
* Authentification protocol
*/
type SecureMethod struct {
RequireAuthentification bool
Protocol string
Values map[string]string
ReturnFormat string
}
/**
* Custom router to store security of route
*/
type SecureRouter struct {
mux.Router
LogQueryMode bool // Log basic query and responde (HTTP code, URL, time ...)
LogContentMode bool // Log query and response body and headers witch can be hudge
security map[string]SecureMethod
Protocol map[string]func(http.Handler, bool) http.Handler
ServeCallback func(*ResponseWriterLogger, *http.Request, time.Time)
}
func (sr *SecureRouter) HandleFunc(path string, f func(http.ResponseWriter, *http.Request), secureMethod ...SecureMethod) *mux.Route {
if len(secureMethod) > 0 {
sr.security[path] = secureMethod[0]
} else {
sr.security[path] = SecureMethod{RequireAuthentification: false, ReturnFormat: "json"}
}
return sr.Router.HandleFunc(path, f)
}
func (sr *SecureRouter) AddProtocol(protocol string, f func(http.Handler, bool) http.Handler) {
sr.Protocol[protocol] = f
}
//Wrapper to log query execution time and print it
func (rl SecureRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
start := time.Now()
if rl.LogQueryMode {
logQuery(r, rl.LogContentMode)
}
// Start custom respons Writer to store response and status
var sw = &ResponseWriterLogger{ResponseWriter: w}
var match mux.RouteMatch
if rl.Match(r, &match) {
// In case of error, use default mux serve for NotFoundHandler, MethodNotAllowedHandler etc ...
if match.MatchErr != nil {
rl.Router.ServeHTTP(sw, r)
if rl.LogQueryMode {
logResponse(sw, r, start, rl.LogContentMode)
}
if rl.ServeCallback != nil {
rl.ServeCallback(sw, r, start)
}
return
} else {
pathTemplate, _ := match.Route.GetPathTemplate()
if secureMethod, ok := rl.security[pathTemplate]; ok {
r.Header.Set("KNOX_PATH_TEMPLATE", pathTemplate)
switch secureMethod.ReturnFormat {
case "json":
w.Header().Add("Content-type", "application/json; charset=utf-8")
case "html":
w.Header().Add("Content-type", "text/html; charset=utf-8")
default:
w.Header().Add("Content-type", "application/json; charset=utf-8")
}
// Security only use path and ignore method
if secureMethod.Protocol != "" {
if protocol, ok := rl.Protocol[secureMethod.Protocol]; ok {
protocol(&rl.Router, secureMethod.RequireAuthentification).ServeHTTP(sw, r)
} else {
http.HandlerFunc(error401).ServeHTTP(sw, r)
}
if rl.LogQueryMode {
logResponse(sw, r, start, rl.LogContentMode)
}
if rl.ServeCallback != nil {
rl.ServeCallback(sw, r, start)
}
return
} else {
// Not secure route
rl.Router.ServeHTTP(sw, r)
if rl.LogQueryMode {
logResponse(sw, r, start, rl.LogContentMode)
}
if rl.ServeCallback != nil {
rl.ServeCallback(sw, r, start)
}
return
}
}
}
}
rl.Router.ServeHTTP(sw, r)
if rl.ServeCallback != nil {
rl.ServeCallback(sw, r, start)
}
if rl.LogQueryMode {
logResponse(sw, r, start, rl.LogContentMode)
}
}
func NewRouter() SecureRouter {
var router = mux.NewRouter()
// Error handler
router.NotFoundHandler = http.HandlerFunc(error404) // This handler is call in RestHandler.ServeHttp
router.MethodNotAllowedHandler = http.HandlerFunc(error405)
var sr = SecureRouter{
*router,
false,
false,
map[string]SecureMethod{},
map[string]func(http.Handler, bool) http.Handler{},
nil,
}
// Defaut middleware
sr.Protocol["jwt"] = jwtMiddleware
// Default handler
sr.HandleFunc("/robots.txt", robot)
sr.HandleFunc("/favicon.ico", favicon)
if os.Getenv("PROB_URL") != "" {
sr.HandleFunc(os.Getenv("PROB_URL"), probe)
}
return sr
}