This service reduces long links from sites on the Internet. If you like the project or use it for any purpose, don't hesitate to give it a star on GitHub!
The entry point to the code is in cmd/url-shortener/url-shortener.go. The service has the following HTTP handlers:
- /shorten - use the POST method and x-www-form-urlencoded parameters url with a URL for shortening. Returns base62 code of the URL.
- /{code} - use GET method and substitute {code} with actual URL code received from the service like udXWFB. Returns the full URL from the code.
- /readiness - check if the database is ready and, if not, will return a 500 status.
- /liveness - return simple status info if the service is alive.
- Docker and docker-compose
- Git
- GNU Make
- Go (if you want to compile and run the service without docker)
-
Clone this repository in the current directory:
git clone https://github.com/illyasch/url-shortener
-
Build Docker images:
make image
-
Migrate and seed the database (uses Docker):
make seed
-
Start the local development environment (uses Docker):
make up
At this point, you should have the url-shortener service running. To confirm the state of the running Docker container, run
$ docker ps
from the docker container
make test
Shorten a URL
$ curl -i --data-urlencode "url=http://www.cnn.com" http://localhost:3000/shorten
HTTP/1.1 200 OK
Content-Type: application/json
Date: Sun, 12 Jun 2022 16:05:58 GMT
Content-Length: 17
{"code":"vdXWFB"}
Get a shortened URL with the code.
$ curl -i http://localhost:3000/vdXWFB
HTTP/1.1 200 OK
Content-Type: application/json
Date: Sun, 12 Jun 2022 16:07:48 GMT
Content-Length: 28
{"url":"http://www.cnn.com"}