An easily configurable, locally host-able URL shortener written in Golang. A (small) weekend project.
To run and deploy this application, you will need a few things:
To start the service (quickly), run the following:
make vendor
make docker-build
make docker-run
make db-create
make db-init
Name | Environment Variable | Values | Description |
---|---|---|---|
encoding | URL_LOGGER_ENCODING |
console or json |
The encoding of the logger. |
level | URL_LOGGER_LEVEL |
info , debug , warning , error , dpanic , panic , and fatal |
The level at which logs are recorded. |
REST host | URL_REST_HOST |
localhost or 0.0.0.0 |
The hostname of the REST server, should be localhost outside of container, and 0.0.0.0 inside a container. |
REST port | URL_REST_PORT |
8080 |
The port of the REST server. |
DB Host | URL_DATABASE_HOST |
localhost or database |
The host of the PostgreSQL instance, should be localhost outside of a container, and database inside a container. |
DB Port | URL_DATABASE_PORT |
5432 |
The port of the PostgreSQL instance, this is the default port. |
DB User | URL_DATABASE_USER |
postgres |
The user of the database, this is the default user. |
DB Password | URL_DATABASE_PASSWORD |
postgres |
The password to the user, this can be changed to whatever value is required. |
DB Name | URL_DATABASE_NAME |
urlshortener |
The name of the database within the PostgreSQL instance that the application uses. |
DB Capacity | URL_DATABASE_CAPACITY |
10 |
The complexity of generated URLs. Directly impacts the length of generated ID's. |
This project makes use of docker-compose, so the following commands can be used:
- To build the containers
docker-compose build --no-cache
- To run the containers
docker-compose up -d
- To bring the containers down
docker-compose down
- To view the logs of the containers
docker-compose logs -f [container-name]
This project uses PostgreSQL for data persistence. It saves any data from the instance to a ./db/
directory placed in the project directory.
- To create the database within the PostgreSQL instance:
make db-create
- Then, to populate the database with the
urls
table:
make db-init
- To wipe all data from the database:
make db-wipe
- Additionally, you can exec into the container using:
make db-exec
There is a known issue where the service is running, and the database is created for the first time, the application may not recognise the database as created. In this case, restart the application container. This can be done with:
docker-compose restart rest-server
It is what it says on the tin -- it's a basic URL shortener. Links have an expiry time added to them by default, but this can be altered easily here. I may add some more customisability in the future.
For both of the below scenarios, we assume that the application is running on localhost:8080
.
URL: POST http://localhost:8080/api/v1/shortener/
Request:
{
"url":"https://github.com/alexmerren"
}
Response:
{
"id": "WTWBY9Rr5C"
}
URL: GET http://localhost:8080/api/v1/shortener/WTWBY9Rr5C/
Request:
// This is deliberately blank...
Response:
{
"url": "https://github.com/alexmerren"
}