ytdl-web/cmd/serve.go

69 lines
1.8 KiB
Go
Raw Normal View History

/*
Copyright © 2023 Evan Fiordeliso <evan.fiordeliso@gmail.com>
*/
package cmd
import (
"github.com/dgraph-io/badger/v2"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.fifitido.net/ytdl-web/app/controllers"
"go.fifitido.net/ytdl-web/pkg/server"
"go.fifitido.net/ytdl-web/pkg/utils"
"go.fifitido.net/ytdl-web/pkg/ytdl"
"go.fifitido.net/ytdl-web/pkg/ytdl/cache"
"golang.org/x/exp/slog"
)
var (
serveCmd = &cobra.Command{
Use: "serve",
Short: "Serve the ytdl-web application",
Long: `Serve the ytdl-web application`,
RunE: func(cmd *cobra.Command, args []string) error {
initLogging()
logger := slog.Default()
db, err := badger.Open(
badger.
DefaultOptions(cfg.Cache.DirPath).
WithLogger(utils.NewBadgerLogger(logger.With("module", "badger"))),
)
if err != nil {
return err
}
defer db.Close()
cache := cache.NewDefaultMetadataCache(db)
ytdl := ytdl.NewYtdl(cfg, slog.Default(), cache)
s := server.New(
server.
DefaultOptions().
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()
},
}
)
func init() {
rootCmd.AddCommand(serveCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// completionCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// completionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}