49 lines
1.6 KiB
Go
49 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
"github.com/justinas/alice"
|
|
|
|
"snippetbox.chaosfem.tw/ui"
|
|
)
|
|
|
|
// routes ...
|
|
func (app *application) routes() http.Handler {
|
|
router := httprouter.New()
|
|
|
|
router.NotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
app.notFound(w)
|
|
})
|
|
|
|
router.MethodNotAllowed = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
app.clientError(w, http.StatusMethodNotAllowed)
|
|
})
|
|
|
|
// setup server for static files
|
|
fileServer := http.FileServer(http.FS(ui.Files))
|
|
router.Handler(http.MethodGet, "/static/*filepath", fileServer)
|
|
|
|
router.HandlerFunc(http.MethodGet, "/ping", ping)
|
|
|
|
dynamic := alice.New(app.sessionManager.LoadAndSave, noSurf, app.authenticate)
|
|
|
|
router.Handler(http.MethodGet, "/", dynamic.ThenFunc(app.home))
|
|
router.Handler(http.MethodGet, "/snippet/view/:id", dynamic.ThenFunc(app.snippetView))
|
|
router.Handler(http.MethodGet, "/user/signup", dynamic.ThenFunc(app.userSignup))
|
|
router.Handler(http.MethodPost, "/user/signup", dynamic.ThenFunc(app.userSignupPost))
|
|
router.Handler(http.MethodGet, "/user/login", dynamic.ThenFunc(app.userLogin))
|
|
router.Handler(http.MethodPost, "/user/login", dynamic.ThenFunc(app.userLoginPost))
|
|
|
|
protected := dynamic.Append(app.requireAuthentication)
|
|
|
|
router.Handler(http.MethodGet, "/snippet/create", protected.ThenFunc(app.snippetCreate))
|
|
router.Handler(http.MethodPost, "/snippet/create", protected.ThenFunc(app.snippetCreatePost))
|
|
router.Handler(http.MethodPost, "/user/logout", protected.ThenFunc(app.userLogoutPost))
|
|
|
|
standard := alice.New(app.recoverPanic, app.logRequest, secureHeaders)
|
|
|
|
return standard.Then(router)
|
|
}
|