Simple URL shortener microservice powered by Sinatra and PostgreSQL.
First clone the repository:
git clone https://github.com/tegon/shorty.gitInside the application directory, build the Docker images running:
cd shorty
docker-compose buildCreate and start the containers:
docker-compose up -dWe need to restart the containers because the app container will fail to start until the database is created:
docker-compose restartYou might have to wait a minute before doing this.
Run the database migrations:
docker-compose run app rake db:migrateRestart (again) because Sequel needs to read the database schema on initialize:
docker-compose restartYou can access the application now at: http://localhost
All responses must be encoded in JSON and have the appropriate Content-Type header
POST /shorten
Content-Type: "application/json"
{
"url": "http://example.com",
"shortcode": "example"
}
| Attribute | Description |
|---|---|
| url | url to shorten |
| shortcode | preferential shortcode |
201 Created
Content-Type: "application/json"
{
"shortcode": :shortcode
}
A random shortcode is generated if none is requested, the generated short code has exactly 6 alpahnumeric characters and passes the following regexp: ^[0-9a-zA-Z_]{6}$.
| Error | Description |
|---|---|
| 400 | url is not present |
| 409 | The the desired shortcode is already in use. Shortcodes are case-sensitive. |
| 422 | The shortcode fails to meet the following regexp: ^[0-9a-zA-Z_]{4,}$. |
GET /:shortcode
Content-Type: "application/json"
| Attribute | Description |
|---|---|
| shortcode | url encoded shortcode |
302 response with the location header pointing to the shortened URL
HTTP/1.1 302 Found
Location: http://www.example.com
| Error | Description |
|---|---|
| 404 | The shortcode cannot be found in the system |
GET /:shortcode/stats
Content-Type: "application/json"
| Attribute | Description |
|---|---|
| shortcode | url encoded shortcode |
200 OK
Content-Type: "application/json"
{
"startDate": "2012-04-23T18:25:43.511Z",
"lastSeenDate": "2012-04-23T18:25:43.511Z",
"redirectCount": 1
}
| Attribute | Description |
|---|---|
| startDate | date when the url was encoded, conformant to ISO8601 |
| redirectCount | number of times the endpoint GET /shortcode was called |
| lastSeenDate | date of the last time the a redirect was issued, not present if redirectCount == 0 |
| Error | Description |
|---|---|
| 404 | The shortcode cannot be found in the system |