Sha256: 17290a77a0ea7239d242f999db4a2fea4f978dc85ff1d443f657df670a2e0097

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require 'open-uri'
require 'webrick/httpproxy'
require 'pp'
require 'open-uri'


class DNSLookup
  def initialize(hash={})
    @hash=hash
  end
  
  def []=(a,b)
    @hash[a]=b
  end

  def [](name)
    @hash[name]
  end
end

class MyWebApp
  attr_reader :name
  def initialize(name)
    @name=name
  end

  def get(req,res)
      res.body="<html><body><h1>MUH</h1>NAME:#{@name}</body></html>"
      res["content-type"]="text/html"
  end
end

class Web
  def initialize(dns)
    @apps=[]
    @dns=dns
  end
  def <<(app)
    @apps<<app
  end
  def [](url)
    name=@dns[url]
    if name
      @apps.select{|app|app.name==name}[0]
    end
  end
end


class MProxy< WEBrick::HTTPProxyServer
  def initialize(port,web)
    @web=web
    super(:BindAddress => ARGV[0] || "127.0.0.1",
        :Port => ARGV[1] || port
    )
  end
  def proxy_service(req, res)   
    app=@web[req.request_uri.host]
    if app
      return app.get(req,res)
    end
    begin
      super
    rescue Object=>e
      pp e,e.backtrace
    end
  end
end

begin

   dns=DNSLookup.new
   dns["blub.appswarm"]="127.0.0.1"
   
   web=Web.new(dns)
   web << MyWebApp.new("my")
   dns["my.appswarm"]="my"
   
   pp dns
    server = MProxy::new(37004,web)
    trap( :INT ) { server.shutdown }
    server.start
rescue Exception
    puts  "Error: #{$!}"
    server.shutdown
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
appswarm-0.0.1 apps/global_web_proxy/proxy/proxy.rb