2024-01-25 00:14:33 +00:00
|
|
|
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-01-25 00:14:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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-25 00:14:33 +00:00
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2024-01-25 00:14:33 +00:00
|
|
|
// 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-01-25 00:14:33 +00:00
|
|
|
|
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))
|
2024-02-07 19:07:29 +00:00
|
|
|
router.Handler(http.MethodGet, "/user/signup", dynamic.ThenFunc(app.userSignup))
|
|
|
|
router.Handler(http.MethodPost, "/user/signup", dynamic.ThenFunc(app.userSignupPost))
|
2024-02-07 19:22:47 +00:00
|
|
|
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-25 00:14:33 +00:00
|
|
|
|
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)
|
2024-01-25 00:14:33 +00:00
|
|
|
}
|