From 2e251e4593919967d30ab5d91eb6a5f5a16f0f01 Mon Sep 17 00:00:00 2001 From: Evan Fiordeliso Date: Tue, 23 May 2023 20:07:48 -0400 Subject: [PATCH] Add cache dir config and add some config env vars to helm chart --- charts/ytdl-web/values.yaml | 68 ++----------------------------------- config.example.yaml | 5 ++- config/config.go | 30 +++++++--------- web/serve.go | 2 +- ytdl/ytdl.go | 6 ++-- 5 files changed, 21 insertions(+), 90 deletions(-) diff --git a/charts/ytdl-web/values.yaml b/charts/ytdl-web/values.yaml index 336fdf9..1ccbf2f 100644 --- a/charts/ytdl-web/values.yaml +++ b/charts/ytdl-web/values.yaml @@ -10,6 +10,7 @@ env: YTDL_HTTP_LISTEN: 0.0.0.0 YTDL_HTTP_PORT: 80 # YTDL_HTTP_BASEPATH: /example + YTDL_COOKIES_FILEPATH: /etc/ytdl-web/cookies.txt service: main: @@ -17,75 +18,10 @@ service: http: port: 80 -secrets: - cookies: - enabled: true - data: - cookies.txt: "" - -configMaps: - config: - enabled: true - data: - config.yaml: |- - # The server environment - # For dev environments use Development - # For prod environments use Production - # For staging envronments use Staging - env: Production - http: - # The port to listen on - port: 8080 - # The address to listen on - # For local only access use 127.0.0.1 - # For public access use 0.0.0.0 - listen: 0.0.0.0 - # The base path of the application, useful for reverse proxies - basePath: "" - - # A list of proxy servers to trust for security purposes - # Only needed when accessing app behind a proxy - trustedProxies: [] - ytdlp: - # The path to the yt-dlp binary, if it is already in your $PATH just yt-dlp will work. - binaryPath: yt-dlp - cookies: - # Whether to use cookies when fetching the video metadata - enabled: false - - # The path to the netscape formatted cookies file - # See: https://www.reddit.com/r/youtubedl/wiki/cookies/ for details. - filePath: ~/.cookies - - # Settings for using cookies from a browser's cookies store - fromBrowser: - # The name of the browser to load cookies from. - # Currently supported browsers are: brave, chrome, chromium, edge, firefox, opera, safari, vivaldi. - browser: firefox - - # The keyring used for decrypting Chromium cookies on Linux - # Currently supported keyrings are: basictext, gnomekeyring, kwallet - keyring: basictext - - # The profile to load cookies from (Firefox) - profile: default - - # The container to load cookies from (Firefox) - container: none - persistence: config: enabled: false - mountPath: /etc/ytdl-web/config.yaml - subPath: config.yaml - type: configMap - name: "{{.Release.Name}}-config" - cookies: - enabled: false - mountPath: /etc/ytdl-web/cookies.txt - subPath: cookies.txt - type: secret - name: "{{.Release.Name}}-cookies" + mountPath: /etc/ytdl-web ingress: main: diff --git a/config.example.yaml b/config.example.yaml index 1546a65..8db7d56 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -3,6 +3,8 @@ # For prod environments use Production # For staging envronments use Staging env: Production +# The path to the yt-dlp binary, if it is already in your $PATH just yt-dlp will work. +binaryPath: yt-dlp http: # The port to listen on port: 8080 @@ -16,9 +18,6 @@ http: # A list of proxy servers to trust for security purposes # Only needed when accessing app behind a proxy trustedProxies: [] -ytdlp: - # The path to the yt-dlp binary, if it is already in your $PATH just yt-dlp will work. - binaryPath: yt-dlp cookies: # Whether to use cookies when fetching the video metadata enabled: false diff --git a/config/config.go b/config/config.go index 0098ab7..a3e1506 100644 --- a/config/config.go +++ b/config/config.go @@ -7,10 +7,11 @@ import ( ) type Config struct { - Env string `mapstructure:"env"` - HTTP ConfigHTTP `mapstructure:"http"` - Ytdlp ConfigYtdlp `mapstructure:"ytdlp"` - Cookies ConfigCookies `mapstructure:"cookies"` + Env string `mapstructure:"env"` + BinaryPath string `mapstructure:"binaryPath"` + Cache ConfigCache `mapstructure:"cache"` + HTTP ConfigHTTP `mapstructure:"http"` + Cookies ConfigCookies `mapstructure:"cookies"` } func (c *Config) IsProduction() bool { @@ -32,13 +33,9 @@ type ConfigHTTP struct { TrustedProxies []string `mapstructure:"trustedProxies"` } -type ConfigYtdlp struct { - BinaryPath string `mapstructure:"binaryPath"` - Cache ConfigYtdlpCache `mapstructure:"cache"` -} - -type ConfigYtdlpCache struct { - TTL time.Duration `mapstructure:"ttl"` +type ConfigCache struct { + TTL time.Duration `mapstructure:"ttl"` + DirPath string `mapstructure:"dirPath"` } type ConfigCookies struct { @@ -61,15 +58,14 @@ func DefaultConfig() *Config { Listen: "127.0.0.1", BasePath: "/", }, - Ytdlp: ConfigYtdlp{ - BinaryPath: "yt-dlp", - Cache: ConfigYtdlpCache{ - TTL: time.Hour, - }, + BinaryPath: "yt-dlp", + Cache: ConfigCache{ + TTL: time.Hour, + DirPath: "/tmp/ytdl-web", }, Cookies: ConfigCookies{ Enabled: false, - FilePath: "/tmp/ytdl-web.cookies", + FilePath: "./cookies.txt", FromBrowser: nil, }, } diff --git a/web/serve.go b/web/serve.go index cc9d0cb..0b2b3c7 100644 --- a/web/serve.go +++ b/web/serve.go @@ -27,7 +27,7 @@ func Serve(cfg *config.Config) error { db, err := badger.Open( badger. - DefaultOptions("/tmp/badger"). + DefaultOptions(cfg.Cache.DirPath). WithLogger(utils.NewBadgerLogger(slog.With("module", "badger"))), ) if err != nil { diff --git a/ytdl/ytdl.go b/ytdl/ytdl.go index 1404304..72c2c62 100644 --- a/ytdl/ytdl.go +++ b/ytdl/ytdl.go @@ -69,12 +69,12 @@ func (y *ytdlImpl) GetMetadata(url string) (*metadata.Metadata, error) { WithDumpJson(meta), ) - if err := Exec(y.cfg.Ytdlp.BinaryPath, url, options...); err != nil { + if err := Exec(y.cfg.BinaryPath, url, options...); err != nil { y.logger.Error("failed to get metadata", slog.String("url", url), slog.String("error", err.Error())) return nil, err } - if err := y.cache.Set(url, meta, y.cfg.Ytdlp.Cache.TTL); err != nil { + if err := y.cache.Set(url, meta, y.cfg.Cache.TTL); err != nil { y.logger.Warn("failed to cache metadata", slog.String("url", url), slog.String("error", err.Error())) } @@ -93,7 +93,7 @@ func (y *ytdlImpl) Download(w io.Writer, url, format string, index int) error { options = append(options, WithPlaylistIndex(index)) } - if err := Exec(y.cfg.Ytdlp.BinaryPath, url, options...); err != nil { + if err := Exec(y.cfg.BinaryPath, url, options...); err != nil { return err }