Add comments in main

This commit is contained in:
William Bouzourène 2025-01-26 14:44:10 +01:00
parent e915d62afa
commit ca545bd5d6
Signed by: bouzoure
SSH key fingerprint: SHA256:19MbXpLua4rUtk8tunMesD8KUKb91LXLHg8E/qTooww

28
main.go
View file

@ -28,11 +28,15 @@ var embedViews embed.FS
func main() { func main() {
log := helpers.GetLogger() log := helpers.GetLogger()
// Fetch app config
config, err := helpers.GetConfig() config, err := helpers.GetConfig()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
// Check if at least one account exists
// If not, create the default one: admin@invalid.tld / password
// Note: since this is the fist call to the DB, this also applies the migrations
accountCheck, err := helpers.FirstAccountCheck() accountCheck, err := helpers.FirstAccountCheck()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -45,10 +49,13 @@ func main() {
} }
} }
// RegisterJobs // Register background jobs
// Note: jobs will be executed at launch and after every interval
go helpers.RegisterJob(60*time.Minute, "clean saved sessions", jobs.CleanSavedSessions) go helpers.RegisterJob(60*time.Minute, "clean saved sessions", jobs.CleanSavedSessions)
// Create a new engine // Initialize the Pongo2 templating engine
// If we are in dev mode, load templates from directory
// Otherwise, use the templates embedded in the binary
var engine *django.Engine var engine *django.Engine
if config.DevMode { if config.DevMode {
engine = django.New("./views", ".html") engine = django.New("./views", ".html")
@ -59,26 +66,31 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
engine = django.NewFileSystem( engine = django.NewFileSystem(http.FS(embedViews2), ".html")
http.FS(embedViews2),
".html",
)
} }
// Register any custom functions or filters in the template engine
pongo2.RegisterFilter("time_diff", helpers.TemplTimeDiff) pongo2.RegisterFilter("time_diff", helpers.TemplTimeDiff)
// Base config for the Fiber web app
// We pass our newly created template engine
// as well as our custom error handler
fiberConfig := fiber.Config{ fiberConfig := fiber.Config{
Views: engine, Views: engine,
ErrorHandler: helpers.FiberErrorHandler, ErrorHandler: helpers.FiberErrorHandler,
} }
// If app behind proxy, use headers for IP // If web app is behind a reverse proxy,
// use X-Forwarded-For header as the IP header
if config.App.BehindProxy { if config.App.BehindProxy {
fiberConfig.ProxyHeader = fiber.HeaderXForwardedFor fiberConfig.ProxyHeader = fiber.HeaderXForwardedFor
} }
// Create the Fiber web app
app := fiber.New(fiberConfig) app := fiber.New(fiberConfig)
// If we are in dev mode, serve assets from directory
// Otherwise, serve assets embedded in binary
if config.DevMode { if config.DevMode {
app.Static("/static", "./static") app.Static("/static", "./static")
} else { } else {
@ -213,6 +225,7 @@ func main() {
app.Post("/admin/roles/:id<int;min(0)>/edit", controllers.RoleEdit) app.Post("/admin/roles/:id<int;min(0)>/edit", controllers.RoleEdit)
app.Post("/admin/roles/:id<int;min(0)>/delete", controllers.RoleDelete) app.Post("/admin/roles/:id<int;min(0)>/delete", controllers.RoleDelete)
// Endpoints only enabled when debug mode is enabled
if config.Debug { if config.Debug {
app.Get("/debug/fake-members/:number<int;min(1)>", controllers.DebugFakeMembers) app.Get("/debug/fake-members/:number<int;min(1)>", controllers.DebugFakeMembers)
app.Get("/debug/fake-contacts/:number<int;min(1)>", controllers.DebugFakeContacts) app.Get("/debug/fake-contacts/:number<int;min(1)>", controllers.DebugFakeContacts)
@ -230,6 +243,7 @@ func main() {
config.App.ListenPort, config.App.ListenPort,
) )
// Start the fiber web app
err = app.Listen(listenAddr) err = app.Listen(listenAddr)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)