# frozen_string_literal: true require_relative 'boot' require 'rails' # Pick the frameworks you want: require 'active_model/railtie' require 'active_job/railtie' require 'active_record/railtie' require 'action_controller/railtie' require 'action_view/railtie' # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) module RynDNS class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. config.load_defaults 6.1 # Configuration for the application, engines, and railties goes here. # # These settings can be overridden in specific environments using the files # in config/environments, which are processed later. # # config.time_zone = "Central Time (US & Canada)" # config.eager_load_paths << Rails.root.join("extras") # Only loads a smaller set of middleware suitable for API only apps. # Middleware like session, flash, cookies can be added back manually. # Skip views, helpers and assets when generating a new resource. config.api_only = true # FIXME: move this out of the application class prefix_valid = lambda { |prefix| error_msg = nil error_msg = 'WARNING: prefix is not set' unless prefix error_msg = 'WARNING: prefix needs to have eight characters' unless error_msg || prefix.length >= 8 error_msg = 'WARNING: prefix needs to be alphanumeric' unless error_msg || prefix.match(/\A[a-zA-Z0-9]*\z/) puts error_msg if error_msg error_msg.nil? } # global constants API_VERSION = '1.0.0' API_VERSION_NAMESPACE = "v#{API_VERSION.split('.')[0]}" RELATIVE_URL_ROOT = ENV['RAILS_RELATIVE_URL_ROOT'].to_s || '/' # TODO: add configuration model to save prefix and use ENV to overwrite it prefix = ENV['RAILS_API_KEY_PREFIX'] if prefix_valid.call(prefix) API_KEY_PREFIX = prefix[0, 8] else puts "WARNING: ENV['RAILS_API_KEY_PREFIX'] is invalid" puts 'WARNING: Generating random prefix which will reset each server start' API_KEY_PREFIX = SecureRandom.alphanumeric(8) puts "Generated prefix: #{API_KEY_PREFIX}" puts "WARNING: Please modify api_key -> #{API_KEY_PREFIX}.########.############" end API_KEY_HEADER = ENV['RAILS_API_KEY_HEADER'] || 'X-API-Key' end end