-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathroutes.go
87 lines (72 loc) · 3.51 KB
/
routes.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
package main
import (
"fmt"
"net/http"
"strings"
"github.com/gorilla/mux"
"github.com/justinas/alice"
)
func (a *application) appRouter() {
//middleware that should be called on every request get added to the chain here
c := alice.New(a.recoverPanic, a.securityHeaders, a.loggingHandler, a.Session.Enable)
router := mux.NewRouter()
fs := http.StripPrefix("/assets/", http.FileServer(noListFileSystem{http.Dir("./assets")}))
// Ending the URL path with no trailing slash looks like it should be a route to a resource, but it's not. Return 404.
// Anything that does have the trailing slash may be a real request for an asset. Handle with the custom filesystem
router.PathPrefix("/assets/").Handler(fs)
//set all the routes here. Uses gorilla/mux so routes can use regex,
//and following with .Methods() allows for limiting them to only specific HTTP methods
router.HandleFunc("/", a.StaticC.Root())
router.HandleFunc("/signup", a.UserC.SignupForm()).Methods("GET")
router.HandleFunc("/signup", a.UserC.New()).Methods("POST")
router.HandleFunc("/login", a.UserC.LoginForm()).Methods("GET")
router.HandleFunc("/login", a.UserC.LoginUser()).Methods("POST")
router.HandleFunc("/logout", a.UserC.Logout()).Methods("POST")
router.HandleFunc("/user", noRoute("currently logged in user")).Methods("GET")
router.HandleFunc("/user/{id:[0-9]+}", a.UserC.Show()).Methods("GET")
router.HandleFunc("/user/{id:[0-9]+}/edit", noRoute("form to edit user")).Methods("GET")
router.HandleFunc("/user/{id:[0-9]+}", noRoute("save user update to db")).Methods("POST").MatcherFunc(makeMatcher("patch"))
router.HandleFunc("/user/{id:[0-9]+}", noRoute("delete user")).Methods("POST").MatcherFunc(makeMatcher("delete"))
router.HandleFunc("/users", a.UserC.List()).Methods("GET")
router.HandleFunc("/user/{id:[0-9]+}/ice", noRoute("update ice")).Methods("POST").MatcherFunc(makeMatcher("patch"))
router.HandleFunc("/user/{id:[0-9]+}/ice", noRoute("delete ice")).Methods("POST").MatcherFunc(makeMatcher("delete"))
router.HandleFunc("/user/{id:[0-9]+}/uploadwaiver", noRoute("uploadwaiver")).Methods("GET")
router.HandleFunc("/user/{id:[0-9]+}/uploadwaiver", noRoute("save waiver")).Methods("POST")
router.HandleFunc("/user/{id:[0-9]+}/waiver", noRoute("show waiver")).Methods("GET")
router.HandleFunc("/user/{id:[0-9]+}/waiver", noRoute("delete waiver")).Methods("POST").MatcherFunc(makeMatcher("delete"))
//TODO: need to implement handlers for 404 and 405, then implement router.NotFoundHandler and router.MethodNotAllowedHandler
//set the app router. Alice will pass all the requests through the middleware chain first,
//then to the functions defined above
a.Router = c.Then(router)
}
func makeMatcher(m string) func(*http.Request, *mux.RouteMatch) bool {
return func(r *http.Request, rm *mux.RouteMatch) bool {
return r.FormValue("_method") == m
}
}
//return a generic error for routes that are in the table but controllers are not ready
func noRoute(msg string) func(w http.ResponseWriter, r *http.Request) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, fmt.Sprintf("no handler for %s: ", msg))
})
}
//Prevent directory listings:
type noListFileSystem struct {
fs http.FileSystem
}
func (nfs noListFileSystem) Open(path string) (http.File, error) {
f, err := nfs.fs.Open(path)
if err != nil {
return nil, err
}
s, err := f.Stat()
if err != nil {
return nil, err
}
if s.IsDir() {
if _, err := nfs.fs.Open(strings.TrimSuffix(path, "/") + "/index.html"); err != nil {
return nil, err
}
}
return f, nil
}