Skip to content

kavehmz/short

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

short

A sample URL shortener Golang app using Redis

Go Lang GoDoc Build Status Coverage Status Go Report Card


short

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.

Usage

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.

How to check

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

About

A sample URL shortener Golang app using Redis

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages