2023-05-20 20:15:57 +02:00
|
|
|
// Copyright 2023 wanderer <a_mirre at utb dot cz>
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-03-22 23:03:21 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2023-03-22 23:11:14 +01:00
|
|
|
"git.dotya.ml/mirre-mt/pcmt/handlers"
|
2023-05-11 04:32:39 +02:00
|
|
|
modtmpl "git.dotya.ml/mirre-mt/pcmt/modules/template"
|
2023-03-22 23:03:21 +01:00
|
|
|
"github.com/labstack/echo/v4"
|
2023-04-26 22:44:18 +02:00
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2023-03-22 23:03:21 +01:00
|
|
|
)
|
|
|
|
|
2023-05-31 22:42:50 +02:00
|
|
|
func (a *App) SetupRoutes() error {
|
2023-03-22 23:03:21 +01:00
|
|
|
e := a.E()
|
2023-05-03 02:18:29 +02:00
|
|
|
setting := a.setting
|
2023-04-26 23:11:58 +02:00
|
|
|
assets := http.FileServer(a.getAssets())
|
|
|
|
tmpls := a.getTemplates()
|
2023-03-22 23:03:21 +01:00
|
|
|
|
2023-05-11 04:32:39 +02:00
|
|
|
modtmpl.Init(setting, tmpls)
|
2023-05-30 23:50:37 +02:00
|
|
|
handlers.SetDBClient(a.db)
|
2023-04-13 00:07:08 +02:00
|
|
|
// run this before declaring any handler funcs.
|
2023-05-11 04:32:39 +02:00
|
|
|
handlers.InitHandlers(setting)
|
|
|
|
|
|
|
|
e.Renderer = modtmpl.Renderer
|
2023-04-13 00:07:08 +02:00
|
|
|
|
2023-05-31 22:42:50 +02:00
|
|
|
compress, err := handlers.WrapMiddlewareCompress()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-13 16:44:40 +02:00
|
|
|
xsrf := a.csrfConfig()
|
|
|
|
|
2023-04-26 22:44:18 +02:00
|
|
|
// keep /static/* as a compatibility fallback for /assets.
|
|
|
|
e.GET(
|
|
|
|
"/static/*",
|
2023-04-27 17:43:34 +02:00
|
|
|
func(c echo.Context) error {
|
2023-04-28 19:07:43 +02:00
|
|
|
return c.Redirect(http.StatusMovedPermanently, c.Request().URL.Path)
|
2023-04-27 17:43:34 +02:00
|
|
|
},
|
2023-04-26 22:44:18 +02:00
|
|
|
middleware.Rewrite(
|
|
|
|
map[string]string{"/static/*": "/assets/$1"},
|
|
|
|
),
|
|
|
|
)
|
2023-04-27 17:43:34 +02:00
|
|
|
// alternative:
|
|
|
|
// e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assets)))
|
2023-05-31 22:42:50 +02:00
|
|
|
e.GET("/assets/*", echo.WrapHandler(http.StripPrefix("/assets/", assets)), handlers.MiddlewareCache, compress)
|
|
|
|
e.HEAD("/assets/*", echo.WrapHandler(http.StripPrefix("/assets/", assets)), handlers.MiddlewareCache, compress)
|
2023-05-13 22:33:38 +02:00
|
|
|
|
2023-08-13 16:44:40 +02:00
|
|
|
e.GET("/healthz", handlers.Healthz())
|
|
|
|
e.GET("/health", handlers.Healthz())
|
2023-05-13 22:33:38 +02:00
|
|
|
|
2023-08-15 17:56:25 +02:00
|
|
|
base := e.Group("", xsrf)
|
2023-08-13 15:52:31 +02:00
|
|
|
|
2023-08-15 17:56:25 +02:00
|
|
|
base.GET("/", handlers.Index(), compress)
|
2023-08-13 16:44:40 +02:00
|
|
|
base.HEAD("/", handlers.Index())
|
2023-08-15 17:56:25 +02:00
|
|
|
base.GET("/signin", handlers.Signin(), compress)
|
2023-08-13 15:52:31 +02:00
|
|
|
base.POST("/signin", handlers.SigninPost(a.db))
|
2023-08-16 15:07:10 +02:00
|
|
|
|
|
|
|
if a.setting.RegistrationAllowed {
|
|
|
|
base.GET("/signup", handlers.Signup(), compress)
|
|
|
|
base.POST("/signup", handlers.SignupPost(a.db))
|
|
|
|
}
|
2023-08-15 17:56:25 +02:00
|
|
|
|
2023-08-13 16:44:40 +02:00
|
|
|
base.GET("/home", handlers.Home(a.db))
|
2023-05-22 03:22:58 +02:00
|
|
|
|
2023-08-12 14:50:49 +02:00
|
|
|
// handle weird attempts here.
|
2023-08-15 17:56:25 +02:00
|
|
|
base.POST("/signin/*", func(c echo.Context) error {
|
2023-08-12 14:50:49 +02:00
|
|
|
return c.NoContent(http.StatusNotFound)
|
|
|
|
})
|
2023-08-15 17:56:25 +02:00
|
|
|
base.POST("/signup/*", func(c echo.Context) error {
|
2023-08-12 14:50:49 +02:00
|
|
|
return c.NoContent(http.StatusNotFound)
|
|
|
|
})
|
|
|
|
|
2023-08-24 18:43:24 +02:00
|
|
|
user := e.Group("/user", handlers.MiddlewareSession, xsrf)
|
|
|
|
|
|
|
|
user.GET("/initial-password-change", handlers.InitialPasswordChange())
|
|
|
|
user.POST("/initial-password-change", handlers.InitialPasswordChange())
|
|
|
|
user.GET("/hibp-search", handlers.GetSearchHIBP())
|
|
|
|
user.POST("/hibp-search", handlers.SearchHIBP())
|
|
|
|
user.GET("/hibp-breach-details/:name", handlers.ViewHIBP())
|
|
|
|
|
2023-08-15 17:56:25 +02:00
|
|
|
manage := e.Group("/manage", handlers.MiddlewareSession, xsrf)
|
2023-08-13 15:52:31 +02:00
|
|
|
|
2023-08-15 17:56:25 +02:00
|
|
|
manage.GET("/api-keys", handlers.ManageAPIKeys(), compress)
|
|
|
|
manage.GET("/users", handlers.ManageUsers(), compress)
|
|
|
|
manage.GET("/users/new", handlers.ManageUsers(), compress)
|
|
|
|
manage.POST("/users/create", handlers.CreateUser())
|
|
|
|
manage.GET("/users/:id", handlers.ViewUser(), compress)
|
|
|
|
manage.GET("/users/:id/edit", handlers.EditUser(), handlers.MiddlewareCache, compress)
|
|
|
|
manage.GET("/users/:id/delete", handlers.DeleteUserConfirmation(), compress)
|
|
|
|
manage.POST("/users/:id/update", handlers.UpdateUser())
|
|
|
|
manage.POST("/users/:id/delete", handlers.DeleteUser())
|
2023-05-22 03:22:58 +02:00
|
|
|
|
2023-08-15 17:56:25 +02:00
|
|
|
e.GET("/logout", handlers.Logout())
|
2023-08-13 16:44:40 +02:00
|
|
|
e.POST("/logout", handlers.Logout(), handlers.MiddlewareSession, xsrf)
|
2023-03-22 23:11:14 +01:00
|
|
|
|
|
|
|
// administrative endpoints.
|
2023-04-13 00:07:08 +02:00
|
|
|
e.GET("/admin/*", handlers.Admin())
|
2023-05-31 22:42:50 +02:00
|
|
|
|
|
|
|
return nil
|
2023-03-22 23:03:21 +01:00
|
|
|
}
|