Skip to content

Commit 010d7c9

Browse files
committed
cool
1 parent 63c7866 commit 010d7c9

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

router.go

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import (
1818
// CustomRouter wraps gorilla mux with database, redis and renderer
1919
type CustomRouter struct {
2020
// Router *mux.Router
21-
Mux *bone.Mux
22-
Store *datastore.Datastore
21+
Mux *bone.Mux
22+
Store *datastore.Datastore
23+
AuthHandler func(store *datastore.Datastore, fn http.HandlerFunc, authMethod string) http.HandlerFunc
2324
}
2425

2526
func New(store *datastore.Datastore) *CustomRouter {
@@ -31,6 +32,20 @@ func New(store *datastore.Datastore) *CustomRouter {
3132
// r.Handle("/attachments/", http.StripPrefix("/attachments/", http.FileServer(http.Dir(store.Settings.AttachmentsFolder))))
3233
customRouter.Mux = r
3334
customRouter.Store = store
35+
customRouter.AuthHandler = authenticate
36+
return customRouter
37+
}
38+
39+
func CustomAuth(store *datastore.Datastore, authFn func(store *datastore.Datastore, fn http.HandlerFunc, authMethod string) http.HandlerFunc) *CustomRouter {
40+
customRouter := &CustomRouter{}
41+
r := bone.New()
42+
r.CaseSensitive = false
43+
// r.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./public/"))))
44+
// r.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("./assets/"))))
45+
// r.Handle("/attachments/", http.StripPrefix("/attachments/", http.FileServer(http.Dir(store.Settings.AttachmentsFolder))))
46+
customRouter.Mux = r
47+
customRouter.Store = store
48+
customRouter.AuthHandler = authFn
3449
return customRouter
3550
}
3651

@@ -42,63 +57,63 @@ func (customRouter *CustomRouter) Application(route string, path string) *bone.R
4257

4358
func (customRouter *CustomRouter) GET(route string, routeFunc CustomHandlerFunc, securityType string) *bone.Route {
4459
//route = strings.ToLower(route)
45-
return customRouter.Mux.GetFunc(route, CustomHandler("GET", customRouter.Store, routeFunc, securityType))
60+
return customRouter.Mux.GetFunc(route, customRouter.customHandler("GET", customRouter.Store, routeFunc, securityType))
4661
}
4762

4863
// POST - Post handler
4964
func (customRouter *CustomRouter) POST(route string, routeFunc CustomHandlerFunc, securityType string) *bone.Route {
5065
//route = strings.ToLower(route)
51-
return customRouter.Mux.PostFunc(route, CustomHandler("POST", customRouter.Store, routeFunc, securityType))
66+
return customRouter.Mux.PostFunc(route, customRouter.customHandler("POST", customRouter.Store, routeFunc, securityType))
5267
}
5368

5469
// PST - Post handler with pst for tidier lines
5570
func (customRouter *CustomRouter) PST(route string, routeFunc CustomHandlerFunc, securityType string) *bone.Route {
5671
//route = strings.ToLower(route)
57-
return customRouter.Mux.PostFunc(route, CustomHandler("POST", customRouter.Store, routeFunc, securityType))
72+
return customRouter.Mux.PostFunc(route, customRouter.customHandler("POST", customRouter.Store, routeFunc, securityType))
5873
}
5974

6075
// PUT - Put handler
6176
func (customRouter *CustomRouter) PUT(route string, routeFunc CustomHandlerFunc, securityType string) *bone.Route {
6277
//route = strings.ToLower(route)
63-
return customRouter.Mux.PutFunc(route, CustomHandler("PUT", customRouter.Store, routeFunc, securityType))
78+
return customRouter.Mux.PutFunc(route, customRouter.customHandler("PUT", customRouter.Store, routeFunc, securityType))
6479
}
6580

6681
// PATCH - Patch handler
6782
func (customRouter *CustomRouter) PATCH(route string, routeFunc CustomHandlerFunc, securityType string) *bone.Route {
6883
//route = strings.ToLower(route)
69-
return customRouter.Mux.PatchFunc(route, CustomHandler("PATCH", customRouter.Store, routeFunc, securityType))
84+
return customRouter.Mux.PatchFunc(route, customRouter.customHandler("PATCH", customRouter.Store, routeFunc, securityType))
7085
}
7186

7287
// OPTIONS - Options handler
7388
func (customRouter *CustomRouter) OPTIONS(route string, routeFunc CustomHandlerFunc, securityType string) *bone.Route {
7489
//route = strings.ToLower(route)
75-
return customRouter.Mux.OptionsFunc(route, CustomHandler("OPTIONS", customRouter.Store, routeFunc, securityType))
90+
return customRouter.Mux.OptionsFunc(route, customRouter.customHandler("OPTIONS", customRouter.Store, routeFunc, securityType))
7691
}
7792

7893
// DELETE - Delete handler
7994
func (customRouter *CustomRouter) DELETE(route string, routeFunc CustomHandlerFunc, securityType string) *bone.Route {
8095
//route = strings.ToLower(route)
81-
return customRouter.Mux.DeleteFunc(route, CustomHandler("DELETE", customRouter.Store, routeFunc, securityType))
96+
return customRouter.Mux.DeleteFunc(route, customRouter.customHandler("DELETE", customRouter.Store, routeFunc, securityType))
8297
}
8398

8499
// DEL - Delete handler
85100
func (customRouter *CustomRouter) DEL(route string, routeFunc CustomHandlerFunc, securityType string) *bone.Route {
86101
//route = strings.ToLower(route)
87-
return customRouter.Mux.DeleteFunc(route, CustomHandler("DELETE", customRouter.Store, routeFunc, securityType))
102+
return customRouter.Mux.DeleteFunc(route, customRouter.customHandler("DELETE", customRouter.Store, routeFunc, securityType))
88103
}
89104

90-
func CustomHandler(reqType string, store *datastore.Datastore, fn CustomHandlerFunc, authMethod string) http.HandlerFunc {
91-
return authenticate(store, func(w http.ResponseWriter, req *http.Request) {
105+
func (customRouter *CustomRouter) customHandler(reqType string, store *datastore.Datastore, fn CustomHandlerFunc, authMethod string) http.HandlerFunc {
106+
return customRouter.AuthHandler(store, func(w http.ResponseWriter, req *http.Request) {
92107
fn(flow.New(w, req, store))
93108
}, authMethod)
94109
}
95110

96-
// DefaultHandler wraps default http functions in auth it does not pass the data store to them
97-
func DefaultHandler(store *datastore.Datastore, fn http.HandlerFunc, authMethod string) http.HandlerFunc {
98-
return authenticate(store, func(w http.ResponseWriter, req *http.Request) {
99-
fn(w, req)
100-
}, authMethod)
101-
}
111+
// // DefaultHandler wraps default http functions in auth it does not pass the data store to them
112+
// func DefaultHandler(store *datastore.Datastore, fn http.HandlerFunc, authMethod string) http.HandlerFunc {
113+
// return authenticate(store, func(w http.ResponseWriter, req *http.Request) {
114+
// fn(w, req)
115+
// }, authMethod)
116+
// }
102117

103118
func authenticate(store *datastore.Datastore, fn http.HandlerFunc, authMethod string) http.HandlerFunc {
104119
return func(w http.ResponseWriter, req *http.Request) {
@@ -170,11 +185,6 @@ func authenticate(store *datastore.Datastore, fn http.HandlerFunc, authMethod st
170185
fn(w, req)
171186
return
172187
}
173-
// else if err != nil { // we dont care about the error
174-
// // WE ONLY check the error after the above because if we aren't authenticating then we will get an error
175-
// view.JSON(w, http.StatusForbidden, err.Error())
176-
// return
177-
// }
178188

179189
// if we have reached this point then the user doesn't have access
180190
if authMethod == security.Disallow {

0 commit comments

Comments
 (0)