A sample URL shortener Golang app using Redis
short is a simple web based api. It will produce shortest hash possible for a url. It will use longer hashes only in case of collision.
For example for https://example.org is redis is empty it will return /5
short has has two entries:
/post which accepts a url and returns a short version of it.
{"url": "https://example.org"}
/TOKEN which based on request headers will return a json reply or will redirect the agent to the original url.
package main
import (
"log"
"net/http"
"github.com/kavehmz/short"
)
func main() {
site := short.Site{Host: "https://short.kaveh.me/"}
http.HandleFunc("/", site.Redirect)
http.HandleFunc("/post", site.Post)
log.Fatal(http.ListenAndServe(":8080", nil))
}
In example/main.go number if serving clients are limited as part of my "play with Golang" motivation.
You need to have a usable go installation. Then create a directory named site and follow the commands:
$ cd site
$ GOPATH=$PWD
$ go get -u github.com/kavehmz/short
This will install the project and all its dependencies.
Now to test it create a redis instance in redislabs.com and export your redis url like:
$ REDISURL="redis://:PASSWORD@pub-zone.redislabs.com:12919/0"
Then you can run the example
$ go run src/github.com/kavehmz/short/example/main.go
To test it use curl
$ # To send a url to be minimized
$ curl -v -X POST -H 'Content-Type: application/json' -d '{"url":"https://example.org"}' http://localhost:8080/post
$ # json query
$ curl -v -X GET -H 'Content-Type: application/json' http://localhost:8080/5
$ # normal redirect
$ curl http://localhost:8080/5