diff --git a/src/config.cr b/src/config.cr index 867b8ab..2384431 100644 --- a/src/config.cr +++ b/src/config.cr @@ -1,37 +1,51 @@ require "yaml" 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 @[YAML::Field(ignore: true)] 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"]? || "/") - # ameba:disable Layout/LineLength - property session_secret : String = (ENV["SESSION_SECRET"]? || "mango-session-secret") - property library_path : String = (ENV["LIBRARY_PATH"]? || "~/mango/library") - # ameba:disable Layout/LineLength - property library_cache_path : String = (ENV["LIBRARY_CACHE_PATH"]? || "~/mango/library.yml.gz") - property db_path : String = (ENV["DB_PATH"]? || "~/mango/mango.db") - # ameba:disable Layout/LineLength - property queue_db_path : String = (ENV["QUEUE_DB_PATH"]? || "~/mango/queue.db") - property scan_interval_minutes : Int32 = (ENV["SCAN_INTERVAL"]? || 5).to_i - # ameba:disable Layout/LineLength - 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") - # ameba:disable Layout/LineLength - 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"]? || "") - # ameba:disable Layout/LineLength - property plugin_update_interval_hours : Int32 = (ENV["PLUGIN_UPDATE_INTERVAL"]? || 24).to_i + + # Go through the options constant above and define them as properties. + # Allow setting the default values through environment variables. + # Overall precedence: config file > environment variable > default value + {% begin %} + {% for k, v in OPTIONS %} + {% if v.is_a? StringLiteral %} + property {{k.id}} : String = ENV[{{k.upcase}}]? || {{ v }} + {% elsif v.is_a? NumberLiteral %} + property {{k.id}} : Int32 = (ENV[{{k.upcase}}]? || {{ v.id }}).to_i + {% elsif v.is_a? BoolLiteral %} + property {{k.id}} : Bool = env_is_true? {{ k.upcase }}, {{ v.id }} + {% else %} + raise "Unknown type in config option: {{ v.class_name.id }}" + {% end %} + {% end %} + {% end %} @@singlet : Config?