short

package module
v0.0.0-...-3f9eb13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2016 License: MIT Imports: 9 Imported by: 0

README

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

Documentation

Overview

Package 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.

short uses redis for its backned storage. You can set the redis url in command or in code.

$ REDISURL="redis://:PASSWORD@pub-zone.redislabs.com:12919/0"

Example of using this package is available in example directory.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Site

type Site struct {
	// Host will define the prefix to be used for generated short token for redirects.
	Host string
	// RedisURL defines the redis instance to use. If it is empty REDISURL environment variable will be used.
	RedisURL string
}

Site defines main attributes of shortener service.

func (Site) Post

func (site Site) Post(w http.ResponseWriter, r *http.Request)

Post Only accepts json data in the following format

{"url": "https://example.org"}

return results is always in the following format:

{"short":"https://short.me/ab", "error":""}

If there was any error short will be empty and error message will show.

func (Site) Redirect

func (site Site) Redirect(w http.ResponseWriter, r *http.Request)

Redirect will either return a json value in the following format:

{"url":"https://example.org", "error":""}

or it will do a 301, http redirect. Return action is based on headers.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL