ytdl-web/cmd/root.go

127 lines
3.5 KiB
Go
Raw Normal View History

2023-04-14 11:58:32 -04:00
/*
Copyright © 2023 Evan Fiordeliso <evan.fiordeliso@gmail.com>
2023-04-14 11:58:32 -04:00
*/
package cmd
import (
"os"
2023-08-14 18:14:08 -04:00
"github.com/dgraph-io/badger/v2"
2023-04-14 11:58:32 -04:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
2023-08-14 18:14:08 -04:00
"go.fifitido.net/ytdl-web/app/controllers"
2023-05-23 18:44:05 -04:00
"go.fifitido.net/ytdl-web/config"
2023-08-14 18:14:08 -04:00
"go.fifitido.net/ytdl-web/pkg/server"
"go.fifitido.net/ytdl-web/utils"
"go.fifitido.net/ytdl-web/ytdl"
"go.fifitido.net/ytdl-web/ytdl/cache"
"golang.org/x/exp/slog"
2023-04-14 11:58:32 -04:00
)
var (
cfgFile string
2023-05-23 18:44:05 -04:00
cfg *config.Config
2023-04-14 11:58:32 -04:00
rootCmd = &cobra.Command{
Use: "ytdl-web",
Short: "A web frontend for yt-dlp",
Long: `YTDL Web
2023-04-14 11:58:32 -04:00
A web application that grabs the links to videos from over a
thousand websites using the yt-dlp project under the hood.`,
2023-08-14 18:14:08 -04:00
RunE: func(cmd *cobra.Command, args []string) error {
logger := slog.Default()
db, err := badger.Open(
badger.
DefaultOptions(cfg.Cache.DirPath).
WithLogger(utils.NewBadgerLogger(logger.With("module", "badger"))),
)
if err != nil {
return err
}
2023-08-14 18:14:08 -04:00
defer db.Close()
cache := cache.NewDefaultMetadataCache(db)
ytdl := ytdl.NewYtdl(cfg, slog.Default(), cache)
s := server.New(
server.DefaultServerOptions().
WithListenAddr(viper.GetString("http.listen")).
WithListenPort(viper.GetInt("http.port")).
WithLogger(logger.With("module", "server")),
)
s.MountController("/", controllers.NewHomeController(ytdl))
s.MountController("/download", controllers.NewDownloadController(ytdl))
return s.ListenAndServe()
},
}
)
2023-04-14 11:58:32 -04:00
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $XDG_CONFIG_HOME/ytdl-web/config.yml)")
2023-04-14 11:58:32 -04:00
2023-08-14 18:14:08 -04:00
rootCmd.PersistentFlags().IntP("port", "p", 8080, "port to listen on")
2023-05-23 21:52:00 -04:00
rootCmd.PersistentFlags().StringP("listen", "l", "", "address to listen on")
rootCmd.PersistentFlags().StringP("base-path", "b", "", "the base path, used when behind reverse proxy")
2023-05-23 21:52:00 -04:00
rootCmd.PersistentFlags().StringP("ytdlp-path", "y", "", "the path to the yt-dlp binary, used when it is not in $PATH")
2023-05-23 18:44:05 -04:00
rootCmd.PersistentFlags().BoolP("cookies-enabled", "C", false, "whether cookies are enabled")
rootCmd.PersistentFlags().StringP("cookies", "c", "", "the path to the cookies file")
2023-04-14 11:58:32 -04:00
// trunk-ignore-begin(golangci-lint/errcheck): Ignoring errors
2023-05-23 18:44:05 -04:00
viper.BindPFlag("http.port", rootCmd.PersistentFlags().Lookup("port"))
viper.BindPFlag("http.listen", rootCmd.PersistentFlags().Lookup("listen"))
viper.BindPFlag("http.basePath", rootCmd.PersistentFlags().Lookup("base-path"))
viper.BindPFlag("ytdlp.binaryPath", rootCmd.PersistentFlags().Lookup("ytdlp-path"))
viper.BindPFlag("cookies.enabled", rootCmd.PersistentFlags().Lookup("cookies-enabled"))
viper.BindPFlag("cookies.filePath", rootCmd.PersistentFlags().Lookup("cookies"))
// trunk-ignore-end(golangci-lint/errcheck)
2023-04-14 11:58:32 -04:00
}
func initConfig() {
2023-05-23 18:44:05 -04:00
var err error
if cfgFile != "" {
2023-05-23 18:44:05 -04:00
cfg, err = config.LoadConfig(cfgFile)
} else {
2023-05-23 18:44:05 -04:00
cfg, err = config.LoadConfig()
}
if err != nil {
slog.Error("Error loading configuration", slog.String("error", err.Error()))
os.Exit(1)
}
2023-05-23 18:44:05 -04:00
initLogging()
2023-04-14 11:58:32 -04:00
2023-05-23 18:44:05 -04:00
slog.Info("Configuration loaded")
}
func initLogging() {
var handler slog.Handler
if cfg.IsProduction() {
handler = slog.HandlerOptions{
AddSource: true,
2023-08-12 23:44:21 -04:00
Level: slog.LevelInfo,
2023-05-23 18:44:05 -04:00
}.NewJSONHandler(os.Stdout)
} else {
handler = slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
}.NewTextHandler(os.Stdout)
}
2023-05-23 18:44:05 -04:00
slog.SetDefault(slog.New(handler))
}