ytdl-web/app/serve.go

36 lines
853 B
Go

package app
import (
"fmt"
"net/http"
"github.com/dgraph-io/badger/v2"
"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 {
logger := slog.Default()
db, err := badger.Open(
badger.
DefaultOptions(cfg.Cache.DirPath).
WithLogger(utils.NewBadgerLogger(slog.With("module", "badger"))),
)
if err != nil {
return err
}
defer db.Close()
cache := cache.NewDefaultMetadataCache(db)
ytdl := ytdl.NewYtdl(cfg, slog.Default(), cache)
router := Router(ytdl)
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 http.ListenAndServe(listenAddr, router)
}