Solution for shortening URLs using aliases. With this it's possible to:
- Shorten URLs
- Use a shortened URL to redirect to the original one
- Check the top 10 most visited URLs
The generator uses a seed that is based on the current UTC time to generate random numbers. Since the alias is a string composed of the english alphanumerical, the algorithm generates a number between 0 and the length of a string containing all upper and lower case letters of the alphabet + decimal digits. It then uses that as an index to get a character from the string. It repeats this process 6 times to get an alias that is easy to remember and also checks if it doesn't already exist to avoid conflicts.
You need to create a .env file. All the environment variables are available in the .env.example file located in the root of the project. You can also use its current values to ease the process.
The database is inside a Docker container. There's a couple of ways to run it using the docker-compose.yaml provided:
docker compose up -dmake upYou also need to run the database migration scripts. To do that you can either run the db/schema.sql
file manually or use dbmate to run the scritps inside db/migrations/
from the root folder (which is why there's a DBMATE_MIGRATIONS_TABLE environment variable).
Finally, you can run the API:
go run main.gogo build
./shortener-goYou can also run the API from within a Docker container:
docker compose --profile release upmake deployTo check the logs:
docker compose logs -tfmake logsTo stop the containers:
docker compose down --remove-orphansmake downTo run both unit and integration tests run:
go test ./...The solution comes with a client to consume the API. To use it, run the following commands
inside client/:
npm install
npm run devnpm install
npm run build
npm run previewYou may also use Yarn or PNPM, if desired
POST http://<HOST>:<PORT>/create?url=<string>&CUSTOM_ALIAS=<string?>
GET http://<HOST>:<PORT>/url/<alias>
GET http://<HOST>:<PORT>/most-visited


