Extracting subdomains in Sinatra
As part of my continuing education in Sinatra, I decided to learn how Sinatra handles subdomains. It turns out that neither Rack, nor Sinatra seem to have any native subdomain handling code. So I took a quick stab at it.
The easiest method I could come up with was simply to re-open the Rack::Request class and add a subdomain method. The method implementation is adapted from Rails, except with Rack style caching.
To test this you will need modify your hosts file with something like:
Then after running the above script, use the url http://sample.test.dev:4567. You should see just the word “sample” as the output.
I’m not sure that this is the best method, but seems to work for now.
The easiest method I could come up with was simply to re-open the Rack::Request class and add a subdomain method. The method implementation is adapted from Rails, except with Rack style caching.
To test this you will need modify your hosts file with something like:
127.0.0.1 sample.test.dev
Then after running the above script, use the url http://sample.test.dev:4567. You should see just the word “sample” as the output.
I’m not sure that this is the best method, but seems to work for now.