Skip to content
Permalink
Browse files

Adds support for fall through routes

  • Loading branch information...
captn3m0 committed Aug 22, 2015
1 parent cd72814 commit fa93a81d913368f8c47e62d654f9a14e16d6efc5
Showing with 44 additions and 3 deletions.
  1. +13 −0 README.md
  2. +17 −2 app.rb
  3. +4 −1 redirects.yml
  4. +10 −0 test.rb
@@ -7,6 +7,19 @@ your domains. The configuration is kept public on this github repository itself.

To add a DNS record, point your domain via a CNAME entry to `lightsaber.captnemo.in`.

You can also have fallthrough domain redirects
(thanks to @vivekprakash for the suggestion), by making the redirect a hash
with a `root` key set to the url prefix.

```yaml
# This is a fallthrough redirect
# so t.co/ev would redirect to twitter.com/ev
t.co:
root: https://twitter.com
# All goo.gl routes will forward to google.com, and not use fallthrough
goo.gl: https://google.com
```

Next, you will need to do the following:

1. Fork this repo
19 app.rb
@@ -3,11 +3,26 @@
require 'sinatra'
require 'yaml'

get '/' do
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
hostname = request.host
route = params[:splat][0]

YAML::load_file('redirects.yml').each do |code, zone|
if zone.has_key? hostname
redirect zone[hostname], code
url = get_url(zone[hostname], route)
if url
redirect url, code
else
halt 400, "Invalid configuration for #{hostname}"
end
end
end

@@ -1,6 +1,9 @@
---
301:
github.captnemo.in: https://github.com/captn3m0
# This is the base root for all requests
# github.captnemo.in/lightsaber => github.com/captn3m0/lightsaber
github.captnemo.in:
root: https://github.com/captn3m0
lightsaber.captnemo.in: https://github.com/captn3m0/lightsaber
302:
fb.captnemo.in: https://facebook.com/captn3m0
10 test.rb
@@ -18,6 +18,8 @@ def test_redirect_sections
def test_each_domain
@config.each do |section, zone|
zone.each do |domain, redirect|
url = get_url(redirect, "")
refute_nil url, "Invalid YAML config for #{domain}"
assert resolves_to_lightsaber(domain),
"DNS for #{domain} isn't setup yet. See README"
end
@@ -34,4 +36,12 @@ def resolves_to_lightsaber(domain)
end
flag
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
end

0 comments on commit fa93a81

Please sign in to comment.
You can’t perform that action at this time.