Sha256: e4c75d5cb8491bb1cc3f7cd1f2db675b2426ce26d1f3814ca09de708344130ad

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

require 'restpack_core_client'

module RestPack
  module Web
    class App
      def initialize(app, options = {})
        p "RestPack::Web::App.initialize"
        @app = app

        options[:core_domain] ||= ENV['RESTPACK_CORE_SERVICE']
        options[:access_key] ||= ENV['RESTPACK_ACCESS_KEY']

        raise "missing RestPack configuration" unless options[:core_domain] || options[:access_key]

        @core_cache = RestPack::Core::Client::Cache.create(options[:core_domain], options[:access_key])
      end

      def call(env)
        env[:restpack] ||= {}

        begin #TODO: GJ: cache should handle errors gracefully
          request = Rack::Request.new(env)

          channel = @core_cache.get_channel(request.host)
          domain = channel.get_domain_by_host(request.host)

          env[:restpack][:request] = request
          env[:restpack][:host] = request.host
          env[:restpack][:channel] = channel
          env[:restpack][:application] = domain.application
          env[:restpack][:domain] = domain
          env[:restpack][:configurations] = @core_cache.configurations
          env[:restpack][:services] = @core_cache.get_configuration_value('services', [])

          add_user env

        rescue Exception => e
          p "---"
          p e.message
          p "===="
          p e.backtrace.inspect
          p "---"
        end

        @app.call(env)
      end

      private

      def add_user(env)
        return unless env['rack.session']

        user_id = env['rack.session'][:user_id]

        if user_id
          user_id = env['rack.session'][:user_id]
          channel_domains = env[:restpack][:channel].configurations.find{ |conf| conf.key == 'domains' }.value
          auth_domain = channel_domains[:auth]
          json = RestClient.get("#{auth_domain}/api/v1/users/#{user_id}.json")
          env[:restpack][:user] = Yajl::Parser.parse(json)['user']
        end

        env[:restpack][:user_id] = user_id
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
restpack_web-0.2.19 lib/restpack_web/app.rb
restpack_web-0.2.18 lib/restpack_web/app.rb
restpack_web-0.2.17 lib/restpack_web/app.rb