Shrinkit is a lightweight Ruby on Rails application that provides a minimal, secure URL shortening service. It stores long URLs and generates short, non-sequential short codes using Hashids (wrapped by EncodedService).
- Create shortened URLs from long URLs via a small web UI
- Short codes generated using a secret salt with the
hashidsgem (configurable via env var) - Validates URLs (protocol and host) with a custom
UrlValidator - Redirects short codes to the original long URL (with
allow_other_host) - Simple test suite using RSpec
Requirements
- Ruby 3.4.6 (see
.ruby-version) - Bundler
- SQLite (default development DB)
- Node/npm is not required (this app uses importmaps)
Local setup
# Install gems
bundle install
# Create and migrate the database
bin/rails db:create db:migrate
# Run the test suite
bundle exec rspec
# Start the dev server
bin/rails server
# then visit http://localhost:3000Docker (production-style image)
# Build (production Dockerfile)
docker build -t shrinkit .
# Run (ensure you provide RAILS_MASTER_KEY in production)
docker run -d -p 80:80 -e RAILS_MASTER_KEY=<your_master_key> --name shrinkit shrinkitImportant environment variables
MY_SECRET_KEY— Salt used byEncodedService(Hashids). Set this in production (do NOT use the dev fallback).ALPHABET— Optional custom alphabet for generated short codes.RAILS_MASTER_KEY— Required for running in production if credentials are encrypted.
EncodedService (short-code generation)
- Implemented in
app/services/encoded_service.rbusing thehashidsgem. - By default the service will use
MY_SECRET_KEYand a sensible alphabet; change via env vars if needed.
- Visit the root (
/) to submit a long URL. - After creating a URL you'll be redirected to a show page that displays the short URL.
- Short URL format:
http://<host>/<short_code>which internally maps to/urls/redirect/:short_codeand performs a redirect to the originallong_url.
API example (simple):
curl -X POST -d "url[long_url]=https://example.com" http://localhost:3000/urlsRun RSpec:
bundle exec rspecThe repository includes specs for models, controllers, and services.
- Rails:
~> 8.1.1 - Database:
sqlite3for development/test - Gems:
hashids,base62(helper utilities),brakemanandrubocopin dev/test - Model:
Urlwithlong_url:stringandshort_code:string(seedb/migrate/*_create_urls.rb) - Validation:
app/validators/url_validator.rbensures a proper URL format and protocol