tijl.dev-core/internal/service/main.go

102 lines
2.6 KiB
Go
Raw Permalink Normal View History

2024-08-22 00:44:48 +02:00
package service
import (
"context"
2024-08-22 13:47:16 +02:00
"fmt"
2024-09-07 17:18:20 +02:00
"html/template"
2024-08-31 17:42:51 +02:00
"math"
2024-08-22 00:44:48 +02:00
"net/http"
2024-08-31 17:42:51 +02:00
"time"
2024-08-22 00:44:48 +02:00
2024-08-25 17:43:55 +02:00
"git.tijl.dev/tijl/tijl.dev-core/internal/apps/flags"
2024-09-08 01:01:29 +02:00
"git.tijl.dev/tijl/tijl.dev-core/internal/apps/inflation"
2024-08-29 12:21:38 +02:00
"git.tijl.dev/tijl/tijl.dev-core/internal/apps/uploader"
2024-08-22 15:15:16 +02:00
"git.tijl.dev/tijl/tijl.dev-core/internal/assets"
"git.tijl.dev/tijl/tijl.dev-core/internal/config"
"git.tijl.dev/tijl/tijl.dev-core/internal/handlers"
"git.tijl.dev/tijl/tijl.dev-core/internal/oidc"
webinternal "git.tijl.dev/tijl/tijl.dev-core/internal/web"
2024-08-24 17:48:14 +02:00
"git.tijl.dev/tijl/tijl.dev-core/locales"
2024-08-22 15:15:16 +02:00
"git.tijl.dev/tijl/tijl.dev-core/modules/db"
2024-08-24 17:48:14 +02:00
"git.tijl.dev/tijl/tijl.dev-core/modules/i18n"
2024-08-22 15:15:16 +02:00
"git.tijl.dev/tijl/tijl.dev-core/modules/logger"
"git.tijl.dev/tijl/tijl.dev-core/modules/web"
webf "git.tijl.dev/tijl/tijl.dev-core/web"
2024-08-22 00:44:48 +02:00
"github.com/gofiber/contrib/fiberzerolog"
"github.com/gofiber/fiber/v2"
2024-09-01 13:15:56 +02:00
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/helmet"
2024-08-22 00:44:48 +02:00
"github.com/gofiber/template/html/v2"
)
func Listen() {
// Load initial context
ctx := context.Background()
// setup logger
log.Load()
// Load config
config.Load()
// Load database
db.Load()
// Setup oidc
oidc.Load(ctx)
// Load assets
assets.Load()
// Load translations
2024-08-24 17:48:14 +02:00
i18n.RegisterTranslations(locales.Embed, ".")
2024-08-22 00:44:48 +02:00
// setup handler
handlers.Setup()
2024-08-25 17:43:55 +02:00
// setup apps
flags.Setup()
2024-08-29 12:21:38 +02:00
uploader.Setup()
2024-09-08 01:01:29 +02:00
inflation.Setup()
2024-08-25 17:43:55 +02:00
2024-08-22 00:44:48 +02:00
// setup web
webinternal.Load()
2024-08-22 20:28:21 +02:00
log.Info().Msg("started internal services")
2024-08-22 00:44:48 +02:00
// Init templating engine
2024-08-22 12:57:38 +02:00
engine := html.NewFileSystem(http.FS(webf.ViewsEmbed), ".html")
engine.Directory = "views"
2024-08-22 00:44:48 +02:00
engine.AddFunc("icon", assets.Svg)
2024-08-31 17:42:51 +02:00
engine.AddFunc("incfloat", func(a, b float64) float64 {
return math.Round((a+b)*1e8) / 1e8
})
engine.AddFunc("formatdate", func(t time.Time) string {
return t.Format("2 Jan 2006")
})
2024-09-07 17:18:20 +02:00
engine.AddFunc("safe", func(s string) template.HTML {
return template.HTML(s)
})
engine.AddFunc("safeurl", func(s string) template.URL {
return template.URL(s)
})
2024-08-22 00:44:48 +02:00
// Init fiber
app := fiber.New(fiber.Config{
Views: engine,
DisableStartupMessage: true,
2024-09-04 23:03:23 +02:00
BodyLimit: 4 * 1024 * 1024 * 1024,
2024-08-22 00:44:48 +02:00
})
app.Use(fiberzerolog.New(fiberzerolog.Config{
Logger: &log.Logger,
}))
2024-09-01 13:15:56 +02:00
app.Use(compress.New(compress.Config{
Level: compress.LevelBestSpeed,
}))
app.Use(helmet.New(helmet.Config{}))
2024-08-22 00:44:48 +02:00
// Setup routes
web.Setup(app)
// Listen web server
2024-08-22 18:04:08 +02:00
log.Info().Int("port", config.Config.Port).Str("host", config.Config.Host).Msg("listening")
2024-08-22 13:47:16 +02:00
if err := app.Listen(fmt.Sprintf("%v:%v", config.Config.Host, config.Config.Port)); err != nil {
2024-08-22 00:44:48 +02:00
log.Fatal().Err(err).Msg("Fiber app error")
}
}