The ShortURL API
provides clear endpoints to shorten, view, and delete short urls. It also provides a convenient endpoint to retrieve metrics on a specific short url (including total views, past 24 hours, and past week). For installation instructions, see the FAQs
section.
- Langauge: Golang
- Database: Redis
- Containerization: Docker / docker-compose
- Build: Github actions
- Gin-Gonic (Web framework)
- Go-Redis (Redis client for go)
- redismock (Go-Redis client meant for mocking in unit tests)
- xid (Used to generate short url identifiers)
- Short URLs must always be unique, and may never collide.
- API users must be able to retrieve short url metrics (these metrics can be easily modified, by changing the Metrics Configuration)
- Short URL ownership is controlled by the
employee_id
. This stands as an example how a real production environment could implement access control on certain routes that manage a short url. (Like metrics collection or deletion) - Optional short url expiration, by using an
expires
query parameter when creating a new short url. - All short urls are public, regardless of the creator. (via the
/v/:short_id
route) - Stored short urls must persist to disk
- GET /healthcheck
- GET /v/:short_id
- GET /short/new/:employee_id
- Query params:
- url=http://... (required, note: if no protocol is provided,
https
is used) - expires=5 (optional, in minutes)
- url=http://... (required, note: if no protocol is provided,
- Query params:
- GET /short/delete/:employee_id/:short_id
- GET /short/metrics/:employee_id/:short_id
- Create a short url that never expires:
http://localhost:8080/short/new/abc123?url=http://example.com
- Create a short url that expires after 30 minutes:
http://localhost:8080/short/new/abc123?url=http://example.com&expires=30
- Delete a short url:
http://localhost:8080/short/delete/abc123/short_url_id
- Fetch metrics for a short url:
http://localhost:8080/short/metrics/abc123/short_url_id
- View short url's long url:
http://localhost:8080/v/short_url_id
These endpoints are all defined as GET
endpoints. This makes it easy to use in the browser, but also useful for a developer to integrate.
In some of the above routes, employee_id
is listed as part of path. This path variable is a unique string that represents a specific user, allowing short urls to be owned by an employee_id
. When creating a new short url with the /short/new/:employee_id
route, the employee_id
may be any string. This will then be needed to reference metrics about the short url, and to delete the short url.
What are the required prerequisites?
The following items are required to use this API locally:
If you are attempting to run this API outside of a containerized environment, the following items are required:
How do I run it locally?
For local use, the following command can be run to spin up the API using docker-compose:
docker-compose up
How do I run tests?
To run the built in unit tests, the following script can be run from the root directory:
./run-tests.sh
If you are unable to run the above script, you may use the following to run the tests:
cd tests go test -v
How do I collect more metrics?
1. Open the Metrics Configuration json file.
2. Modify the periods object to collect customized metrics. Example addition which collects 1 hour of short url link counts
{ "1h": "past_hour" }