A fully functional URL shortener using MySQL as the storage server and Redis as the cache server.
The current components include:
- a web server for URL shortening and redirection
- regex to verify validity of a URL
- Base62 encoding to compress a long URL
- an external Redis cache server
- a local dev enviroment setup with
docker-compose
- automatic configuration management with a single
.env
file - logging
- docs generated by swagger
- request timeout control
- in-memory cache
- [TODO] integration test running on CI
- [TODO] IP based rate limiting
Docs are generated by gin-swagger. To access the docs, open your browser and go to the following link after launching the web server on your localhost.
http://127.0.0.1:8000/swagger/index.html
We provide two API endpoints: one for URL shortening and another for URL redirection.
POST /api/v1/data/shorten
- Request parameter: {longURL: string}
- Rreturn: shortURL
Example usage:
$ curl -iX POST 'http://localhost:8080/api/v1/data/shorten?longURL=www.google.com'
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Fri, 01 Dec 2023 05:10:33 GMT
Content-Length: 18
{"shortURL":"aA4"}
shortURL
: The request URI path
GET /api/v1/:shortURL
- Request parameter: no, using URI path binding instead
- Return: longURL for HTTP redirection
Example usage:
$ curl -i 'http://localhost:8080/api/v1/aA4'
HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=utf-8
Location: /api/v1/www.google.com
Date: Fri, 01 Dec 2023 05:11:54 GMT
Content-Length: 57
<a href="/api/v1/www.google.com">Moved Permanently</a>.
We use the command runner just as an alternative to makefile. Execute just help
to access a complete list of available commands. If you do not have just
installed, check the justfile for all receipts.
We use docker-compose
to set up local development pretty easily. It exposes a web server forwarded to host port 8080, a MySQL server forwarded to host port 3306, and a Redis server forwarded to host port 6379. To launch, simply run:
$ docker-compose up -d
Then you are ready to go.
To build a binary, run:
$ just build
To connect to the mysql server, run:
$ just mysql
To connect to the redis server, run:
$ just redis