DEVELOPMENT ENVIRONMENT

~liljamo/emerwen-web

ref: a20635584f23b381e2f025219d48ef23f2f13324 emerwen-web/internal/router/router.go -rw-r--r-- 1.1 KiB
a2063558Jonni Liljamo chore: update flake inputs 4 days 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
/*
 * 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/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) *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))

	return r
}