Skip to content

allisson/go-url-shortener

Repository files navigation

go-url-shortener

Build Status Go Report Card

Golang url shortener using clean code.

Features:

  • Multiple storage engine: Redis and MongoDB.
  • Multiple message format: JSON and Msgpack.

Run Tests

  1. Set envvars
export MONGODB_URL=mongodb://localhost/shortener
export MONGODB_TIMEOUT=30
export MONGODB_DATABASE=shortener
export REDIS_URL=redis://localhost:6379
  1. Run tests
make test

Run the server

  1. Set envvars

For redis storage:

export PORT=3000
export STORAGE_ENGINE=redis
export REDIS_URL=redis://localhost:6379

For mongo storage:

export PORT=3000
export STORAGE_ENGINE=mongo
export MONGODB_URL=mongodb://localhost/shortener
export MONGODB_TIMEOUT=30
export MONGODB_DATABASE=shortener
  1. Run the server

With binary:

make build
./shortener

Without binary:

make run

How to use

Create new short url:

curl -X POST http://localhost:3000 \
  -H 'Content-Type: application/json' \
  -d '{
	"url": "https://github.com/allisson"
}'

The response:

{
    "code": "YzaFuvFWg",
    "url": "https://github.com/allisson",
    "created_at": 1566827456
}

Get redirected:

curl -X GET http://localhost:3000/YzaFuvFWg

The response:

<a href="https://github.com/allisson">Moved Permanently</a>.

If you want to use msgpack format, change Content-Type header to application/x-msgpack.

Releases

No releases published

Packages

No packages published