forked from midou/invidious
@ -7,7 +7,7 @@ class InfoException < Exception
|
||||
end
|
||||
|
||||
macro error_template(*args)
|
||||
error_template_helper(env, config, locale, {{*args}})
|
||||
error_template_helper(env, locale, {{*args}})
|
||||
end
|
||||
|
||||
def github_details(summary : String, content : String)
|
||||
@ -22,9 +22,9 @@ def github_details(summary : String, content : String)
|
||||
return HTML.escape(details)
|
||||
end
|
||||
|
||||
def error_template_helper(env : HTTP::Server::Context, config : Config, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)
|
||||
def error_template_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)
|
||||
if exception.is_a?(InfoException)
|
||||
return error_template_helper(env, config, locale, status_code, exception.message || "")
|
||||
return error_template_helper(env, locale, status_code, exception.message || "")
|
||||
end
|
||||
env.response.content_type = "text/html"
|
||||
env.response.status_code = status_code
|
||||
@ -43,7 +43,7 @@ def error_template_helper(env : HTTP::Server::Context, config : Config, locale :
|
||||
return templated "error"
|
||||
end
|
||||
|
||||
def error_template_helper(env : HTTP::Server::Context, config : Config, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String)
|
||||
def error_template_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String)
|
||||
env.response.content_type = "text/html"
|
||||
env.response.status_code = status_code
|
||||
error_message = translate(locale, message)
|
||||
@ -51,31 +51,31 @@ def error_template_helper(env : HTTP::Server::Context, config : Config, locale :
|
||||
end
|
||||
|
||||
macro error_atom(*args)
|
||||
error_atom_helper(env, config, locale, {{*args}})
|
||||
error_atom_helper(env, locale, {{*args}})
|
||||
end
|
||||
|
||||
def error_atom_helper(env : HTTP::Server::Context, config : Config, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)
|
||||
def error_atom_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)
|
||||
if exception.is_a?(InfoException)
|
||||
return error_atom_helper(env, config, locale, status_code, exception.message || "")
|
||||
return error_atom_helper(env, locale, status_code, exception.message || "")
|
||||
end
|
||||
env.response.content_type = "application/atom+xml"
|
||||
env.response.status_code = status_code
|
||||
return "<error>#{exception.inspect_with_backtrace}</error>"
|
||||
end
|
||||
|
||||
def error_atom_helper(env : HTTP::Server::Context, config : Config, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String)
|
||||
def error_atom_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String)
|
||||
env.response.content_type = "application/atom+xml"
|
||||
env.response.status_code = status_code
|
||||
return "<error>#{message}</error>"
|
||||
end
|
||||
|
||||
macro error_json(*args)
|
||||
error_json_helper(env, config, locale, {{*args}})
|
||||
error_json_helper(env, locale, {{*args}})
|
||||
end
|
||||
|
||||
def error_json_helper(env : HTTP::Server::Context, config : Config, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception, additional_fields : Hash(String, Object) | Nil)
|
||||
def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception, additional_fields : Hash(String, Object) | Nil)
|
||||
if exception.is_a?(InfoException)
|
||||
return error_json_helper(env, config, locale, status_code, exception.message || "", additional_fields)
|
||||
return error_json_helper(env, locale, status_code, exception.message || "", additional_fields)
|
||||
end
|
||||
env.response.content_type = "application/json"
|
||||
env.response.status_code = status_code
|
||||
@ -86,11 +86,11 @@ def error_json_helper(env : HTTP::Server::Context, config : Config, locale : Has
|
||||
return error_message.to_json
|
||||
end
|
||||
|
||||
def error_json_helper(env : HTTP::Server::Context, config : Config, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)
|
||||
return error_json_helper(env, config, locale, status_code, exception, nil)
|
||||
def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)
|
||||
return error_json_helper(env, locale, status_code, exception, nil)
|
||||
end
|
||||
|
||||
def error_json_helper(env : HTTP::Server::Context, config : Config, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String, additional_fields : Hash(String, Object) | Nil)
|
||||
def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String, additional_fields : Hash(String, Object) | Nil)
|
||||
env.response.content_type = "application/json"
|
||||
env.response.status_code = status_code
|
||||
error_message = {"error" => message}
|
||||
@ -100,6 +100,6 @@ def error_json_helper(env : HTTP::Server::Context, config : Config, locale : Has
|
||||
return error_message.to_json
|
||||
end
|
||||
|
||||
def error_json_helper(env : HTTP::Server::Context, config : Config, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String)
|
||||
error_json_helper(env, config, locale, status_code, message, nil)
|
||||
def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String)
|
||||
error_json_helper(env, locale, status_code, message, nil)
|
||||
end
|
||||
|
@ -115,6 +115,63 @@ class Config
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def self.load
|
||||
# Load config from file or YAML string env var
|
||||
env_config_file = "INVIDIOUS_CONFIG_FILE"
|
||||
env_config_yaml = "INVIDIOUS_CONFIG"
|
||||
|
||||
config_file = ENV.has_key?(env_config_file) ? ENV.fetch(env_config_file) : "config/config.yml"
|
||||
config_yaml = ENV.has_key?(env_config_yaml) ? ENV.fetch(env_config_yaml) : File.read(config_file)
|
||||
|
||||
config = Config.from_yaml(config_yaml)
|
||||
|
||||
# Update config from env vars (upcased and prefixed with "INVIDIOUS_")
|
||||
{% for ivar in Config.instance_vars %}
|
||||
{% env_id = "INVIDIOUS_#{ivar.id.upcase}" %}
|
||||
|
||||
if ENV.has_key?({{env_id}})
|
||||
# puts %(Config.{{ivar.id}} : Loading from env var {{env_id}})
|
||||
env_value = ENV.fetch({{env_id}})
|
||||
success = false
|
||||
|
||||
# Use YAML converter if specified
|
||||
{% ann = ivar.annotation(::YAML::Field) %}
|
||||
{% if ann && ann[:converter] %}
|
||||
puts %(Config.{{ivar.id}} : Parsing "#{env_value}" as {{ivar.type}} with {{ann[:converter]}} converter)
|
||||
config.{{ivar.id}} = {{ann[:converter]}}.from_yaml(YAML::ParseContext.new, YAML::Nodes.parse(ENV.fetch({{env_id}})).nodes[0])
|
||||
puts %(Config.{{ivar.id}} : Set to #{config.{{ivar.id}}})
|
||||
success = true
|
||||
|
||||
# Use regular YAML parser otherwise
|
||||
{% else %}
|
||||
{% ivar_types = ivar.type.union? ? ivar.type.union_types : [ivar.type] %}
|
||||
# Sort types to avoid parsing nulls and numbers as strings
|
||||
{% ivar_types = ivar_types.sort_by { |ivar_type| ivar_type == Nil ? 0 : ivar_type == Int32 ? 1 : 2 } %}
|
||||
{{ivar_types}}.each do |ivar_type|
|
||||
if !success
|
||||
begin
|
||||
# puts %(Config.{{ivar.id}} : Trying to parse "#{env_value}" as #{ivar_type})
|
||||
config.{{ivar.id}} = ivar_type.from_yaml(env_value)
|
||||
puts %(Config.{{ivar.id}} : Set to #{config.{{ivar.id}}} (#{ivar_type}))
|
||||
success = true
|
||||
rescue
|
||||
# nop
|
||||
end
|
||||
end
|
||||
end
|
||||
{% end %}
|
||||
|
||||
# Exit on fail
|
||||
if !success
|
||||
puts %(Config.{{ivar.id}} failed to parse #{env_value} as {{ivar.type}})
|
||||
exit(1)
|
||||
end
|
||||
end
|
||||
{% end %}
|
||||
|
||||
return config
|
||||
end
|
||||
end
|
||||
|
||||
struct DBConfig
|
||||
|
@ -280,9 +280,9 @@ def arg_array(array, start = 1)
|
||||
return args
|
||||
end
|
||||
|
||||
def make_host_url(config, kemal_config)
|
||||
ssl = config.https_only || kemal_config.ssl
|
||||
port = config.external_port || kemal_config.port
|
||||
def make_host_url(kemal_config)
|
||||
ssl = CONFIG.https_only || kemal_config.ssl
|
||||
port = CONFIG.external_port || kemal_config.port
|
||||
|
||||
if ssl
|
||||
scheme = "https://"
|
||||
@ -297,11 +297,11 @@ def make_host_url(config, kemal_config)
|
||||
port = ""
|
||||
end
|
||||
|
||||
if !config.domain
|
||||
if !CONFIG.domain
|
||||
return ""
|
||||
end
|
||||
|
||||
host = config.domain.not_nil!.lchop(".")
|
||||
host = CONFIG.domain.not_nil!.lchop(".")
|
||||
|
||||
return "#{scheme}#{host}#{port}"
|
||||
end
|
||||
@ -345,7 +345,7 @@ def sha256(text)
|
||||
return digest.final.hexstring
|
||||
end
|
||||
|
||||
def subscribe_pubsub(topic, key, config)
|
||||
def subscribe_pubsub(topic, key)
|
||||
case topic
|
||||
when .match(/^UC[A-Za-z0-9_-]{22}$/)
|
||||
topic = "channel_id=#{topic}"
|
||||
|
Reference in New Issue
Block a user