Allow config defaults to be sourced from ENV

This allows the default config to source values from ENV variables if
they are set. With this change we don't have to modify the docker CMD or
edit the config.yml and then relaunch.
This commit is contained in:
Chris Alexander 2022-06-15 10:12:51 -05:00
parent 3b5e764d36
commit 2e91028ead
No known key found for this signature in database
GPG Key ID: 0E7483FA52195C26
3 changed files with 50 additions and 27 deletions

View File

@ -1,14 +1,31 @@
require "./spec_helper"
describe Config do
it "creates config if it does not exist" do
with_default_config do |_, path|
it "creates default config if it does not exist" do
with_default_config do |config, path|
File.exists?(path).should be_true
config.port.should eq 9000
end
end
it "correctly loads config" do
config = Config.load "spec/asset/test-config.yml"
config.port.should eq 3000
config.base_url.should eq "/"
end
it "correctly reads config defaults from ENV" do
ENV["LOG_LEVEL"] = "debug"
config = Config.load "spec/asset/test-config.yml"
config.log_level.should eq "debug"
config.base_url.should eq "/"
end
it "correctly handles ENV truthiness" do
ENV["CACHE_ENABLED"] = "false"
config = Config.load "spec/asset/test-config.yml"
config.cache_enabled.should be_false
config.cache_log_enabled.should be_true
config.disable_login.should be_false
end
end

View File

@ -4,28 +4,34 @@ class Config
include YAML::Serializable
@[YAML::Field(ignore: true)]
property path = ""
property host = "0.0.0.0"
property port : Int32 = 9000
property base_url = "/"
property session_secret = "mango-session-secret"
property library_path = "~/mango/library"
property library_cache_path = "~/mango/library.yml.gz"
property db_path = "~/mango/mango.db"
property queue_db_path = "~/mango/queue.db"
property scan_interval_minutes : Int32 = 5
property thumbnail_generation_interval_hours : Int32 = 24
property log_level = "info"
property upload_path = "~/mango/uploads"
property plugin_path = "~/mango/plugins"
property download_timeout_seconds : Int32 = 30
property cache_enabled = true
property cache_size_mbs = 50
property cache_log_enabled = true
property disable_login = false
property default_username = ""
property auth_proxy_header_name = ""
property plugin_update_interval_hours : Int32 = 24
property path : String = ""
property host : String = (ENV["LISTEN_HOST"]? || "0.0.0.0")
property port : Int32 = (ENV["LISTEN_PORT"]? || 9000).to_i
property base_url : String = (ENV["BASE_URL"]? || "/")
property session_secret : String = \
(ENV["SESSION_SECRET"]? || "mango-session-secret")
property library_path : String = (ENV["LIBRARY_PATH"]? || "~/mango/library")
property library_cache_path : String = \
(ENV["LIBRARY_CACHE_PATH"]? || "~/mango/library.yml.gz")
property db_path : String = (ENV["DB_PATH"]? || "~/mango/mango.db")
property queue_db_path : String = \
(ENV["QUEUE_DB_PATH"]? || "~/mango/queue.db")
property scan_interval_minutes : Int32 = (ENV["SCAN_INTERVAL"]? || 5).to_i
property thumbnail_generation_interval_hours : Int32 = \
(ENV["THUMBNAIL_INTERVAL"]? || 24).to_i
property log_level : String = (ENV["LOG_LEVEL"]? || "info")
property upload_path : String = (ENV["UPLOAD_PATH"]? || "~/mango/uploads")
property plugin_path : String = (ENV["PLUGIN_PATH"]? || "~/mango/plugins")
property download_timeout_seconds : Int32 = \
(ENV["DOWNLOAD_TIMEOUT"]? || 30).to_i
property cache_enabled : Bool = env_is_true?("CACHE_ENABLED", true)
property cache_size_mbs : Int32 = (ENV["CACHE_SIZE"]? || 50).to_i
property cache_log_enabled : Bool = env_is_true?("CACHE_LOG_ENABLED", true)
property disable_login : Bool = env_is_true?("DISABLE_LOGIN", false)
property default_username : String = (ENV["DEFAULT_USERNAME"]? || "")
property auth_proxy_header_name : String = (ENV["AUTH_PROXY_HEADER"]? || "")
property plugin_update_interval_hours : Int32 = \
(ENV["PLUGIN_UPDATE_INTERVAL"]? || 24).to_i
@@singlet : Config?
@ -38,7 +44,7 @@ class Config
end
def self.load(path : String?)
path = "~/.config/mango/config.yml" if path.nil?
path = (ENV["CONFIG_PATH"]? || "~/.config/mango/config.yml") if path.nil?
cfg_path = File.expand_path path, home: true
if File.exists? cfg_path
config = self.from_yaml File.read cfg_path

View File

@ -95,9 +95,9 @@ class String
end
end
def env_is_true?(key : String) : Bool
def env_is_true?(key : String, default : Bool = false) : Bool
val = ENV[key.upcase]? || ENV[key.downcase]?
return false unless val
return default unless val
val.downcase.in? "1", "true"
end