A stateless app to handle all my domain redirects.
This application handles all incoming requests by looking at the database for a matching domain. If a domain is matched, it will send the user agent to the redirect_uri
with a 301.
The app is hosted on heroku, the mysql credentials are set via environment variables. When you add the ClearDB Add-on to your heroku app, it will set the environment variable on production to use your database. For more info see Setting up config vars. All db credentials are stored in process.env.CLEARDB_DATABASE_URL
. I used MySQL because ClearDB gives you several free backup strategies.
You need to add a domain to the Heroku app to be able to redirect it. Using the Heroku toolbelt you can do heroku domains:add mydomain.com
to have Heroku point that domain to your app. Domains are managed via a MySQL table.
-------------------------------------------------
| host | redirect_uri |
-------------------------------------------------
| mydomain.com | myotherdomain.com/landingpage |
| myapp.net | myappgallery.com/myapp |
-------------------------------------------------
You can find an SQL dump in rv-redirect_domains.sql
. I manage this table via Sequel Pro, there is no admin interface yet.
Currently I'm setting up my domains with three A records pointing to:
174.129.212.2
75.101.145.87
75.101.163.44
This is highly discouraged as it introduces a single point of failure on my side whenever Heroku decides to route their traffic differently. Read more about setting up custom domains here:
I will update this section once I find a setup that is more reliable.
- Node 0.4.7 (install via nvm)
- npm 1.0.x
- Heroku toolbelt
Create an Heroku app and get yourself familiar with the requirements for running Node on Heroku. Run $ npm install
to setup node-mysql. Start the app using Foreman, which is bundled with the Heroku toolbelt.
Add the db addon: $ heroku addons:add cleardb:ignite
. Run $ heroku config
to get your credentials so you can connect your local environment to it. Connect to the database with your favorite MySQL tool and import the sql structure. When starting the app, export the environment variable to be sure foreman picks it up:
$ CLEARDB_DATABASE_URL="mysql://USERNAME:PASSWORD@HOST/DATABASE" foreman start
$ git push heroku master
$ heroku ps:scale web=1
- Profit!
To see the app in action once it's in production, run:
$ heroku logs -p web.1 --tail
Whenever a domain is recognized and redirected, the app will print it to the logs.
- Refactor
- Add a counter per domain