Sha256: f9e009d17191b656ab82a006d15adeaad14e3668dee06d5caff59f9aea8246d6

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require 'rack/cors'
require 'rack-rewrite'
require 'rack-server-pages'
require 'otr-activerecord' if SlackRubyBotServer::Config.activerecord?

module SlackRubyBotServer
  module Api
    class Middleware
      def self.logger
        @logger ||= begin
          $stdout.sync = true
          Logger.new(STDOUT)
        end
      end

      def self.instance
        @instance ||= Rack::Builder.new do
          use OTR::ActiveRecord::ConnectionManagement if SlackRubyBotServer::Config.activerecord?

          use Rack::Cors do
            allow do
              origins '*'
              resource '*', headers: :any, methods: [:get, :post]
            end
          end

          # rewrite HAL links to make them clickable in a browser
          use Rack::Rewrite do
            r302 %r{(\/[\w\/]*\/)(%7B|\{)?(.*)(%7D|\})}, '$1'
          end

          use Rack::ServerPages do |config|
            config.view_path = [
              'views', # relative to Dir.pwd
              'public', # relative to Dir.pwd
              File.expand_path(File.join(__dir__, '../../../public')) # built-in fallback
            ]
          end

          run Middleware.new
        end.to_app
      end

      def call(env)
        Endpoints::RootEndpoint.call(env)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slack-ruby-bot-server-0.6.0 lib/slack-ruby-bot-server/api/middleware.rb