-
Notifications
You must be signed in to change notification settings - Fork 86
/
routes.go
77 lines (66 loc) · 2.38 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
package server
import (
"log"
"net/http"
"runtime/debug"
"github.com/julienschmidt/httprouter"
"github.com/karlkeefer/pngr/golang/db"
"github.com/karlkeefer/pngr/golang/env"
"github.com/karlkeefer/pngr/golang/errors"
"github.com/karlkeefer/pngr/golang/server/handlers"
"github.com/karlkeefer/pngr/golang/server/write"
)
func (srv *server) ConfigureRouter() {
srv.router = httprouter.New()
// setup error handlers for our router
srv.router.MethodNotAllowed = write.Error(errors.BadRequestMethod)
srv.router.NotFound = write.Error(errors.RouteNotFound)
srv.router.PanicHandler = func(w http.ResponseWriter, r *http.Request, err interface{}) {
log.Println("Panic on", r.URL.Path)
debug.PrintStack()
write.Error(errors.InternalError)(w, r)
}
// SESSION
srv.POST("/session", handlers.Login)
srv.DELETE("/session", handlers.Logout)
// RESETS
srv.POST("/reset", handlers.CreateReset)
srv.GET("/reset/:code", handlers.DoReset)
// USER
srv.POST("/user", handlers.Signup)
srv.GET("/user", handlers.Whoami)
srv.POST("/user/verify", handlers.Verify)
srv.PUT("/user/password", handlers.UpdatePassword)
// POSTS
srv.GET("/post", handlers.GetPosts)
srv.GET("/post/:id", handlers.GetPost)
srv.POST("/post", handlers.CreatePost)
srv.PUT("/post", handlers.UpdatePost)
srv.DELETE("/post/:id", handlers.DeletePost)
}
// srvHandler is the extended handler function that our API routes use
type srvHandler func(env env.Env, user *db.User, w http.ResponseWriter, r *http.Request) http.HandlerFunc
// helpers for easily adding routes
func (srv *server) GET(path string, handler srvHandler) {
srv.router.HandlerFunc(http.MethodGet, path, srv.wrap(handler))
}
func (srv *server) PUT(path string, handler srvHandler) {
srv.router.HandlerFunc(http.MethodPut, path, srv.wrap(handler))
}
func (srv *server) POST(path string, handler srvHandler) {
srv.router.HandlerFunc(http.MethodPost, path, srv.wrap(handler))
}
func (srv *server) DELETE(path string, handler srvHandler) {
srv.router.HandlerFunc(http.MethodDelete, path, srv.wrap(handler))
}
// wrap does all the middleware together
func (srv *server) wrap(h srvHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// convert our fancy handler to a normal handlerFunc
fn := withUserAndEnv(srv.env, h, w, r)
// wrap it with middlewares
wrapped := lag(csrf(cors(fn)))
// execute the wrapped handler
wrapped(w, r)
}
}