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

37 lines
1.1 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"
)
// 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
fileServer := http.FileServer(http.Dir("./ui/static"))
2024-01-26 06:01:12 +00:00
router.Handler(http.MethodGet, "/static/*filepath", http.StripPrefix("/static", fileServer))
2024-02-07 05:06:48 +00:00
dynamic := alice.New(app.sessionManager.LoadAndSave)
router.Handler(http.MethodGet, "/", dynamic.ThenFunc(app.home))
router.Handler(http.MethodGet, "/snippet/view/:id", dynamic.ThenFunc(app.snippetView))
router.Handler(http.MethodGet, "/snippet/create", dynamic.ThenFunc(app.snippetCreate))
router.Handler(http.MethodPost, "/snippet/create", dynamic.ThenFunc(app.snippetCreatePost))
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)
}