If you follow the social media sites like Twitter and Google+, you’ll have noticed that rather than use the full URL we use a nice friendly shortened one like t.co/EvDSyOOPTH.
Wouldn’t be great to have your own shortened URLs inside your own domain?
Let’s review the basics first.
Programming Languages are just tools for us, but we need a safe, fast and “cross-platform” programming language to power our service.
Go is a rapidly growing open source programming language designed for building simple, fast, and reliable software. Take a look here which great companies use Go to power their services.
Extensive information about downloading & installing Go can be found here.
Maybe Windows or Mac OS X user?
The article does not contain an introduction to the language itself, if you’re a newcomer I recommend you to bookmark this article, learn the language’s fundamentals and come back later on.
Many articles have been written, in the past, that lead users not to use a web framework because they’re “bad”. I have to tell you that there is no such a thing, it always depends on the (web) framework that you’re going to use.In production level, usually, we don’t have the time to code everything that we wanna use in the big company’s applications. In short terms: good frameworks are helpful tools for any developer, company or startup and bad are waste of time, clear and simple.
You’ll need only two required dependencies and one optional:
Installing using go packages is a simple task, just open your terminal and execute the following commands:
$ go get -u github.com/kataras/iris$ go get -u github.com/etcd-io/bbolt$ go get -u github.com/satori/go.uuid
Good, if we’re all in the same page, it’s time to learn how we can create a URL Shortener server that will be easy to deploy and can be extended even more!
To create a short link, we generate a random string and use Bolt to store the original URL with our random string as the key. When a “GET ” request is issued to the shortened URL , we retrieve the original URL from Bolt. If such a value exists, we redirect, otherwise we respond with a corresponding message.
For the sake of simplicity let’s say that the project is located at $GOPATH/src/you/shortener directory and the package name is main.
Our front-end is ridiculous simple, it contains just an index page and its “style”, templates are located to ./templates
folder and the style at ./resources/css
folder.
Moving straight to the database level, we will create a simple implementation which will be able to save a shortened URL(key) and its original/full URL(value), retrieve a full URL based on the key and return the total number of all registered URLs to the database.
Let’s create our factory which will generate the shortened URLs for us!
We should create a main file which will connect and combine all the components to run an http server which will serve our small URL Shortener service.
And…. let’s build and run our application!
$ cd $GOPATH/src/github.com/myname/url-shortener$ go build # build$ ./url-shortener # run
Testing our apps is always a useful thing to do, therefore I’ll give you the first idea on how you can test these type of web applications with Iris, straight to the code below.
Running the test…
$ cd $GOPATH/src/github.com/myname/url-shortener$ go test -v # test
Share your thoughts about this post and let me know what awesome apps you’re planing to build with Go + Iris!
Thanks for taking the time to read the whole article, I admire you for your patience :)
The full source code is located here.If you have any further questions please feel free to leave a comment below or ask here!
I have updated the source code and the article’s links at 31 August of 2018 in order to replace the ( archived) “https://github.com/boltdb/bolt” with the new https://github.com/etcd-io/bbolt from CoreOS based on the https://github.com/etcd-io/bbolt/releases/tag/v1.3.1-etcd.7 (released at 29 August of 2018)