pcmt/app/routes.go
surtur e7be74d1eb
All checks were successful
continuous-integration/drone/push Build is passing
routes: add extra handling for signin/signup
2023-08-12 14:50:49 +02:00

85 lines
2.8 KiB
Go

// Copyright 2023 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: AGPL-3.0-only
package app
import (
"net/http"
"git.dotya.ml/mirre-mt/pcmt/handlers"
modtmpl "git.dotya.ml/mirre-mt/pcmt/modules/template"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func (a *App) SetupRoutes() error {
e := a.E()
setting := a.setting
assets := http.FileServer(a.getAssets())
tmpls := a.getTemplates()
modtmpl.Init(setting, tmpls)
handlers.SetDBClient(a.db)
// run this before declaring any handler funcs.
handlers.InitHandlers(setting)
e.Renderer = modtmpl.Renderer
compress, err := handlers.WrapMiddlewareCompress()
if err != nil {
return err
}
// keep /static/* as a compatibility fallback for /assets.
e.GET(
"/static/*",
func(c echo.Context) error {
return c.Redirect(http.StatusMovedPermanently, c.Request().URL.Path)
},
middleware.Rewrite(
map[string]string{"/static/*": "/assets/$1"},
),
)
// alternative:
// e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assets)))
e.GET("/assets/*", echo.WrapHandler(http.StripPrefix("/assets/", assets)), handlers.MiddlewareCache, compress)
e.HEAD("/assets/*", echo.WrapHandler(http.StripPrefix("/assets/", assets)), handlers.MiddlewareCache, compress)
e.GET("/healthz", handlers.Healthz())
e.GET("/health", handlers.Healthz())
e.GET("/", handlers.Index(), compress)
e.HEAD("/", handlers.Index(), compress)
e.GET("/signin", handlers.Signin(), compress)
e.POST("/signin", handlers.SigninPost(a.db))
e.GET("/signup", handlers.Signup(), compress)
e.POST("/signup", handlers.SignupPost(a.db))
e.GET("/home", handlers.Home(a.db), compress)
// handle weird attempts here.
e.POST("/signin/*", func(c echo.Context) error {
return c.NoContent(http.StatusNotFound)
})
e.POST("/signup/*", func(c echo.Context) error {
return c.NoContent(http.StatusNotFound)
})
e.GET("/manage/api-keys", handlers.ManageAPIKeys(), handlers.MiddlewareSession)
e.GET("/manage/users", handlers.ManageUsers(), handlers.MiddlewareSession)
e.GET("/manage/users/new", handlers.ManageUsers(), handlers.MiddlewareSession)
e.POST("/manage/users/create", handlers.CreateUser(), handlers.MiddlewareSession)
e.GET("/manage/users/:id", handlers.ViewUser(), handlers.MiddlewareSession)
e.GET("/manage/users/:id/edit", handlers.EditUser(), handlers.MiddlewareSession, handlers.MiddlewareCache, compress)
e.GET("/manage/users/:id/delete", handlers.DeleteUserConfirmation(), handlers.MiddlewareSession)
e.POST("/manage/users/:id/update", handlers.UpdateUser(), handlers.MiddlewareSession)
e.POST("/manage/users/:id/delete", handlers.DeleteUser(), handlers.MiddlewareSession)
e.GET("/logout", handlers.Logout(), compress)
e.POST("/logout", handlers.Logout(), handlers.MiddlewareSession)
// administrative endpoints.
e.GET("/admin/*", handlers.Admin())
return nil
}