mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-03 03:15:31 -04:00
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:
parent
3b5e764d36
commit
2e91028ead
@ -1,14 +1,31 @@
|
|||||||
require "./spec_helper"
|
require "./spec_helper"
|
||||||
|
|
||||||
describe Config do
|
describe Config do
|
||||||
it "creates config if it does not exist" do
|
it "creates default config if it does not exist" do
|
||||||
with_default_config do |_, path|
|
with_default_config do |config, path|
|
||||||
File.exists?(path).should be_true
|
File.exists?(path).should be_true
|
||||||
|
config.port.should eq 9000
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "correctly loads config" do
|
it "correctly loads config" do
|
||||||
config = Config.load "spec/asset/test-config.yml"
|
config = Config.load "spec/asset/test-config.yml"
|
||||||
config.port.should eq 3000
|
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
|
||||||
end
|
end
|
||||||
|
@ -4,28 +4,34 @@ class Config
|
|||||||
include YAML::Serializable
|
include YAML::Serializable
|
||||||
|
|
||||||
@[YAML::Field(ignore: true)]
|
@[YAML::Field(ignore: true)]
|
||||||
property path = ""
|
property path : String = ""
|
||||||
property host = "0.0.0.0"
|
property host : String = (ENV["LISTEN_HOST"]? || "0.0.0.0")
|
||||||
property port : Int32 = 9000
|
property port : Int32 = (ENV["LISTEN_PORT"]? || 9000).to_i
|
||||||
property base_url = "/"
|
property base_url : String = (ENV["BASE_URL"]? || "/")
|
||||||
property session_secret = "mango-session-secret"
|
property session_secret : String = \
|
||||||
property library_path = "~/mango/library"
|
(ENV["SESSION_SECRET"]? || "mango-session-secret")
|
||||||
property library_cache_path = "~/mango/library.yml.gz"
|
property library_path : String = (ENV["LIBRARY_PATH"]? || "~/mango/library")
|
||||||
property db_path = "~/mango/mango.db"
|
property library_cache_path : String = \
|
||||||
property queue_db_path = "~/mango/queue.db"
|
(ENV["LIBRARY_CACHE_PATH"]? || "~/mango/library.yml.gz")
|
||||||
property scan_interval_minutes : Int32 = 5
|
property db_path : String = (ENV["DB_PATH"]? || "~/mango/mango.db")
|
||||||
property thumbnail_generation_interval_hours : Int32 = 24
|
property queue_db_path : String = \
|
||||||
property log_level = "info"
|
(ENV["QUEUE_DB_PATH"]? || "~/mango/queue.db")
|
||||||
property upload_path = "~/mango/uploads"
|
property scan_interval_minutes : Int32 = (ENV["SCAN_INTERVAL"]? || 5).to_i
|
||||||
property plugin_path = "~/mango/plugins"
|
property thumbnail_generation_interval_hours : Int32 = \
|
||||||
property download_timeout_seconds : Int32 = 30
|
(ENV["THUMBNAIL_INTERVAL"]? || 24).to_i
|
||||||
property cache_enabled = true
|
property log_level : String = (ENV["LOG_LEVEL"]? || "info")
|
||||||
property cache_size_mbs = 50
|
property upload_path : String = (ENV["UPLOAD_PATH"]? || "~/mango/uploads")
|
||||||
property cache_log_enabled = true
|
property plugin_path : String = (ENV["PLUGIN_PATH"]? || "~/mango/plugins")
|
||||||
property disable_login = false
|
property download_timeout_seconds : Int32 = \
|
||||||
property default_username = ""
|
(ENV["DOWNLOAD_TIMEOUT"]? || 30).to_i
|
||||||
property auth_proxy_header_name = ""
|
property cache_enabled : Bool = env_is_true?("CACHE_ENABLED", true)
|
||||||
property plugin_update_interval_hours : Int32 = 24
|
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?
|
@@singlet : Config?
|
||||||
|
|
||||||
@ -38,7 +44,7 @@ class Config
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.load(path : String?)
|
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
|
cfg_path = File.expand_path path, home: true
|
||||||
if File.exists? cfg_path
|
if File.exists? cfg_path
|
||||||
config = self.from_yaml File.read cfg_path
|
config = self.from_yaml File.read cfg_path
|
||||||
|
@ -95,9 +95,9 @@ class String
|
|||||||
end
|
end
|
||||||
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]?
|
val = ENV[key.upcase]? || ENV[key.downcase]?
|
||||||
return false unless val
|
return default unless val
|
||||||
val.downcase.in? "1", "true"
|
val.downcase.in? "1", "true"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user