Merge branch 'dev' into fix/rescan-when-files-added

This commit is contained in:
Alex Ling 2022-07-03 15:44:02 +08:00 committed by GitHub
commit 2d2486a598
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 27 deletions

View File

@ -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

View File

@ -1,31 +1,51 @@
require "yaml" require "yaml"
class Config class Config
private OPTIONS = {
"host" => "0.0.0.0",
"port" => 9000,
"base_url" => "/",
"session_secret" => "mango-session-secret",
"library_path" => "~/mango/library",
"library_cache_path" => "~/mango/library.yml.gz",
"db_path" => "~/mango.db",
"queue_db_path" => "~/mango/queue.db",
"scan_interval_minutes" => 5,
"thumbnail_generation_interval_hours" => 24,
"log_level" => "info",
"upload_path" => "~/mango/uploads",
"plugin_path" => "~/mango/plugins",
"download_timeout_seconds" => 30,
"cache_enabled" => true,
"cache_size_mbs" => 50,
"cache_log_enabled" => true,
"disable_login" => false,
"default_username" => "",
"auth_proxy_header_name" => "",
"plugin_update_interval_hours" => 24,
}
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 port : Int32 = 9000 # Go through the options constant above and define them as properties.
property base_url = "/" # Allow setting the default values through environment variables.
property session_secret = "mango-session-secret" # Overall precedence: config file > environment variable > default value
property library_path = "~/mango/library" {% begin %}
property library_cache_path = "~/mango/library.yml.gz" {% for k, v in OPTIONS %}
property db_path = "~/mango/mango.db" {% if v.is_a? StringLiteral %}
property queue_db_path = "~/mango/queue.db" property {{k.id}} : String = ENV[{{k.upcase}}]? || {{ v }}
property scan_interval_minutes : Int32 = 5 {% elsif v.is_a? NumberLiteral %}
property thumbnail_generation_interval_hours : Int32 = 24 property {{k.id}} : Int32 = (ENV[{{k.upcase}}]? || {{ v.id }}).to_i
property log_level = "info" {% elsif v.is_a? BoolLiteral %}
property upload_path = "~/mango/uploads" property {{k.id}} : Bool = env_is_true? {{ k.upcase }}, {{ v.id }}
property plugin_path = "~/mango/plugins" {% else %}
property download_timeout_seconds : Int32 = 30 raise "Unknown type in config option: {{ v.class_name.id }}"
property cache_enabled = true {% end %}
property cache_size_mbs = 50 {% end %}
property cache_log_enabled = true {% end %}
property disable_login = false
property default_username = ""
property auth_proxy_header_name = ""
property plugin_update_interval_hours : Int32 = 24
@@singlet : Config? @@singlet : Config?
@ -38,7 +58,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

View File

@ -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