Inspired by Mathias Bynens' PHP URL Shortener, and triggered by a wish to learn Go, I wanted to try and see if I could build an URL shortener in Go.
- Redirect to your main website when no slug, or incorrect slug, is entered, e.g.
http://wiere.ma/
→http://samwierema.nl/
. - Generates short URLs using only
[a-z0-9]
characters. - Doesn’t create multiple short URLs when you try to shorten the same URL. In this case, the script will simply return the existing short URL for that long URL.
- Download the source code and install it using the
go install
command. - Use
database.sql
to create theredirect
table in a database of choice. - Create a config file in
/path/to/.go-url-shortener/
namedconfig.(json|yaml|toml)
. Useconfig-example.json
as a example. - Run the program as a daemon using one of the many methods: write a script for upstart, init, use daemonize, Supervisord, Circus or just plain old
nohup
. You can even start (and manage) it in ascreen
session. - Adding the following configuration to Apache (make sure you've got mod_proxy enabled):
<VirtualHost *:80>
ServerName your-short-domain.ext
ProxyPreserveHost on
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
You will find an example init script in the scripts
folder. To use, you must at least change the GOPATH line to point to your Go root path.
- Add tests