Skip to content

Commit

Permalink
Config: Reload configuration on modification
Browse files Browse the repository at this point in the history
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 iv-org#16
  • Loading branch information
Fijxu committed Oct 28, 2024
1 parent 3afac4d commit 67d7b78
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
4 changes: 4 additions & 0 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 15 additions & 1 deletion src/invidious.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require "yaml"
require "compress/zip"
require "protodec/utils"
require "redis"
require "inotify"

require "./invidious/database/*"
require "./invidious/database/migrations/*"
Expand All @@ -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
Expand Down
27 changes: 25 additions & 2 deletions src/invidious/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit 67d7b78

Please sign in to comment.