/*
* Copyright (C) 2024 Jonni Liljamo <jonni@liljamo.com>
*
* This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
* more information.
*/
package routers
import (
"git.src.quest/~skye/felu-ddns/internal/handlers"
"git.src.quest/~skye/felu-ddns/internal/log"
"git.src.quest/~skye/felu-ddns/internal/middlewares"
"git.src.quest/~skye/felu-ddns/internal/renderer"
"github.com/alexedwards/scs/v2"
"github.com/gin-gonic/gin"
)
// SetupFrontendRouter returns the frontend router.
func SetupFrontendRouter(sm *scs.SessionManager) *gin.Engine {
r := gin.New()
r.Use(gin.Recovery())
r.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
return log.GinFormat(param, "frontend")
}))
r.Static("/static", "./static")
r.HTMLRender = &renderer.TemplRender{}
r.GET("/", handlers.Index())
r.GET("/login", middlewares.SessionExists(sm), handlers.Login())
auth := r.Group("/auth")
{
auth.POST("/login", handlers.AuthLogin(sm))
auth.GET("/logout", handlers.AuthLogout(sm))
}
// routes for the actual application frontend
manage := r.Group("/manage", middlewares.SessionExists(sm))
{
manage.GET("/", handlers.Manage())
manage.GET("/user", handlers.ManageUser())
manage.POST("/user/password", handlers.PostUserPassword())
manage.POST("/user/email", handlers.PostUserEmail())
manage.DELETE("/user", handlers.DeleteUser(sm))
manage.POST("/domains", handlers.PostDomain())
manage.PATCH("/domains/:id", handlers.PatchDomain())
manage.DELETE("/domains/:id", handlers.DeleteDomain())
manage.POST("/domains/:id/api_key", handlers.RefreshDomainAPIKey())
manage.GET("/partials/domains", handlers.ManagePartialDomains())
}
manageAdmin := r.Group("/manage/admin",
middlewares.SessionExists(sm), middlewares.AdminOnly())
{
manageAdmin.GET("/", handlers.ManageAdmin())
manageAdmin.GET("/users", handlers.ManageAdminUsers())
manageAdmin.GET("/domains", handlers.ManageAdminDomains())
manageAdmin.POST("/users", handlers.PostUser())
manageAdmin.GET("/partials/users_list", handlers.AdminPartialUsersList())
manageAdmin.GET("/partials/domains_list", handlers.AdminPartialDomainsList())
}
return r
}