https://github.com/allisson/go-url-shortener
Golang url shortener using clean code
https://github.com/allisson/go-url-shortener
clean-architecture clean-code golang hexagonal-architecture
Last synced: 12 months ago
JSON representation
Golang url shortener using clean code
- Host: GitHub
- URL: https://github.com/allisson/go-url-shortener
- Owner: allisson
- License: mit
- Created: 2019-08-24T14:25:58.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-08-27T15:55:30.000Z (almost 7 years ago)
- Last Synced: 2025-06-06T04:09:03.238Z (about 1 year ago)
- Topics: clean-architecture, clean-code, golang, hexagonal-architecture
- Language: Go
- Size: 19.5 KB
- Stars: 10
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-url-shortener
[](https://travis-ci.org/allisson/go-url-shortener) [](https://goreportcard.com/report/github.com/allisson/go-url-shortener)
Golang url shortener using clean code.
Features:
* Multiple storage engine: Redis and MongoDB.
* Multiple message format: JSON and Msgpack.
## Run Tests
1. Set envvars
```bash
export MONGODB_URL=mongodb://localhost/shortener
export MONGODB_TIMEOUT=30
export MONGODB_DATABASE=shortener
export REDIS_URL=redis://localhost:6379
```
2. Run tests
```bash
make test
```
## Run the server
1. Set envvars
For redis storage:
```bash
export PORT=3000
export STORAGE_ENGINE=redis
export REDIS_URL=redis://localhost:6379
```
For mongo storage:
```bash
export PORT=3000
export STORAGE_ENGINE=mongo
export MONGODB_URL=mongodb://localhost/shortener
export MONGODB_TIMEOUT=30
export MONGODB_DATABASE=shortener
```
2. Run the server
With binary:
```bash
make build
./shortener
```
Without binary:
```bash
make run
```
## How to use
Create new short url:
```bash
curl -X POST http://localhost:3000 \
-H 'Content-Type: application/json' \
-d '{
"url": "https://github.com/allisson"
}'
```
The response:
```json
{
"code": "YzaFuvFWg",
"url": "https://github.com/allisson",
"created_at": 1566827456
}
```
Get redirected:
```bash
curl -X GET http://localhost:3000/YzaFuvFWg
```
The response:
```bash
Moved Permanently.
```
If you want to use msgpack format, change `Content-Type` header to `application/x-msgpack`.