Skip to content

Instantly share code, notes, and snippets.

@JaciBrunning
Last active September 15, 2017 11:41
Show Gist options
  • Select an option

  • Save JaciBrunning/63a2c361222d1f9efd9dba355747bb2b to your computer and use it in GitHub Desktop.

Select an option

Save JaciBrunning/63a2c361222d1f9efd9dba355747bb2b to your computer and use it in GitHub Desktop.
Rackup Config for Subdomains and Virtual Servers
# config.ru
require 'rack'
$:.unshift File.dirname(__FILE__)
class SubdomainMiddleware
def initialize(app, sub, mod)
@app = app
@sub = sub
@mod = mod.new
end
def call(env)
domain = env["HTTP_HOST"][/^[^:]+/]
if @sub[0] =~ domain
@mod.call(env)
else
@app.call(env)
end
end
end
MODULES = {}
SUBDOMAINS = []
module Kernel
def define_webcore_module sym, clazz
MODULES[sym] = clazz
puts "Registering Module(#{sym})..."
end
def define_virtual_server domain_regex, module_sym, options={}
options = { priority: 50 }.merge(options)
SUBDOMAINS << [domain_regex, module_sym, options]
puts "Registering Subdomain(#{domain_regex.inspect}) -> Module(#{module_sym}) (#{options})..."
end
end
Dir['modules/**/library.rb'].each do |p| # Preload files that can be used in other modules (e.g. setting up cross-module APIs)
puts "Preloading Library #{p}..."
require_relative p
end
Dir['modules/**/module.rb'].each do |p|
puts "Loading #{p}..."
require_relative p
end
SUBDOMAINS.sort_by { |x| x[2][:priority] }.each do |sub|
use SubdomainMiddleware, sub, MODULES[sub[1]]
run Proc.new { |env| [404, {}, ['Not Found']] }
end
# modules/test/module.rb
require 'sinatra/base'
class API < Sinatra::Base
get '/' do
"<h1> Hello World </h1>"
end
end
class EverythingElse < Sinatra::Base
get '*' do
"<h1> All Else </h1>"
end
end
define_webcore_module :api, API
define_virtual_server /api\..*/i, :api
define_webcore_module :main, EverythingElse
define_virtual_server /.*/i, :main, priority: 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment