/*
* Copyright (C) 2024 Jonni Liljamo <jonni@liljamo.com>
*
* This file is licensed under GPL-3.0-or-later, see NOTICE and LICENSE for
* more information.
*/
// Package router provides functions for setting up the router.
package router
import (
"encoding/gob"
"log/slog"
"git.src.quest/~liljamo/emerwen-web/internal/auth"
"git.src.quest/~liljamo/emerwen-web/internal/client"
"git.src.quest/~liljamo/emerwen-web/internal/handlers"
"git.src.quest/~liljamo/emerwen-web/internal/middlewares"
"git.src.quest/~liljamo/emerwen-web/internal/renderer"
"github.com/alexedwards/scs/v2"
"github.com/gin-gonic/gin"
sloggin "github.com/samber/slog-gin"
)
// SetupRouter returns a gin engine.
func SetupRouter(l *slog.Logger, a *auth.Auth, sm *scs.SessionManager, client *client.Client) *gin.Engine {
r := gin.New()
r.Use(gin.Recovery())
r.Use(sloggin.New(l))
r.Static("/static", "./static")
r.HTMLRender = &renderer.TemplRender{}
gob.Register(auth.Claims{})
r.Use(middlewares.CheckForSession(sm))
r.NoRoute(handlers.NoRoute())
r.GET("/", handlers.Index())
r.GET("/login", handlers.Login(a, sm))
r.GET("/logout", handlers.Logout(sm))
r.GET("/oauth2/oidc/callback", handlers.OAuth2OIDCCallback(a, sm))
s := r.Group("/", middlewares.RequireSession(sm))
{
s.GET("/partials/workers", handlers.PartialWorkers(client))
a := r.Group("/api")
{
a.POST("/worker", handlers.APIPostWorker(client))
a.PATCH("/worker", handlers.APIPatchWorker(client))
a.DELETE("/worker", handlers.APIDeleteWorker(client))
a.POST("/target", handlers.APIPostTarget(client))
a.PATCH("/target", handlers.APIPatchTarget(client))
a.DELETE("/target", handlers.APIDeleteTarget(client))
}
}
return r
}