tijl.dev-core/internal/service/main.go
tijl 1d072c6cc6
Some checks failed
build / build (push) Successful in 28s
release-tag / release-image (push) Has been cancelled
general page updates
2024-08-31 17:42:51 +02:00

86 lines
2.1 KiB
Go

package service
import (
"context"
"fmt"
"math"
"net/http"
"time"
"git.tijl.dev/tijl/tijl.dev-core/internal/apps/flags"
"git.tijl.dev/tijl/tijl.dev-core/internal/apps/uploader"
"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"
"git.tijl.dev/tijl/tijl.dev-core/locales"
"git.tijl.dev/tijl/tijl.dev-core/modules/db"
"git.tijl.dev/tijl/tijl.dev-core/modules/i18n"
"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"
"github.com/gofiber/contrib/fiberzerolog"
"github.com/gofiber/fiber/v2"
"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
i18n.RegisterTranslations(locales.Embed, ".")
// setup handler
handlers.Setup()
// setup apps
flags.Setup()
uploader.Setup()
// setup web
webinternal.Load()
log.Info().Msg("started internal services")
// Init templating engine
engine := html.NewFileSystem(http.FS(webf.ViewsEmbed), ".html")
engine.Directory = "views"
engine.AddFunc("icon", assets.Svg)
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")
})
// Init fiber
app := fiber.New(fiber.Config{
Views: engine,
DisableStartupMessage: true,
})
app.Use(fiberzerolog.New(fiberzerolog.Config{
Logger: &log.Logger,
}))
// Setup routes
web.Setup(app)
// Listen web server
log.Info().Int("port", config.Config.Port).Str("host", config.Config.Host).Msg("listening")
if err := app.Listen(fmt.Sprintf("%v:%v", config.Config.Host, config.Config.Port)); err != nil {
log.Fatal().Err(err).Msg("Fiber app error")
}
}