Sha256: 621301224e2f33d0e60d48258958a7f0f1c14d8018482971a241d7123a97f5bf

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 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']
        
        @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
          host = env["SERVER_NAME"]
          env[:restpack][:host] = host
       
          channel = @core_cache.get_channel(host)
          domain = channel.get_domain_by_host(host)
          
          env[:restpack][:channel] = channel
          env[:restpack][:application] = domain.application
          env[:restpack][:domain] = domain
          
          add_user env
          
          add_application_domains env
          
        rescue Exception => e
          p "---"
          p e.message  
          p e.backtrace.inspect
          p "---"
        end

        @app.call(env)
      end
      
      private
      
      def add_user(env)
        user_id = env['rack.session'][:user_id]
        if user_id
          url = "http://:1234@localhost:1112/api/v1/users/#{user_id}.json" #TODO: GJ: build from config
          p url
          json = RestClient.get(url)
          user = JSON.parse(json)['user']
          env[:restpack][:user] = user
        end

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

      def add_application_domains(env)
        application = env[:restpack][:application]

        domains = {}

        configured_domains = application.get_configuration_by_key('domains')

        if configured_domains
          for key in configured_domains.value.keys
            domains[key] = configured_domains.value[key]
          end
        end

        env[:restpack][:application_domains] = domains
      end
    end
    

    
    

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
restpack-web-0.1.20 lib/restpack-web/app.rb