Invidious is an alternative front-end to YouTube
Allow providing 12-Factor-style Database URL in config
Andre Borie79e99908def9
diff --git a/config/config.example.yml b/config/config.example.ymlindex e83a7515..e8330705 100644--- a/config/config.example.yml+++ b/config/config.example.yml@@ -6,6 +6,8 @@ db:host: localhostport: 5432dbname: invidious+# alternatively, the database URL can be provided directly - if both are set then the latter takes precedence+# database_url: postgres://kemal:kemal@localhost:5432/invidiousfull_refresh: falsehttps_only: falsedomain:diff --git a/src/invidious.cr b/src/invidious.crindex 12145d8b..a0702820 100644--- a/src/invidious.cr+++ b/src/invidious.cr@@ -33,16 +33,7 @@ require "./invidious/jobs/**"CONFIG = Config.loadHMAC_KEY = CONFIG.hmac_key || Random::Secure.hex(32)-PG_URL = URI.new(- scheme: "postgres",- user: CONFIG.db.user,- password: CONFIG.db.password,- host: CONFIG.db.host,- port: CONFIG.db.port,- path: CONFIG.db.dbname,-)--PG_DB = DB.open PG_URL+PG_DB = DB.open CONFIG.database_urlARCHIVE_URL = URI.parse("https://archive.org")LOGIN_URL = URI.parse("https://accounts.google.com")PUBSUB_URL = URI.parse("https://pubsubhubbub.appspot.com")@@ -192,7 +183,7 @@ if CONFIG.captcha_keyendconnection_channel = Channel({Bool, Channel(PQ::Notification)}).new(32)-Invidious::Jobs.register Invidious::Jobs::NotificationJob.new(connection_channel, PG_URL)+Invidious::Jobs.register Invidious::Jobs::NotificationJob.new(connection_channel, CONFIG.database_url)Invidious::Jobs.start_alldiff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.crindex 00087143..a591b5eb 100644--- a/src/invidious/helpers/helpers.cr+++ b/src/invidious/helpers/helpers.cr@@ -64,11 +64,14 @@ endclass Configinclude YAML::Serializable- property channel_threads : Int32 = 1 # Number of threads to use for crawling videos from channels (for updating subscriptions)- property feed_threads : Int32 = 1 # Number of threads to use for updating feeds- property output : String = "STDOUT" # Log file path or STDOUT- property log_level : LogLevel = LogLevel::Info # Default log level, valid YAML values are ints and strings, see src/invidious/helpers/logger.cr- property db : DBConfig # Database configuration+ property channel_threads : Int32 = 1 # Number of threads to use for crawling videos from channels (for updating subscriptions)+ property feed_threads : Int32 = 1 # Number of threads to use for updating feeds+ property output : String = "STDOUT" # Log file path or STDOUT+ property log_level : LogLevel = LogLevel::Info # Default log level, valid YAML values are ints and strings, see src/invidious/helpers/logger.cr+ property db : DBConfig? = nil # Database configuration with separate parameters (username, hostname, etc)++ @[YAML::Field(converter: Preferences::URIConverter)]+ property database_url : URI = URI.parse("") # Database configuration using 12-Factor "Database URL" syntaxproperty decrypt_polling : Bool = true # Use polling to keep decryption function up to dateproperty full_refresh : Bool = false # Used for crawling channels: threads should check all videos uploaded by a channelproperty https_only : Bool? # Used to tell Invidious it is behind a proxy, so links to resources should be https://@@ -170,6 +173,23 @@ class Configend{% end %}+ # Build database_url from db.* if it's not set directly+ if config.database_url.to_s.empty?+ if db = config.db+ config.database_url = URI.new(+ scheme: "postgres",+ user: db.user,+ password: db.password,+ host: db.host,+ port: db.port,+ path: db.dbname,+ )+ else+ puts "Config : Either database_url or db.* is required"+ exit(1)+ end+ end+return configendenddiff --git a/src/invidious/users.cr b/src/invidious/users.crindex 153e3b6a..7a948b76 100644--- a/src/invidious/users.cr+++ b/src/invidious/users.cr@@ -173,6 +173,20 @@ struct Preferencesendend+ module URIConverter+ def self.to_yaml(value : URI, yaml : YAML::Nodes::Builder)+ yaml.scalar value.normalize!+ end++ def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : URI+ if node.is_a?(YAML::Nodes::Scalar)+ URI.parse node.value+ else+ node.raise "Expected scalar, not #{node.class}"+ end+ end+ end+module ProcessStringdef self.to_json(value : String, json : JSON::Builder)json.string value