Shorty
Simple link shortener
This project was created for training purposes to practice practical tasks and showcase my skills
Features
- Materialistic http rest api
- Creating short links
- View statistics on short link visits
- Redirecting users to real url addresses
Build
Clone the repository and compile the application
# Clone repo
git clone https://github.com/need-o/shorty.git
#Build app
cd shorty
go build
Usage
You can use environment variables to change the default address to the database and the server address
# Export environment variables
export SHORTY_DB_PATH=shorty.db
export SHORTY_ADDRESS=:1323
export SHORTY_MIGRATIONS_SOURCE=file://migrations
# Run shorty
./shorty
Creating a short link
curl -d '{"url": "https://example.com" }' -H "Content-Type: application/json" -X POST http://localhost:1323/api/shorty
{
"id":"sE5qwb",
"address":"localhost:1323/sE5qwb"
}
Follow the short link
curl http://localhost:1323/sE5qwb -v
* processing: http://localhost:1323/sE5qwb
* Trying [::1]:1323...
* Connected to localhost (::1) port 1323
> GET /sE5qwb HTTP/1.1
> Host: localhost:1323
> User-Agent: curl/8.2.1
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Location: https://example.com
< X-Request-Id: vwdoUkuTzQAQ6d65lGwTOatPipViaiYM
< Date: Mon, 28 Aug 2023 14:14:41 GMT
< Content-Length: 0
<
* Connection #0 to host localhost left intact
View visit statistics
curl http://localhost:1323/api/shorty/sE5qwb
{
"id": "sE5qwb",
"url": "https://example.com",
"visits": [
{
"shorty_id": "sE5qwb",
"referer": "",
"user_ip": "127.0.0.1",
"user_agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0",
"created_at": "2023-08-28T14:56:47.898469006+03:00",
"updated_at": "2023-08-28T14:56:47.898469006+03:00"
}
],
"created_at": "2023-08-28T14:23:31.413414468+03:00",
"updated_at": "2023-08-28T14:23:31.413414468+03:00"
}
Testing
To run tests, use the default go test
command:
go test ./...
License
Shorty released under GNU GPL license, refer LICENSE file.