learn-go/snippetbox/cmd/web/routes.go

47 lines
1.6 KiB
Go
Raw Normal View History

package main
import (
"net/http"
2024-01-26 00:27:56 +00:00
2024-01-26 06:01:12 +00:00
"github.com/julienschmidt/httprouter"
2024-01-26 00:27:56 +00:00
"github.com/justinas/alice"
2024-02-08 18:11:53 +00:00
"snippetbox.chaosfem.tw/ui"
)
// routes ...
2024-01-25 23:43:07 +00:00
func (app *application) routes() http.Handler {
2024-01-26 06:01:12 +00:00
router := httprouter.New()
2024-01-26 06:31:27 +00:00
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
2024-02-08 18:11:53 +00:00
fileServer := http.FileServer(http.FS(ui.Files))
router.Handler(http.MethodGet, "/static/*filepath", fileServer)
2024-02-08 17:26:25 +00:00
dynamic := alice.New(app.sessionManager.LoadAndSave, noSurf, app.authenticate)
2024-02-07 05:06:48 +00:00
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))
2024-02-08 06:46:35 +00:00
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))
2024-01-26 00:27:56 +00:00
standard := alice.New(app.recoverPanic, app.logRequest, secureHeaders)
2024-01-26 06:01:12 +00:00
return standard.Then(router)
}