-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
194 lines (168 loc) · 6.72 KB
/
router.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package router
import (
"net/http"
"os"
"strings"
"github.com/go-zoo/bone"
"github.com/nerdynz/datastore"
"github.com/nerdynz/flow"
"github.com/nerdynz/security"
"github.com/unrolled/render"
)
// CustomRouter wraps gorilla mux with database, redis and renderer
type CustomRouter struct {
// Router *mux.Router
Mux *bone.Mux
Renderer *render.Render
Key security.Key
Store *datastore.Datastore
AuthHandler func(w http.ResponseWriter, req *http.Request, flw *flow.Flow, store *datastore.Datastore, fn CustomHandlerFunc, authMethod string)
}
type CustomHandlerFunc func(w http.ResponseWriter, req *http.Request, flw *flow.Flow, store *datastore.Datastore)
func New(renderer *render.Render, s *datastore.Datastore, key security.Key, caseSensitive bool) *CustomRouter {
customRouter := &CustomRouter{}
r := bone.New()
r.CaseSensitive = caseSensitive
customRouter.Mux = r
customRouter.Store = s
customRouter.Key = key
customRouter.Renderer = renderer
customRouter.AuthHandler = authenticate
return customRouter
}
func CustomAuth(renderer *render.Render, s *datastore.Datastore, key security.Key, authFn func(w http.ResponseWriter, req *http.Request, flw *flow.Flow, store *datastore.Datastore, fn CustomHandlerFunc, authMethod string)) *CustomRouter {
customRouter := New(renderer, s, key, true)
customRouter.AuthHandler = authFn
return customRouter
}
// GET - Get handler
func (customRouter *CustomRouter) Application(route string, path string) *bone.Route {
//route = strings.ToLower(route)
return customRouter.Mux.GetFunc(route, customRouter.appHandler(path))
}
func (customRouter *CustomRouter) GET(route string, routeFunc CustomHandlerFunc, securityType string) *bone.Route {
//route = strings.ToLower(route)
return customRouter.Mux.GetFunc(route, customRouter.handler("GET", routeFunc, securityType))
}
// POST - Post handler
func (customRouter *CustomRouter) POST(route string, routeFunc CustomHandlerFunc, securityType string) *bone.Route {
//route = strings.ToLower(route)
return customRouter.Mux.PostFunc(route, customRouter.handler("POST", routeFunc, securityType))
}
// PST - Post handler with pst for tidier lines
func (customRouter *CustomRouter) PST(route string, routeFunc CustomHandlerFunc, securityType string) *bone.Route {
//route = strings.ToLower(route)
return customRouter.Mux.PostFunc(route, customRouter.handler("POST", routeFunc, securityType))
}
// PUT - Put handler
func (customRouter *CustomRouter) PUT(route string, routeFunc CustomHandlerFunc, securityType string) *bone.Route {
//route = strings.ToLower(route)
return customRouter.Mux.PutFunc(route, customRouter.handler("PUT", routeFunc, securityType))
}
// PATCH - Patch handler
func (customRouter *CustomRouter) PATCH(route string, routeFunc CustomHandlerFunc, securityType string) *bone.Route {
//route = strings.ToLower(route)
return customRouter.Mux.PatchFunc(route, customRouter.handler("PATCH", routeFunc, securityType))
}
// OPTIONS - Options handler
func (customRouter *CustomRouter) OPTIONS(route string, routeFunc CustomHandlerFunc, securityType string) *bone.Route {
//route = strings.ToLower(route)
return customRouter.Mux.OptionsFunc(route, customRouter.handler("OPTIONS", routeFunc, securityType))
}
// DELETE - Delete handler
func (customRouter *CustomRouter) DELETE(route string, routeFunc CustomHandlerFunc, securityType string) *bone.Route {
//route = strings.ToLower(route)
return customRouter.Mux.DeleteFunc(route, customRouter.handler("DELETE", routeFunc, securityType))
}
// DEL - Delete handler
func (customRouter *CustomRouter) DEL(route string, routeFunc CustomHandlerFunc, securityType string) *bone.Route {
//route = strings.ToLower(route)
return customRouter.Mux.DeleteFunc(route, customRouter.handler("DELETE", routeFunc, securityType))
}
func (customRouter *CustomRouter) handler(reqType string, fn CustomHandlerFunc, authMethod string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
flow := flow.New(w, req, customRouter.Renderer, customRouter.Store, customRouter.Key)
req.ParseForm()
customRouter.AuthHandler(w, req, flow, customRouter.Store, fn, authMethod)
}
}
// // DefaultHandler wraps default http functions in auth it does not pass the data store to them
// func DefaultHandler(store *datastore.Datastore, fn http.HandlerFunc, authMethod string) http.HandlerFunc {
// return authenticate(store, func(w http.ResponseWriter, req *http.Request) {
// fn(w, req)
// }, authMethod)
// }
func authenticate(w http.ResponseWriter, req *http.Request, flw *flow.Flow, store *datastore.Datastore, fn CustomHandlerFunc, authMethod string) {
// canonical host
canonical := store.Settings.Get("CANNONICAL_URL")
if canonical != "" && store.Settings.IsProduction() { // set in ENV
root := strings.ToLower(req.Host)
if !strings.HasSuffix(root, "/") {
root += "/"
}
if !strings.HasSuffix(canonical, "/") {
canonical += "/"
}
// logrus.Info("root", root)
// logrus.Info("root", canonical)
if canonical != root {
redirectURL := "http://"
if store.Settings.GetBool("IS_HTTPS") {
redirectURL = "https://"
}
redirectURL += strings.TrimRight(canonical, "/")
if req.URL.Path != "" {
redirectURL += req.URL.Path
// logrus.Info("0", redirectURL)
}
if req.URL.RawQuery != "" {
redirectURL += "?" + req.URL.RawQuery
// logrus.Info("2", redirectURL)
}
if req.URL.Fragment != "" {
redirectURL += "#" + req.URL.Fragment
// logrus.Info("2", redirectURL)
}
http.Redirect(w, req, redirectURL, http.StatusMovedPermanently)
return
}
}
if authMethod == security.NoAuth {
fn(w, req, flw, store)
return
}
// if we are at this point then we want a login
loggedInUser, _, err := flw.Padlock.LoggedInUser()
if err != nil {
if err.Error() == "redis: nil" {
// ignore it, its expired from cache
flw.ErrorJSON(http.StatusForbidden, "Login Expired", err)
} else {
flw.ErrorJSON(http.StatusForbidden, "Auth Failure", err)
}
return
}
if loggedInUser != nil {
fn(w, req, flw, store)
return
}
// if we have reached this point then the user doesn't have access
if authMethod == security.Disallow {
flw.ErrorJSON(http.StatusForbidden, "You're not currently logged in", err)
return
}
if authMethod == security.Redirect {
flw.Redirect("/Login", http.StatusSeeOther)
}
}
func (customRouter *CustomRouter) appHandler(file string) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
flw := flow.New(w, req, customRouter.Renderer, customRouter.Store, customRouter.Key)
fullpath, err := os.Getwd()
if err != nil {
flw.ErrorJSON(http.StatusInternalServerError, "Failed to get current working directory", err)
}
fullpath += file
flw.StaticFile(200, fullpath, "text/html; charset=utf-8")
}
}