Golang url shortener using clean code.
Features:
- Multiple storage engine: Redis and MongoDB.
- Multiple message format: JSON and Msgpack.
- Set envvars
export MONGODB_URL=mongodb://localhost/shortener
export MONGODB_TIMEOUT=30
export MONGODB_DATABASE=shortener
export REDIS_URL=redis://localhost:6379
- Run tests
make test
- 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
- Run the server
With binary:
make build
./shortener
Without binary:
make run
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
.