Sha256: 262f17d5a50a694ed8fe43c47bdee83eb818ce6dfe9ebc4d0383c402d49bc965

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

require 'rack/cors'
require 'jsonapi-serializers'
require 'groupdate'
require 'net/http'
require 'useragent'
require 'jwt'
require 'bcrypt'
require_relative 'bootstraper'
require_relative 'collection'

module ForestLiana
  class Engine < ::Rails::Engine
    isolate_namespace ForestLiana

    def configure_forest_cors
      begin
        rack_cors_class = Rack::Cors
        rack_cors_class = 'Rack::Cors' if Rails::VERSION::MAJOR < 5

        config.middleware.insert_before 0, rack_cors_class do
          allow do
            hostnames = ['localhost:4200', /\A.*\.forestadmin\.com\z/]
            hostnames += ENV['CORS_ORIGINS'].split(',') if ENV['CORS_ORIGINS']

            origins hostnames
            resource '*', headers: :any, methods: :any, max_age: 86400 # NOTICE: 1 day
          end
        end
        nil
      rescue => exception
        exception
      end
    end

    def rake?
      File.basename($0) == 'rake'
    end

    def database_available?
      database_available = true
      begin
        ActiveRecord::Base.connection_pool.with_connection { |connection| connection.active? }
      rescue => error
        database_available = false
        FOREST_LOGGER.error "No Apimap sent to Forest servers, it seems that the database is not accessible:\n#{error}"
      end
      database_available
    end

    error = configure_forest_cors unless ENV['FOREST_CORS_DEACTIVATED']

    config.after_initialize do |app|
      if !Rails.env.test? && !rake?
        if error
          FOREST_LOGGER.error "Impossible to set the whitelisted Forest " \
            "domains for CORS constraint:\n#{error}"
        end

        app.eager_load!

        if database_available?
          # NOTICE: Do not run the code below on rails g forest_liana:install.
          Bootstraper.new(app).perform if ForestLiana.env_secret || ForestLiana.secret_key
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
forest_liana-2.10.2 lib/forest_liana/engine.rb
forest_liana-2.10.1 lib/forest_liana/engine.rb
forest_liana-2.10.0 lib/forest_liana/engine.rb
forest_liana-2.9.2 lib/forest_liana/engine.rb