From 500049031a2ea9bb00e4b3030856239657d575ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Bouzour=C3=A8ne?= Date: Sun, 26 Jan 2025 14:44:10 +0100 Subject: [PATCH] Add comments in main --- main.go | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index e8a663c..6fc3ae0 100644 --- a/main.go +++ b/main.go @@ -28,11 +28,15 @@ var embedViews embed.FS func main() { log := helpers.GetLogger() + // Fetch app config config, err := helpers.GetConfig() if err != nil { 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() if err != nil { 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) - // 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 if config.DevMode { engine = django.New("./views", ".html") @@ -59,26 +66,31 @@ func main() { log.Fatal(err) } - engine = django.NewFileSystem( - http.FS(embedViews2), - ".html", - ) + engine = django.NewFileSystem(http.FS(embedViews2), ".html") } + // Register any custom functions or filters in the template engine 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{ Views: engine, 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 { fiberConfig.ProxyHeader = fiber.HeaderXForwardedFor } + // Create the Fiber web app app := fiber.New(fiberConfig) + // If we are in dev mode, serve assets from directory + // Otherwise, serve assets embedded in binary if config.DevMode { app.Static("/static", "./static") } else { @@ -213,6 +225,7 @@ func main() { app.Post("/admin/roles/:id/edit", controllers.RoleEdit) app.Post("/admin/roles/:id/delete", controllers.RoleDelete) + // Endpoints only enabled when debug mode is enabled if config.Debug { app.Get("/debug/fake-members/:number", controllers.DebugFakeMembers) app.Get("/debug/fake-contacts/:number", controllers.DebugFakeContacts) @@ -230,6 +243,7 @@ func main() { config.App.ListenPort, ) + // Start the fiber web app err = app.Listen(listenAddr) if err != nil { log.Fatal(err)