Skip to content

Knuds1/url-shortener

Repository files navigation

URL Shortener

A simple URL shortener made with Ruby & Sinatra

Start the server

docker-compose up

Server is running on localhost:8080

Alternatively, run manually with

Environment variables

APP_ENV=production
NANOID_SIZE=11 # Length of short-URLs

REDIS_URL=redis:https://localhost:6379
DATABASE_HOST=localhost:5432
DATABASE_NAME=postgres
DATABASE_USER=postgres
DATABASE_PASSWORD=postgres

Startup

bundle install
bundle exec rake db:migrate
bundle exec puma -p 80

Development

Clone the repository and open the Dev Container in VSCode, run

bundle exec rake db:migrate
bundle exec puma

Development server will be forwarded to localhost:9292

Run tests

bundle exec rake spec:all

API Reference

Redirect

GET /{url}
Parameter Type Description
json "true"|"false" Return JSON response, otherwise redirects

JSON Response

{
  "exists": boolean,
  "target": string
}

The exists attribute is true if the URL exists.

The target attribute is the destination URL, to be used for redirecting.

NOTE: Status code 404 is returned if the URL does not exist.

Shorten URL

POST /
Parameter Type Description
response_html "true"|"false" Return HTML response (used by the frontend), otherwise JSON

JSON Response

{
  "url_full": string
  "url": string
  "base_url": string
  "target": string
}

The url_full attribute is the full short-URL, equivalent to {base_url}/{url}.

The url attribute is the resulting URL slug.

The base_url attribute is the protocol and domain part of the short-URL.

The target attribute is the destination URL, to be used for redirecting.

About

A simple URL shortener made with Ruby & Sinatra

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published