Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
5.2 KB
Raw Blame
require_relative "boot"

require "rails/all"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module Sure
  # Matches Rails::Application::Finisher#setup_default_session_store for Sure::Application
  # ("_#{railtie_name.chomp("_application")}_session" → "_sure_session").
  SESSION_COOKIE_KEY = "_sure_session"

  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 8.1

    # Please, add to the `ignore` list any other `lib` subdirectories that do
    # not contain `.rb` files, or that should not be reloaded or eager loaded.
    # Common ones are `templates`, `generators`, or `middleware`, for example.
    config.autoload_lib(ignore: %w[assets tasks generators rubocop])

    # 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")

    # Explicit source of truth for the session cookie name (also used by Codespaces).
    config.session_store :cookie_store, key: SESSION_COOKIE_KEY

    # Pages are server-rendered HTML that compresses ~15x, and the Docker image
    # serves Puma directly, so without this every page crossed the network
    # uncompressed unless a reverse proxy happened to compress it. A proxy that
    # does compress leaves an already-encoded response alone. Not in
    # development, where rack-mini-profiler has to inject into the HTML.
    #
    # After Rack::Sendfile, which every stack has: ActionDispatch::Static is
    # absent when public_file_server is disabled. Text only (PDFs and images are
    # already compressed), and never a 206, whose Content-Range counts bytes of
    # the uncompressed file.
    unless Rails.env.development?
      config.middleware.insert_after Rack::Sendfile, Rack::Deflater,
        include: %w[text/html text/vnd.turbo-stream.html text/css text/plain text/csv text/javascript
                    application/javascript application/json application/xml image/svg+xml],
        if: ->(_env, status, _headers, _body) { status != 206 }
    end

    # TODO: This is here for incremental adoption of localization.  This can be removed when all translations are implemented.
    config.i18n.fallbacks = true

    # Exact opt-in: version metadata is authenticated by default.
    config.public_version_endpoint = ENV["PUBLIC_VERSION_ENDPOINT"] == "true"

    config.app_mode = (ENV["SELF_HOSTED"] == "true" || ENV["SELF_HOSTING_ENABLED"] == "true" ? "self_hosted" : "managed").inquiry

    # Self hosters can optionally set their own encryption keys if they want to use ActiveRecord encryption.
    if Rails.application.credentials.active_record_encryption.present?
      config.active_record.encryption = Rails.application.credentials.active_record_encryption
    end

    config.view_component.previews.controller = "LookbooksController"
    config.lookbook.preview_display_options = {
      theme: [ "light", "dark" ] # available in view as params[:theme]
    }

    # Enable Skylight instrumentation for ActiveJob (background workers)
    # Developers can opt-in to Skylight locally by setting SKYLIGHT_ENABLED=true
    if defined?(Skylight) && config.respond_to?(:skylight)
      config.skylight.probes << "active_job"
      if ENV["SKYLIGHT_ENABLED"] == "true"
        config.skylight.environments += [ "development" ]
      end
    end

    # Rack::Attack's own Railtie (lib/rack/attack/railtie.rb in the gem)
    # already inserts it into the middleware stack — this explicit `use` was
    # a second, redundant insertion (confirmed via `bin/rails middleware`,
    # which listed Rack::Attack twice). Since Rack::Attack's counters
    # increment once per middleware pass, every throttle in
    # config/initializers/rack_attack.rb was silently firing at half its
    # documented limit. Removed rather than kept as a second layer, since
    # nothing in the app relies on it running twice and the halved limits
    # were undocumented/accidental, not a deliberate stricter policy.

    config.x.ui = ActiveSupport::OrderedOptions.new
    default_layout = ENV.fetch("DEFAULT_UI_LAYOUT", "dashboard")
    config.x.ui.default_layout = default_layout.in?(%w[dashboard intro]) ? default_layout : "dashboard"

    config.x.debug_log = ActiveSupport::OrderedOptions.new
    retention_days = ENV.fetch("DEBUG_LOG_RETENTION_DAYS", "90").to_i
    config.x.debug_log.retention_days = retention_days.positive? ? retention_days : 90

    # Handle OmniAuth/OIDC errors gracefully (must be before OmniAuth middleware)
    require_relative "../app/middleware/omniauth_error_handler"
    config.middleware.use OmniauthErrorHandler

    # Bound a FinanceKit batch upload before Rails reads and parses it; see
    # the class comment for why the controller's own size check is too late.
    # Ahead of Rack::MethodOverride, the first middleware that reads a body.
    require_relative "../app/middleware/financekit_body_limit"
    config.middleware.insert_before Rack::MethodOverride, FinancekitBodyLimit
  end
end