51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package web
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/dgraph-io/badger/v2"
|
|
"github.com/gofiber/fiber/v2"
|
|
slogfiber "github.com/samber/slog-fiber"
|
|
"go.fifitido.net/ytdl-web/config"
|
|
"go.fifitido.net/ytdl-web/utils"
|
|
"go.fifitido.net/ytdl-web/ytdl"
|
|
"go.fifitido.net/ytdl-web/ytdl/cache"
|
|
"golang.org/x/exp/slog"
|
|
)
|
|
|
|
func Serve(cfg *config.Config) error {
|
|
engine := ViewsEngine()
|
|
app := fiber.New(fiber.Config{
|
|
Views: engine,
|
|
EnableTrustedProxyCheck: true,
|
|
TrustedProxies: cfg.HTTP.TrustedProxies,
|
|
DisableStartupMessage: true,
|
|
})
|
|
|
|
logger := slog.With("module", "web")
|
|
app.Use(slogfiber.New(logger))
|
|
|
|
db, err := badger.Open(
|
|
badger.
|
|
DefaultOptions("/tmp/badger").
|
|
WithLogger(utils.NewBadgerLogger(slog.With("module", "badger"))),
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer db.Close()
|
|
|
|
cache := cache.NewDefaultMetadataCache(db)
|
|
|
|
routes := &routes{
|
|
ytdl: ytdl.NewYtdl(cfg, slog.Default(), cache),
|
|
}
|
|
routes.Register(app)
|
|
|
|
listenAddr := fmt.Sprintf("%s:%d", cfg.HTTP.Listen, cfg.HTTP.Port)
|
|
|
|
logger.Info("Starting HTTP server", slog.String("host", cfg.HTTP.Listen), slog.Int("port", cfg.HTTP.Port))
|
|
|
|
return app.Listen(listenAddr)
|
|
}
|