Define properties with macro

This commit is contained in:
Alex Ling 2022-07-03 07:24:33 +00:00
parent 49425ff714
commit f2d6d28a72

View File

@ -1,37 +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 : String = "" property path : String = ""
property host : String = (ENV["LISTEN_HOST"]? || "0.0.0.0")
property port : Int32 = (ENV["LISTEN_PORT"]? || 9000).to_i # Go through the options constant above and define them as properties.
property base_url : String = (ENV["BASE_URL"]? || "/") # Allow setting the default values through environment variables.
# ameba:disable Layout/LineLength # Overall precedence: config file > environment variable > default value
property session_secret : String = (ENV["SESSION_SECRET"]? || "mango-session-secret") {% begin %}
property library_path : String = (ENV["LIBRARY_PATH"]? || "~/mango/library") {% for k, v in OPTIONS %}
# ameba:disable Layout/LineLength {% if v.is_a? StringLiteral %}
property library_cache_path : String = (ENV["LIBRARY_CACHE_PATH"]? || "~/mango/library.yml.gz") property {{k.id}} : String = ENV[{{k.upcase}}]? || {{ v }}
property db_path : String = (ENV["DB_PATH"]? || "~/mango/mango.db") {% elsif v.is_a? NumberLiteral %}
# ameba:disable Layout/LineLength property {{k.id}} : Int32 = (ENV[{{k.upcase}}]? || {{ v.id }}).to_i
property queue_db_path : String = (ENV["QUEUE_DB_PATH"]? || "~/mango/queue.db") {% elsif v.is_a? BoolLiteral %}
property scan_interval_minutes : Int32 = (ENV["SCAN_INTERVAL"]? || 5).to_i property {{k.id}} : Bool = env_is_true? {{ k.upcase }}, {{ v.id }}
# ameba:disable Layout/LineLength {% else %}
property thumbnail_generation_interval_hours : Int32 = (ENV["THUMBNAIL_INTERVAL"]? || 24).to_i raise "Unknown type in config option: {{ v.class_name.id }}"
property log_level : String = (ENV["LOG_LEVEL"]? || "info") {% end %}
property upload_path : String = (ENV["UPLOAD_PATH"]? || "~/mango/uploads") {% end %}
property plugin_path : String = (ENV["PLUGIN_PATH"]? || "~/mango/plugins") {% end %}
# 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
@@singlet : Config? @@singlet : Config?