Refactors the config logic to separate class
This commit is contained in:
parent
9c221f8f5e
commit
618a5cc0c2
27
app.rb
27
app.rb
|
@ -1,30 +1,11 @@
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
|
require 'pp'
|
||||||
require 'bundler/setup'
|
require 'bundler/setup'
|
||||||
require 'sinatra'
|
require 'sinatra'
|
||||||
require 'yaml'
|
require 'yaml'
|
||||||
|
require_relative './lightsaber'
|
||||||
def get_url(domain_object, rel_route)
|
|
||||||
if domain_object.is_a? Hash
|
|
||||||
return domain_object['root'] + "/" + rel_route
|
|
||||||
elsif domain_object.is_a? String
|
|
||||||
return domain_object
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
get '/*' do
|
get '/*' do
|
||||||
hostname = request.host
|
saber = Lightsaber.new(request.url)
|
||||||
route = params[:splat][0]
|
saber.get_response.finish
|
||||||
|
|
||||||
YAML::load_file('redirects.yml').each do |code, zone|
|
|
||||||
if zone.has_key? hostname
|
|
||||||
url = get_url(zone[hostname], route)
|
|
||||||
if url
|
|
||||||
redirect url, code
|
|
||||||
else
|
|
||||||
halt 400, "Invalid configuration for #{hostname}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
halt 404, "#{hostname} hasn't been setup yet."
|
|
||||||
end
|
end
|
|
@ -0,0 +1,51 @@
|
||||||
|
require 'uri'
|
||||||
|
require 'rack/response'
|
||||||
|
|
||||||
|
# This is the class that resolves a given URL
|
||||||
|
# to what we need to redirect to
|
||||||
|
class Lightsaber
|
||||||
|
|
||||||
|
def initialize(url)
|
||||||
|
@url = URI(url)
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_url(domain_object, rel_route)
|
||||||
|
if domain_object.is_a? Hash
|
||||||
|
return domain_object['root'] + "/" + rel_route
|
||||||
|
elsif domain_object.is_a? String
|
||||||
|
return domain_object
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_response_from_yml
|
||||||
|
res = Rack::Response.new
|
||||||
|
|
||||||
|
YAML::load_file('redirects.yml').each do |code, zone|
|
||||||
|
if zone.has_key? @url.host.to_s
|
||||||
|
url = get_url(zone[@url.host], @url.path)
|
||||||
|
if url
|
||||||
|
res.redirect url, code
|
||||||
|
else
|
||||||
|
res.status = 400
|
||||||
|
res.body = "Invalid configuration for #{@url.host}"
|
||||||
|
end
|
||||||
|
return res
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def not_setup_response
|
||||||
|
res = Rack::Response.new
|
||||||
|
res.status = 404
|
||||||
|
res.body = "#{@url.host} hasn't been setup yet."
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def get_response
|
||||||
|
|
||||||
|
yml_res = get_response_from_yml
|
||||||
|
return yml_res unless yml_res.nil?
|
||||||
|
|
||||||
|
not_setup_response
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue