Example URL shortener written in Go
This is an example web service for getting short urls. This example was written for use during my introductory code talk for the Morgantown Codes tech meetup on August 10th 2017.
- Install Go from binaries available at the Go website.
- Use
go get github.com/kelcecil/shorturlor clone to your$GOPATH. - Do
go run *.go.
Just send a POST to the root with a JSON payload containing an attribute called "url":
{
"url": "http://github.com"
}You can use curl to easily add a URL from command line like so.
curl -X POST http://localhost:8080/ -H "Accept: application/json" -d '{"url":"http://github.com"}'The JSON payload in response will containing a JSON attribute called key that you'll use for your short url.
{
"key": "a"
}Just call the root like so using the key you received when submitting the URL.
curl -I -X GET http://localhost:8080/aYour output will look something like:
HTTP/1.1 307 Temporary Redirect
Location: http://github.com
Date: Thu, 10 Aug 2017 21:21:03 GMT
Content-Length: 50
Content-Type: text/html; charset=utf-8
Try it in a web browser for maximum effect.
This example is intended to lead into a workshop session where people try running the application and make small improvements to the code to try their hand at Go programming. A few possibilities (in ascending order of difficulty) are:
- Adding more logging. (Explore the
logstandard library package.) - Add a flag to configure the port to listen on. (Explore the
flagstandard library package.) - Add tests to test the HTTP endpoints. (Explore the
httpteststandard library package.) - Add benchmarks to evaluate the speed of the encoding. (Explore the
testingstandard library package.) - Write code to ensure that the symmetric key will never produce a curse word. (Think http://localhost:8080/butts)
- Implement a new storage backend for URLStorage. (sqlite3 might be a good option).
- Implement middleware to add rate limiting or metrics collection.
Absolutely.