DEVELOPMENT ENVIRONMENT

~liljamo/emerwen-web

ref: 4e555b1e3309881de4969794eff19e5275430069 emerwen-web/internal/router/router.go -rw-r--r-- 1.7 KiB
4e555b1eJonni Liljamo feat: UI for new/patch/delete worker/target 18 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * 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
}