From 02cab4bf31b717bc105ae87046c28f09cff5063f Mon Sep 17 00:00:00 2001 From: Fijxu Date: Mon, 28 Oct 2024 13:37:06 -0300 Subject: [PATCH] Config: Reload configuration on modification It detects changes on the config.yml automtically if invidious is running on linux. If not, the configuration can be reloaded using `kill -s HUP $(pidof invidious)` or any other tool that sends a SIGHUP signal to the invidious process. Closes #16 --- shard.lock | 4 ++++ shard.yml | 3 +++ src/invidious.cr | 16 +++++++++++++++- src/invidious/config.cr | 27 +++++++++++++++++++++++++-- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/shard.lock b/shard.lock index 8c599d1b..af3a96d7 100644 --- a/shard.lock +++ b/shard.lock @@ -20,6 +20,10 @@ shards: git: https://github.com/crystal-loot/exception_page.git version: 0.2.2 + inotify: + git: https://github.com/petoem/inotify.cr.git + version: 1.0.3 + kemal: git: https://github.com/kemalcr/kemal.git version: 1.1.2 diff --git a/shard.yml b/shard.yml index c46e1c42..33d57e8f 100644 --- a/shard.yml +++ b/shard.yml @@ -30,6 +30,9 @@ dependencies: version: ~> 0.1.1 redis: github: stefanwille/crystal-redis + inotify: + github: petoem/inotify.cr + version: 1.0.3 development_dependencies: spectator: diff --git a/src/invidious.cr b/src/invidious.cr index b66d5edb..5d81aa8c 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -32,6 +32,7 @@ require "yaml" require "compress/zip" require "protodec/utils" require "redis" +require "inotify" require "./invidious/database/*" require "./invidious/database/migrations/*" @@ -58,7 +59,20 @@ end # Simple alias to make code easier to read alias IV = Invidious -CONFIG = Config.load +CONFIG = Config.load + +Signal::HUP.trap do + Config.reload +end + +{% if flag?(:linux) %} +if CONFIG.reload_config_automatically + Inotify.watch("config/config.yml") do |event| + Config.reload + end +end +{% end %} + HMAC_KEY = CONFIG.hmac_key PG_DB = DB.open CONFIG.database_url diff --git a/src/invidious/config.cr b/src/invidious/config.cr index accfad2f..f83d8c29 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -95,6 +95,8 @@ class Config property hmac_key : String = "" # Domain to be used for links to resources on the site where an absolute URL is required property domain : String? + # Materialious redirects + property materialious_domain : String? # Alternative domains. You can add other domains, like TOR and I2P addresses property alternative_domains : Array(String) = [] of String property donation_url : String? @@ -190,9 +192,11 @@ class Config property pubsub_domain : String = "" property ignore_user_tokens : Bool = false + + {% if flag?(:linux) %} + property reload_config_automatically : Bool = true + {% end %} - # Materialious redirects - property materialious_domain : String? def disabled?(option) case disabled = CONFIG.disable_proxy @@ -209,6 +213,25 @@ class Config end end + def self.reload + LOGGER.info("Config: Reloading configuration") + # Load config from file or YAML string env var + env_config_file = "INVIDIOUS_CONFIG_FILE" + env_config_yaml = "INVIDIOUS_CONFIG" + config_file = ENV.has_key?(env_config_file) ? ENV.fetch(env_config_file) : "config/config.yml" + config_yaml = ENV.has_key?(env_config_yaml) ? ENV.fetch(env_config_yaml) : File.read(config_file) + begin + config = Config.from_yaml(config_yaml) + rescue ex + LOGGER.error("Config: Error when reloading configuration: '#{ex.message}'") + config = CONFIG + end + {% for ivar in Config.instance_vars %} + CONFIG.{{ivar}} = config.{{ivar}} + {% end %} + LOGGER.info("Config: Reload successfull") + end + def self.load # Load config from file or YAML string env var env_config_file = "INVIDIOUS_CONFIG_FILE"