102 lines
2.6 KiB
Go
102 lines
2.6 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"html/template"
|
|
"math"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.tijl.dev/tijl/tijl.dev-core/internal/apps/flags"
|
|
"git.tijl.dev/tijl/tijl.dev-core/internal/apps/inflation"
|
|
"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/fiber/v2/middleware/compress"
|
|
"github.com/gofiber/fiber/v2/middleware/helmet"
|
|
"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()
|
|
inflation.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")
|
|
})
|
|
engine.AddFunc("safe", func(s string) template.HTML {
|
|
return template.HTML(s)
|
|
})
|
|
engine.AddFunc("safeurl", func(s string) template.URL {
|
|
return template.URL(s)
|
|
})
|
|
|
|
// Init fiber
|
|
app := fiber.New(fiber.Config{
|
|
Views: engine,
|
|
DisableStartupMessage: true,
|
|
BodyLimit: 4 * 1024 * 1024 * 1024,
|
|
})
|
|
app.Use(fiberzerolog.New(fiberzerolog.Config{
|
|
Logger: &log.Logger,
|
|
}))
|
|
app.Use(compress.New(compress.Config{
|
|
Level: compress.LevelBestSpeed,
|
|
}))
|
|
app.Use(helmet.New(helmet.Config{}))
|
|
|
|
// 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")
|
|
}
|
|
|
|
}
|