2023-08-12 23:27:11 -04:00
|
|
|
package app
|
2023-04-14 11:58:32 -04:00
|
|
|
|
|
|
|
import (
|
2023-04-14 17:14:27 -04:00
|
|
|
"fmt"
|
2023-08-12 23:27:11 -04:00
|
|
|
"net/http"
|
2023-04-14 16:07:57 -04:00
|
|
|
|
2023-05-23 19:07:44 -04:00
|
|
|
"github.com/dgraph-io/badger/v2"
|
2023-05-23 18:44:05 -04:00
|
|
|
"go.fifitido.net/ytdl-web/config"
|
2023-05-23 19:07:44 -04:00
|
|
|
"go.fifitido.net/ytdl-web/utils"
|
|
|
|
"go.fifitido.net/ytdl-web/ytdl"
|
|
|
|
"go.fifitido.net/ytdl-web/ytdl/cache"
|
2023-05-23 18:44:05 -04:00
|
|
|
"golang.org/x/exp/slog"
|
2023-04-14 11:58:32 -04:00
|
|
|
)
|
|
|
|
|
2023-05-23 18:44:05 -04:00
|
|
|
func Serve(cfg *config.Config) error {
|
2023-08-12 23:27:11 -04:00
|
|
|
logger := slog.Default()
|
2023-05-23 18:49:58 -04:00
|
|
|
|
2023-05-23 19:07:44 -04:00
|
|
|
db, err := badger.Open(
|
|
|
|
badger.
|
2023-05-23 20:07:48 -04:00
|
|
|
DefaultOptions(cfg.Cache.DirPath).
|
2023-05-23 19:07:44 -04:00
|
|
|
WithLogger(utils.NewBadgerLogger(slog.With("module", "badger"))),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
cache := cache.NewDefaultMetadataCache(db)
|
2023-08-12 23:27:11 -04:00
|
|
|
ytdl := ytdl.NewYtdl(cfg, slog.Default(), cache)
|
|
|
|
router := Router(ytdl)
|
2023-04-14 11:58:32 -04:00
|
|
|
|
2023-05-23 18:44:05 -04:00
|
|
|
listenAddr := fmt.Sprintf("%s:%d", cfg.HTTP.Listen, cfg.HTTP.Port)
|
2023-05-23 18:49:58 -04:00
|
|
|
logger.Info("Starting HTTP server", slog.String("host", cfg.HTTP.Listen), slog.Int("port", cfg.HTTP.Port))
|
2023-08-12 23:27:11 -04:00
|
|
|
return http.ListenAndServe(listenAddr, router)
|
2023-04-14 11:58:32 -04:00
|
|
|
}
|