DEVELOPMENT ENVIRONMENT

~liljamo/felu

ref: 0ea20b4e37252798761c5d4442dc1e0641c86673 felu/internal/routers/frontend.go -rw-r--r-- 2.1 KiB
0ea20b4eJonni Liljamo feat: apikey refreshing and eye toggle 1 year, 10 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * Copyright (C) 2023 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"
)

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") // TODO:
		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
}