lib/gumdrop/server.rb in gumdrop-0.3.10 vs lib/gumdrop/server.rb in gumdrop-0.4.0
- old
+ new
@@ -1,11 +1,10 @@
# Rework this to be nicer.. Extend Sintra::Base
require 'sinatra/base'
require 'logger'
-
module Gumdrop
class Server < Sinatra::Base
set :port, Gumdrop.config.port if Gumdrop.config.port
@@ -16,10 +15,52 @@
# get '/' do
# redirect '/index.html'
# end
Gumdrop.run dry_run:true, log:server_log
+
+ if Gumdrop.config.proxy_enabled
+ require 'gumdrop/proxy_handler'
+ Gumdrop.report 'Enabled proxy at /-proxy/*', :info
+ get '/-proxy/*' do
+ proxy_to= params[:splat][0]
+ proxy_parts= proxy_to.split('/')
+ host= proxy_parts.shift
+ path_info= "/#{proxy_parts.join('/')}"
+ #puts "HOST: #{host} PATH_INFO: #{path_info}"
+ opts={ :to=>host, :path_info=>path_info }
+ Gumdrop.handle_proxy opts, proxy_to, env
+ end
+ post '/-proxy/*' do
+ proxy_to= params[:splat][0]
+ proxy_parts= proxy_to.split('/')
+ host= proxy_parts.shift
+ path_info= "/#{proxy_parts.join('/')}"
+ #puts "HOST: #{host} PATH_INFO: #{path_info}"
+ opts={ :to=>host, :path_info=>path_info }
+ Gumdrop.handle_proxy opts, proxy_to, env
+ end
+ delete '/-proxy/*' do
+ proxy_to= params[:splat][0]
+ proxy_parts= proxy_to.split('/')
+ host= proxy_parts.shift
+ path_info= "/#{proxy_parts.join('/')}"
+ #puts "HOST: #{host} PATH_INFO: #{path_info}"
+ opts={ :to=>host, :path_info=>path_info }
+ Gumdrop.handle_proxy opts, proxy_to, env
+ end
+ put '/-proxy/*' do
+ proxy_to= params[:splat][0]
+ proxy_parts= proxy_to.split('/')
+ host= proxy_parts.shift
+ path_info= "/#{proxy_parts.join('/')}"
+ #puts "HOST: #{host} PATH_INFO: #{path_info}"
+ opts={ :to=>host, :path_info=>path_info }
+ Gumdrop.handle_proxy opts, proxy_to, env
+ end
+
+ end
get '/*' do
file_path= get_content_path params[:splat].join('/')
Gumdrop.log.info "[#{$$}] GET /#{params[:splat].join('/')}"
@@ -48,13 +89,30 @@
Gumdrop.log.info "[#{$$}] *Static: #{file_path}"
source_base_path= File.expand_path(Gumdrop.config.source_dir)
send_file File.join( source_base_path, file_path)
end
else
- Gumdrop.log.error "[#{$$}] *Missing: #{file_path}"
- puts "NOT FOUND: #{file_path}"
- "#{file_path} Not Found"
+ # uri_path= params[:splat].join('/')
+ # puts "LOOKING FOR: #{uri_path}"
+ # if uri_path =~ /^\-proxy\/(.*)$/
+ # uri= URI.parse "http://#{$1}"
+ #
+ # puts "PROXY TO: #{uri}"
+ #
+ # http = Net::HTTP.new(uri.host, uri.port)
+ # response = http.request(Net::HTTP::Get.new(uri.request_uri))
+ #
+ # #[response.code, response.body]
+ # #halt response.code, {}, response.body.to_s
+ # puts "Responded with: #{response.body.to_s}"
+ # #response
+ # [response.code.to_i, {}, response.body.to_s]
+ # else
+ Gumdrop.log.error "[#{$$}] *Missing: #{file_path}"
+ puts "NOT FOUND: #{file_path}"
+ "#{file_path} Not Found"
+ # end
end
end
def get_content_path(file_path)
@@ -69,10 +127,10 @@
keys.detect {|k| Gumdrop.site.has_key?(k) }
end
end
if Gumdrop.config.auto_run
- Gumdrop.run dry_run:true
+ #Gumdrop.run dry_run:true
run!
end
def self.start(opts={})
# Options