A simple project developing a URL shortening service, primarily backend, using Golang.
To built a backend service using Go and it's frameworks, and to try out creating an containerised application off of that service to gain hands-on experience.
- Go
- Redis (for database)
- Fiber (for web services)
- Docker (for containerisation)
- Clone the repository.
git clone https://github.com/PMohanJ/go-url-shortener.git
-
Make sure you've all the Requirements/dependencies installed in your system.
-
Enter project directory.
cd go-url-shortener
- Now initiate the docker-compose file to start the application.
docker-compose up
After the container is up and running, you can make requests to the application at following port http://localhost:3000/
- Send a request with following syntax:
curl localhost:3000/api/v1 -H "Content-Type":"application/json" \
-d '{"url": "https://www.youtube.com/watch?v=R_fZjGm2OrM"}'
Response for above request looks as below
{
"url":"https://www.youtube.com/watch?v=R_fZjGm2OrM",
"short":"localhost:3000/dd2970",
"expiry":24,
"rate_limit":6,
"rate_limit_reset":12
}
- The application does even support
custom url
, for example:
curl localhost:3000/api/v1 -H "Content-Type":"application/json" \
-d '{"url": "https://www.youtube.com/watch?v=R_fZjGm2OrM", "short":"123@Mine"}'
Response to above command
{
"url":"https://www.youtube.com/watch?v=R_fZjGm2OrM",
"short":"localhost:3000/123@Mine",
"expiry":24,
"rate_limit":5,
"rate_limit_reset":8
}
- You can also set the
custom expiry time
for the URL, which actaully defaults to 24hr if not set:
curl localhost:3000/api/v1 -H "Content-Type":"application/json" \
-d '{"url": "https://www.youtube.com/watch?v=R_fZjGm2OrM", "expiry":10}'
Response to above command
{
"url":"https://www.youtube.com/watch?v=R_fZjGm2OrM",
"short":"localhost:3000/325800",
"expiry":10,
"rate_limit":4,
"rate_limit_reset":6
}
So, whenever you use the shortened URL, you'll will be redirected to original URL.