DEVELOPMENT ENVIRONMENT

~liljamo/felu

ref: 1e596cec12a5c40a210a67fd8106d84ee007b1bc felu/felu.go -rw-r--r-- 4.4 KiB
1e596cecJonni Liljamo feat: show users in a table 1 year, 29 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * 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 main

import (
	"fmt"
	"log"
	"net/http"
	"strconv"
	"time"

	"git.src.quest/~skye/felu-ddns/components"
	"git.src.quest/~skye/felu-ddns/config"
	"git.src.quest/~skye/felu-ddns/db"
	"git.src.quest/~skye/felu-ddns/dns"
	"git.src.quest/~skye/felu-ddns/handlers"
	"git.src.quest/~skye/felu-ddns/middlewares"
	"github.com/alexedwards/scs/v2"
	"github.com/gin-gonic/gin"
	"golang.org/x/sync/errgroup"
)

var version = "notset-builtin"

var (
	g errgroup.Group
	sessionManager *scs.SessionManager
)

func ginLogFormat(param gin.LogFormatterParams, router string) string {
	var statusColor, methodColor, resetColor string
	if param.IsOutputColor() {
		statusColor = param.StatusCodeColor()
		methodColor = param.MethodColor()
		resetColor = param.ResetColor()
	}

	if param.Latency > time.Minute {
		param.Latency = param.Latency.Truncate(time.Second)
	}
	return fmt.Sprintf("[GIN] [felu/%-8s] | %v |%s %3d %s| %13v | %15s |%s %-7s %s %#v\n%s",
		router,
		param.TimeStamp.Format("2006/01/02 - 15:04:05"),
		statusColor, param.StatusCode, resetColor,
		param.Latency,
		param.ClientIP,
		methodColor, param.Method, resetColor,
		param.Path,
		param.ErrorMessage,
	)
}

func setupFrontendRouter() *gin.Engine {
	r := gin.New()
	r.Use(gin.Recovery())
	r.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
		return ginLogFormat(param, "frontend")
	}))
	r.Static("/static", "./static")
	r.HTMLRender = &TemplRender{}

	r.GET("/", func(c *gin.Context) {
		c.HTML(http.StatusOK, "", components.Index())
	})

	r.GET("/login", middlewares.SessionExists(sessionManager), func(c *gin.Context) {
		c.HTML(http.StatusOK, "", components.Login())
	})

	auth := r.Group("/auth")
	{
		auth.POST("/login", func(c *gin.Context) {
			handlers.AuthLogin(sessionManager, c)
		})
	}

	// routes for the actual application frontend
	manage := r.Group("/manage").Use(middlewares.SessionExists(sessionManager))
	{
		manage.GET("/", func(c *gin.Context) {
			c.HTML(http.StatusOK, "", components.Manage())
		})
		manage.GET("/settings", func(c *gin.Context) {
			c.HTML(http.StatusOK, "", components.ManageSettings())
		})
	}
	manageAdmin := r.Group("/manage/admin").Use(
		middlewares.SessionExists(sessionManager),
		middlewares.AdminOnly(),
	)
	{
		manageAdmin.GET("/", handlers.ManageAdmin())
		manageAdmin.GET("/users", handlers.ManageAdminUsers())
		manageAdmin.GET("/domains", handlers.ManageAdminDomains())

		manageAdmin.GET("/partials/users_list", handlers.AdminPartialsUsersList())
	}

	return r
}

func setupBackendRouter() *gin.Engine {
	r := gin.New()
	r.Use(gin.Recovery())
	r.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
		return ginLogFormat(param, "backend")
	}))

	r.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"status": "success",
		})
	})

	r.GET("/version", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"version": version,
		})
	})

	return r
}

func main() {
	log.Print("[felu] Starting up...")

	config.InitConfig()

	if err := db.InitDB(); err != nil {
		log.Fatalf("[felu] Failed to initialize database: '%s'", err)
	}
	defer db.DBConn.Close()
	if err := db.InitAdminUser(); err != nil {
		log.Fatalf("[felu] Failed to initialize admin user: '%s'", err)
	}

	sessionManager = scs.New()
	sessionManager.Lifetime = 6 * time.Hour

	frontend := &http.Server{
		Addr: config.FeluConfig.FrontendBindAddr,
		Handler: sessionManager.LoadAndSave(setupFrontendRouter()),
		ReadTimeout: 5 * time.Second,
		WriteTimeout: 10 * time.Second,
	}

	backend := &http.Server{
		Addr: config.FeluConfig.BackendBindAddr,
		Handler: setupBackendRouter(),
		ReadTimeout: 5 * time.Second,
		WriteTimeout: 10 * time.Second,
	}

	log.Printf("[felu] Serving frontend at '%s'", config.FeluConfig.FrontendBindAddr)
	g.Go(func() error {
		return frontend.ListenAndServe()
	})

	log.Printf("[felu] Serving backend at '%s'", config.FeluConfig.BackendBindAddr)
	g.Go(func() error {
		return backend.ListenAndServe()
	})

	dnsIP := config.FeluConfig.DNSBindIP
	dnsPort := strconv.Itoa(int(config.FeluConfig.DNSBindPort))
	dnsAddr := dnsIP + ":" + dnsPort
	log.Printf("[felu] Serving DNS at '%s'", dnsAddr)
	g.Go(func() error {
		return dns.Run(dnsAddr)
	})

	if err := g.Wait(); err != nil {
		log.Fatalf("[felu] Error while running: %s", err)
	}
}