├── .dockerignore ├── .gitattributes ├── .github ├── dependabot.yml └── workflows │ └── ci.yml ├── .gitignore ├── .kamal ├── hooks │ ├── docker-setup.sample │ ├── post-app-boot.sample │ ├── post-deploy.sample │ ├── post-proxy-reboot.sample │ ├── pre-app-boot.sample │ ├── pre-build.sample │ ├── pre-connect.sample │ ├── pre-deploy.sample │ └── pre-proxy-reboot.sample └── secrets ├── .rspec ├── .rubocop.yml ├── .ruby-version ├── Dockerfile ├── Gemfile ├── Gemfile.lock ├── LICENSE ├── README.md ├── Rakefile ├── app ├── assets │ ├── images │ │ └── .keep │ └── stylesheets │ │ └── application.css ├── controllers │ ├── api │ │ └── v1 │ │ │ └── links_controller.rb │ ├── application_controller.rb │ ├── concerns │ │ └── .keep │ └── redirects_controller.rb ├── helpers │ └── application_helper.rb ├── javascript │ ├── application.js │ └── controllers │ │ ├── application.js │ │ ├── hello_controller.js │ │ └── index.js ├── jobs │ └── application_job.rb ├── mailers │ └── application_mailer.rb ├── models │ ├── application_record.rb │ ├── concerns │ │ └── .keep │ └── link.rb └── views │ ├── layouts │ ├── application.html.erb │ ├── mailer.html.erb │ └── mailer.text.erb │ └── pwa │ ├── manifest.json.erb │ └── service-worker.js ├── bin ├── brakeman ├── bundle ├── dev ├── docker-entrypoint ├── importmap ├── jobs ├── kamal ├── rails ├── rake ├── rubocop ├── setup └── thrust ├── config.ru ├── config ├── application.rb ├── boot.rb ├── cable.yml ├── cache.yml ├── credentials.yml.enc ├── database.yml ├── deploy.yml ├── environment.rb ├── environments │ ├── development.rb │ ├── production.rb │ └── test.rb ├── importmap.rb ├── initializers │ ├── assets.rb │ ├── content_security_policy.rb │ ├── filter_parameter_logging.rb │ └── inflections.rb ├── locales │ └── en.yml ├── puma.rb ├── queue.yml ├── recurring.yml ├── routes.rb └── storage.yml ├── db ├── cable_schema.rb ├── cache_schema.rb ├── migrate │ └── 20250223174204_create_links.rb ├── queue_schema.rb ├── schema.rb └── seeds.rb ├── lib └── tasks │ └── .keep ├── log └── .keep ├── public ├── 400.html ├── 404.html ├── 406-unsupported-browser.html ├── 410.html ├── 422.html ├── 500.html ├── icon.png ├── icon.svg └── robots.txt ├── script └── .keep ├── spec ├── controllers │ ├── api │ │ └── v1 │ │ │ └── links_controller_spec.rb │ └── redirects_controller_spec.rb ├── models │ └── link_spec.rb ├── rails_helper.rb └── spec_helper.rb ├── storage └── .keep ├── test ├── application_system_test_case.rb ├── controllers │ └── .keep ├── fixtures │ ├── files │ │ └── .keep │ └── links.yml ├── helpers │ └── .keep ├── integration │ └── .keep ├── mailers │ └── .keep ├── models │ ├── .keep │ └── link_test.rb ├── system │ └── .keep └── test_helper.rb ├── tmp ├── .keep ├── pids │ └── .keep └── storage │ └── .keep └── vendor ├── .keep └── javascript └── .keep /.dockerignore: -------------------------------------------------------------------------------- 1 | # See https://docs.docker.com/engine/reference/builder/#dockerignore-file for more about ignoring files. 2 | 3 | # Ignore git directory. 4 | /.git/ 5 | /.gitignore 6 | 7 | # Ignore bundler config. 8 | /.bundle 9 | 10 | # Ignore all environment files. 11 | /.env* 12 | 13 | # Ignore all default key files. 14 | /config/master.key 15 | /config/credentials/*.key 16 | 17 | # Ignore all logfiles and tempfiles. 18 | /log/* 19 | /tmp/* 20 | !/log/.keep 21 | !/tmp/.keep 22 | 23 | # Ignore pidfiles, but keep the directory. 24 | /tmp/pids/* 25 | !/tmp/pids/.keep 26 | 27 | # Ignore storage (uploaded files in development and any SQLite databases). 28 | /storage/* 29 | !/storage/.keep 30 | /tmp/storage/* 31 | !/tmp/storage/.keep 32 | 33 | # Ignore assets. 34 | /node_modules/ 35 | /app/assets/builds/* 36 | !/app/assets/builds/.keep 37 | /public/assets 38 | 39 | # Ignore CI service files. 40 | /.github 41 | 42 | # Ignore Kamal files. 43 | /config/deploy*.yml 44 | /.kamal 45 | 46 | # Ignore development files 47 | /.devcontainer 48 | 49 | # Ignore Docker-related files 50 | /.dockerignore 51 | /Dockerfile* 52 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # See https://git-scm.com/docs/gitattributes for more about git attribute files. 2 | 3 | # Mark the database schema as having been generated. 4 | db/schema.rb linguist-generated 5 | 6 | # Mark any vendored files as having been vendored. 7 | vendor/* linguist-vendored 8 | config/credentials/*.yml.enc diff=rails_credentials 9 | config/credentials.yml.enc diff=rails_credentials 10 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: bundler 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | open-pull-requests-limit: 10 8 | - package-ecosystem: github-actions 9 | directory: "/" 10 | schedule: 11 | interval: daily 12 | open-pull-requests-limit: 10 13 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | pull_request: 5 | push: 6 | branches: [ main ] 7 | 8 | jobs: 9 | scan_ruby: 10 | runs-on: ubuntu-latest 11 | 12 | steps: 13 | - name: Checkout code 14 | uses: actions/checkout@v4 15 | 16 | - name: Set up Ruby 17 | uses: ruby/setup-ruby@v1 18 | with: 19 | ruby-version: .ruby-version 20 | bundler-cache: true 21 | 22 | - name: Scan for common Rails security vulnerabilities using static analysis 23 | # This runs Brakeman with an ignore config (in JSON) that skips the redirect warning. 24 | run: bin/brakeman --no-pager --skip-files app/controllers/redirects_controller.rb 25 | 26 | scan_js: 27 | runs-on: ubuntu-latest 28 | 29 | steps: 30 | - name: Checkout code 31 | uses: actions/checkout@v4 32 | 33 | - name: Set up Ruby 34 | uses: ruby/setup-ruby@v1 35 | with: 36 | ruby-version: .ruby-version 37 | bundler-cache: true 38 | 39 | - name: Scan for security vulnerabilities in JavaScript dependencies 40 | run: bin/importmap audit 41 | 42 | lint: 43 | runs-on: ubuntu-latest 44 | steps: 45 | - name: Checkout code 46 | uses: actions/checkout@v4 47 | 48 | - name: Set up Ruby 49 | uses: ruby/setup-ruby@v1 50 | with: 51 | ruby-version: .ruby-version 52 | bundler-cache: true 53 | 54 | - name: Lint code for consistent style 55 | run: bin/rubocop -f github 56 | 57 | test: 58 | runs-on: ubuntu-latest 59 | 60 | steps: 61 | - name: Install packages 62 | run: sudo apt-get update && sudo apt-get install --no-install-recommends -y build-essential git pkg-config google-chrome-stable 63 | 64 | - name: Checkout code 65 | uses: actions/checkout@v4 66 | 67 | - name: Set up Ruby 68 | uses: ruby/setup-ruby@v1 69 | with: 70 | ruby-version: .ruby-version 71 | bundler-cache: true 72 | 73 | - name: Run tests 74 | env: 75 | RAILS_ENV: test 76 | run: bin/rails db:test:prepare test test:system 77 | 78 | - name: Keep screenshots from failed system tests 79 | uses: actions/upload-artifact@v4 80 | if: failure() 81 | with: 82 | name: screenshots 83 | path: ${{ github.workspace }}/tmp/screenshots 84 | if-no-files-found: ignore 85 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files for more about ignoring files. 2 | # 3 | # Temporary files generated by your text editor or operating system 4 | # belong in git's global ignore instead: 5 | # `$XDG_CONFIG_HOME/git/ignore` or `~/.config/git/ignore` 6 | 7 | # Ignore bundler config. 8 | /.bundle 9 | 10 | # Ignore all environment files. 11 | /.env* 12 | 13 | # Ignore all logfiles and tempfiles. 14 | /log/* 15 | /tmp/* 16 | !/log/.keep 17 | !/tmp/.keep 18 | 19 | # Ignore pidfiles, but keep the directory. 20 | /tmp/pids/* 21 | !/tmp/pids/ 22 | !/tmp/pids/.keep 23 | 24 | # Ignore storage (uploaded files in development and any SQLite databases). 25 | /storage/* 26 | !/storage/.keep 27 | /tmp/storage/* 28 | !/tmp/storage/ 29 | !/tmp/storage/.keep 30 | 31 | /public/assets 32 | 33 | # Ignore master key for decrypting credentials and more. 34 | /config/master.key 35 | -------------------------------------------------------------------------------- /.kamal/hooks/docker-setup.sample: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "Docker set up on $KAMAL_HOSTS..." 4 | -------------------------------------------------------------------------------- /.kamal/hooks/post-app-boot.sample: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "Booted app version $KAMAL_VERSION on $KAMAL_HOSTS..." 4 | -------------------------------------------------------------------------------- /.kamal/hooks/post-deploy.sample: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # A sample post-deploy hook 4 | # 5 | # These environment variables are available: 6 | # KAMAL_RECORDED_AT 7 | # KAMAL_PERFORMER 8 | # KAMAL_VERSION 9 | # KAMAL_HOSTS 10 | # KAMAL_ROLE (if set) 11 | # KAMAL_DESTINATION (if set) 12 | # KAMAL_RUNTIME 13 | 14 | echo "$KAMAL_PERFORMER deployed $KAMAL_VERSION to $KAMAL_DESTINATION in $KAMAL_RUNTIME seconds" 15 | -------------------------------------------------------------------------------- /.kamal/hooks/post-proxy-reboot.sample: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "Rebooted kamal-proxy on $KAMAL_HOSTS" 4 | -------------------------------------------------------------------------------- /.kamal/hooks/pre-app-boot.sample: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "Booting app version $KAMAL_VERSION on $KAMAL_HOSTS..." 4 | -------------------------------------------------------------------------------- /.kamal/hooks/pre-build.sample: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # A sample pre-build hook 4 | # 5 | # Checks: 6 | # 1. We have a clean checkout 7 | # 2. A remote is configured 8 | # 3. The branch has been pushed to the remote 9 | # 4. The version we are deploying matches the remote 10 | # 11 | # These environment variables are available: 12 | # KAMAL_RECORDED_AT 13 | # KAMAL_PERFORMER 14 | # KAMAL_VERSION 15 | # KAMAL_HOSTS 16 | # KAMAL_ROLE (if set) 17 | # KAMAL_DESTINATION (if set) 18 | 19 | if [ -n "$(git status --porcelain)" ]; then 20 | echo "Git checkout is not clean, aborting..." >&2 21 | git status --porcelain >&2 22 | exit 1 23 | fi 24 | 25 | first_remote=$(git remote) 26 | 27 | if [ -z "$first_remote" ]; then 28 | echo "No git remote set, aborting..." >&2 29 | exit 1 30 | fi 31 | 32 | current_branch=$(git branch --show-current) 33 | 34 | if [ -z "$current_branch" ]; then 35 | echo "Not on a git branch, aborting..." >&2 36 | exit 1 37 | fi 38 | 39 | remote_head=$(git ls-remote $first_remote --tags $current_branch | cut -f1) 40 | 41 | if [ -z "$remote_head" ]; then 42 | echo "Branch not pushed to remote, aborting..." >&2 43 | exit 1 44 | fi 45 | 46 | if [ "$KAMAL_VERSION" != "$remote_head" ]; then 47 | echo "Version ($KAMAL_VERSION) does not match remote HEAD ($remote_head), aborting..." >&2 48 | exit 1 49 | fi 50 | 51 | exit 0 52 | -------------------------------------------------------------------------------- /.kamal/hooks/pre-connect.sample: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | # A sample pre-connect check 4 | # 5 | # Warms DNS before connecting to hosts in parallel 6 | # 7 | # These environment variables are available: 8 | # KAMAL_RECORDED_AT 9 | # KAMAL_PERFORMER 10 | # KAMAL_VERSION 11 | # KAMAL_HOSTS 12 | # KAMAL_ROLE (if set) 13 | # KAMAL_DESTINATION (if set) 14 | # KAMAL_RUNTIME 15 | 16 | hosts = ENV["KAMAL_HOSTS"].split(",") 17 | results = nil 18 | max = 3 19 | 20 | elapsed = Benchmark.realtime do 21 | results = hosts.map do |host| 22 | Thread.new do 23 | tries = 1 24 | 25 | begin 26 | Socket.getaddrinfo(host, 0, Socket::AF_UNSPEC, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME) 27 | rescue SocketError 28 | if tries < max 29 | puts "Retrying DNS warmup: #{host}" 30 | tries += 1 31 | sleep rand 32 | retry 33 | else 34 | puts "DNS warmup failed: #{host}" 35 | host 36 | end 37 | end 38 | 39 | tries 40 | end 41 | end.map(&:value) 42 | end 43 | 44 | retries = results.sum - hosts.size 45 | nopes = results.count { |r| r == max } 46 | 47 | puts "Prewarmed %d DNS lookups in %.2f sec: %d retries, %d failures" % [ hosts.size, elapsed, retries, nopes ] 48 | -------------------------------------------------------------------------------- /.kamal/hooks/pre-deploy.sample: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | # A sample pre-deploy hook 4 | # 5 | # Checks the Github status of the build, waiting for a pending build to complete for up to 720 seconds. 6 | # 7 | # Fails unless the combined status is "success" 8 | # 9 | # These environment variables are available: 10 | # KAMAL_RECORDED_AT 11 | # KAMAL_PERFORMER 12 | # KAMAL_VERSION 13 | # KAMAL_HOSTS 14 | # KAMAL_COMMAND 15 | # KAMAL_SUBCOMMAND 16 | # KAMAL_ROLE (if set) 17 | # KAMAL_DESTINATION (if set) 18 | 19 | # Only check the build status for production deployments 20 | if ENV["KAMAL_COMMAND"] == "rollback" || ENV["KAMAL_DESTINATION"] != "production" 21 | exit 0 22 | end 23 | 24 | require "bundler/inline" 25 | 26 | # true = install gems so this is fast on repeat invocations 27 | gemfile(true, quiet: true) do 28 | source "https://rubygems.org" 29 | 30 | gem "octokit" 31 | gem "faraday-retry" 32 | end 33 | 34 | MAX_ATTEMPTS = 72 35 | ATTEMPTS_GAP = 10 36 | 37 | def exit_with_error(message) 38 | $stderr.puts message 39 | exit 1 40 | end 41 | 42 | class GithubStatusChecks 43 | attr_reader :remote_url, :git_sha, :github_client, :combined_status 44 | 45 | def initialize 46 | @remote_url = `git config --get remote.origin.url`.strip.delete_prefix("https://github.com/") 47 | @git_sha = `git rev-parse HEAD`.strip 48 | @github_client = Octokit::Client.new(access_token: ENV["GITHUB_TOKEN"]) 49 | refresh! 50 | end 51 | 52 | def refresh! 53 | @combined_status = github_client.combined_status(remote_url, git_sha) 54 | end 55 | 56 | def state 57 | combined_status[:state] 58 | end 59 | 60 | def first_status_url 61 | first_status = combined_status[:statuses].find { |status| status[:state] == state } 62 | first_status && first_status[:target_url] 63 | end 64 | 65 | def complete_count 66 | combined_status[:statuses].count { |status| status[:state] != "pending"} 67 | end 68 | 69 | def total_count 70 | combined_status[:statuses].count 71 | end 72 | 73 | def current_status 74 | if total_count > 0 75 | "Completed #{complete_count}/#{total_count} checks, see #{first_status_url} ..." 76 | else 77 | "Build not started..." 78 | end 79 | end 80 | end 81 | 82 | 83 | $stdout.sync = true 84 | 85 | puts "Checking build status..." 86 | attempts = 0 87 | checks = GithubStatusChecks.new 88 | 89 | begin 90 | loop do 91 | case checks.state 92 | when "success" 93 | puts "Checks passed, see #{checks.first_status_url}" 94 | exit 0 95 | when "failure" 96 | exit_with_error "Checks failed, see #{checks.first_status_url}" 97 | when "pending" 98 | attempts += 1 99 | end 100 | 101 | exit_with_error "Checks are still pending, gave up after #{MAX_ATTEMPTS * ATTEMPTS_GAP} seconds" if attempts == MAX_ATTEMPTS 102 | 103 | puts checks.current_status 104 | sleep(ATTEMPTS_GAP) 105 | checks.refresh! 106 | end 107 | rescue Octokit::NotFound 108 | exit_with_error "Build status could not be found" 109 | end 110 | -------------------------------------------------------------------------------- /.kamal/hooks/pre-proxy-reboot.sample: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "Rebooting kamal-proxy on $KAMAL_HOSTS..." 4 | -------------------------------------------------------------------------------- /.kamal/secrets: -------------------------------------------------------------------------------- 1 | # Secrets defined here are available for reference under registry/password, env/secret, builder/secrets, 2 | # and accessories/*/env/secret in config/deploy.yml. All secrets should be pulled from either 3 | # password manager, ENV, or a file. DO NOT ENTER RAW CREDENTIALS HERE! This file needs to be safe for git. 4 | 5 | # Example of extracting secrets from 1password (or another compatible pw manager) 6 | # SECRETS=$(kamal secrets fetch --adapter 1password --account your-account --from Vault/Item KAMAL_REGISTRY_PASSWORD RAILS_MASTER_KEY) 7 | # KAMAL_REGISTRY_PASSWORD=$(kamal secrets extract KAMAL_REGISTRY_PASSWORD ${SECRETS}) 8 | # RAILS_MASTER_KEY=$(kamal secrets extract RAILS_MASTER_KEY ${SECRETS}) 9 | 10 | # Use a GITHUB_TOKEN if private repositories are needed for the image 11 | # GITHUB_TOKEN=$(gh config get -h github.com oauth_token) 12 | 13 | # Grab the registry password from ENV 14 | KAMAL_REGISTRY_PASSWORD=$KAMAL_REGISTRY_PASSWORD 15 | 16 | # Improve security by using a password manager. Never check config/master.key into git! 17 | RAILS_MASTER_KEY=$(cat config/master.key) 18 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --require spec_helper 2 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | # Omakase Ruby styling for Rails 2 | inherit_gem: { rubocop-rails-omakase: rubocop.yml } 3 | 4 | # Overwrite or add rules to create your own house style 5 | # 6 | # # Use `[a, [b, c]]` not `[ a, [ b, c ] ]` 7 | # Layout/SpaceInsideArrayLiteralBrackets: 8 | # Enabled: false 9 | -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | 3.4.2 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # syntax=docker/dockerfile:1 2 | # check=error=true 3 | 4 | # This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand: 5 | # docker build -t yll . 6 | # docker run -d -p 80:80 -e RAILS_MASTER_KEY= --name yll yll 7 | 8 | # For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html 9 | 10 | # Make sure RUBY_VERSION matches the Ruby version in .ruby-version 11 | ARG RUBY_VERSION=3.4.2 12 | FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base 13 | 14 | # Rails app lives here 15 | WORKDIR /rails 16 | 17 | # Install base packages 18 | RUN apt-get update -qq && \ 19 | apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 && \ 20 | rm -rf /var/lib/apt/lists /var/cache/apt/archives 21 | 22 | # Set production environment 23 | ENV RAILS_ENV="production" \ 24 | BUNDLE_DEPLOYMENT="1" \ 25 | BUNDLE_PATH="/usr/local/bundle" \ 26 | BUNDLE_WITHOUT="development" 27 | 28 | # Throw-away build stage to reduce size of final image 29 | FROM base AS build 30 | 31 | # Install packages needed to build gems 32 | RUN apt-get update -qq && \ 33 | apt-get install --no-install-recommends -y build-essential git pkg-config && \ 34 | rm -rf /var/lib/apt/lists /var/cache/apt/archives 35 | 36 | # Install application gems 37 | COPY Gemfile Gemfile.lock ./ 38 | RUN bundle install && \ 39 | rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \ 40 | bundle exec bootsnap precompile --gemfile 41 | 42 | # Copy application code 43 | COPY . . 44 | 45 | # Precompile bootsnap code for faster boot times 46 | RUN bundle exec bootsnap precompile app/ lib/ 47 | 48 | # Precompiling assets for production without requiring secret RAILS_MASTER_KEY 49 | RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile 50 | 51 | 52 | 53 | 54 | # Final stage for app image 55 | FROM base 56 | 57 | # Copy built artifacts: gems, application 58 | COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}" 59 | COPY --from=build /rails /rails 60 | 61 | # Run and own only the runtime files as a non-root user for security 62 | RUN groupadd --system --gid 1000 rails && \ 63 | useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \ 64 | chown -R rails:rails db log storage tmp 65 | USER 1000:1000 66 | 67 | # Entrypoint prepares the database. 68 | ENTRYPOINT ["/rails/bin/docker-entrypoint"] 69 | 70 | # Start server via Thruster by default, this can be overwritten at runtime 71 | EXPOSE 80 72 | CMD ["./bin/thrust", "./bin/rails", "server"] 73 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | ruby "3.4.2" 4 | 5 | # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" 6 | gem "rails", "~> 8.0.1" 7 | # The modern asset pipeline for Rails [https://github.com/rails/propshaft] 8 | gem "propshaft" 9 | # Use sqlite3 as the database for Active Record 10 | gem "sqlite3", ">= 2.1" 11 | # Use the Puma web server [https://github.com/puma/puma] 12 | gem "puma", ">= 5.0" 13 | # Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails] 14 | gem "importmap-rails" 15 | # Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev] 16 | gem "turbo-rails" 17 | # Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev] 18 | gem "stimulus-rails" 19 | # Build JSON APIs with ease [https://github.com/rails/jbuilder] 20 | gem "jbuilder" 21 | 22 | # Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword] 23 | # gem "bcrypt", "~> 3.1.7" 24 | 25 | # Windows does not include zoneinfo files, so bundle the tzinfo-data gem 26 | gem "tzinfo-data", platforms: %i[ windows jruby ] 27 | 28 | # Use the database-backed adapters for Rails.cache, Active Job, and Action Cable 29 | gem "solid_cache" 30 | gem "solid_queue" 31 | gem "solid_cable" 32 | 33 | # Reduces boot times through caching; required in config/boot.rb 34 | gem "bootsnap", require: false 35 | 36 | # Deploy this application anywhere as a Docker container [https://kamal-deploy.org] 37 | gem "kamal", require: false 38 | 39 | # Add HTTP asset caching/compression and X-Sendfile acceleration to Puma [https://github.com/basecamp/thruster/] 40 | gem "thruster", require: false 41 | 42 | # Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] 43 | # gem "image_processing", "~> 1.2" 44 | 45 | gem "addressable", "~> 2.8" 46 | gem "faraday", "~> 2.7" 47 | gem "bcrypt", "~> 3.1.16" 48 | 49 | group :development, :test do 50 | # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem 51 | gem "debug", platforms: %i[ mri windows ], require: "debug/prelude" 52 | 53 | # Static analysis for security vulnerabilities [https://brakemanscanner.org/] 54 | gem "brakeman", require: false 55 | 56 | # Omakase Ruby styling [https://github.com/rails/rubocop-rails-omakase/] 57 | gem "rubocop-rails-omakase", require: false 58 | 59 | gem "rspec-rails", "~> 7.1" 60 | end 61 | 62 | group :development do 63 | # Use console on exceptions pages [https://github.com/rails/web-console] 64 | gem "web-console" 65 | end 66 | 67 | group :test do 68 | # Use system testing [https://guides.rubyonrails.org/testing.html#system-testing] 69 | gem "capybara" 70 | gem "selenium-webdriver" 71 | end 72 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | actioncable (8.0.1) 5 | actionpack (= 8.0.1) 6 | activesupport (= 8.0.1) 7 | nio4r (~> 2.0) 8 | websocket-driver (>= 0.6.1) 9 | zeitwerk (~> 2.6) 10 | actionmailbox (8.0.1) 11 | actionpack (= 8.0.1) 12 | activejob (= 8.0.1) 13 | activerecord (= 8.0.1) 14 | activestorage (= 8.0.1) 15 | activesupport (= 8.0.1) 16 | mail (>= 2.8.0) 17 | actionmailer (8.0.1) 18 | actionpack (= 8.0.1) 19 | actionview (= 8.0.1) 20 | activejob (= 8.0.1) 21 | activesupport (= 8.0.1) 22 | mail (>= 2.8.0) 23 | rails-dom-testing (~> 2.2) 24 | actionpack (8.0.1) 25 | actionview (= 8.0.1) 26 | activesupport (= 8.0.1) 27 | nokogiri (>= 1.8.5) 28 | rack (>= 2.2.4) 29 | rack-session (>= 1.0.1) 30 | rack-test (>= 0.6.3) 31 | rails-dom-testing (~> 2.2) 32 | rails-html-sanitizer (~> 1.6) 33 | useragent (~> 0.16) 34 | actiontext (8.0.1) 35 | actionpack (= 8.0.1) 36 | activerecord (= 8.0.1) 37 | activestorage (= 8.0.1) 38 | activesupport (= 8.0.1) 39 | globalid (>= 0.6.0) 40 | nokogiri (>= 1.8.5) 41 | actionview (8.0.1) 42 | activesupport (= 8.0.1) 43 | builder (~> 3.1) 44 | erubi (~> 1.11) 45 | rails-dom-testing (~> 2.2) 46 | rails-html-sanitizer (~> 1.6) 47 | activejob (8.0.1) 48 | activesupport (= 8.0.1) 49 | globalid (>= 0.3.6) 50 | activemodel (8.0.1) 51 | activesupport (= 8.0.1) 52 | activerecord (8.0.1) 53 | activemodel (= 8.0.1) 54 | activesupport (= 8.0.1) 55 | timeout (>= 0.4.0) 56 | activestorage (8.0.1) 57 | actionpack (= 8.0.1) 58 | activejob (= 8.0.1) 59 | activerecord (= 8.0.1) 60 | activesupport (= 8.0.1) 61 | marcel (~> 1.0) 62 | activesupport (8.0.1) 63 | base64 64 | benchmark (>= 0.3) 65 | bigdecimal 66 | concurrent-ruby (~> 1.0, >= 1.3.1) 67 | connection_pool (>= 2.2.5) 68 | drb 69 | i18n (>= 1.6, < 2) 70 | logger (>= 1.4.2) 71 | minitest (>= 5.1) 72 | securerandom (>= 0.3) 73 | tzinfo (~> 2.0, >= 2.0.5) 74 | uri (>= 0.13.1) 75 | addressable (2.8.7) 76 | public_suffix (>= 2.0.2, < 7.0) 77 | ast (2.4.2) 78 | base64 (0.2.0) 79 | bcrypt (3.1.20) 80 | bcrypt_pbkdf (1.1.1) 81 | benchmark (0.4.0) 82 | bigdecimal (3.1.9) 83 | bindex (0.8.1) 84 | bootsnap (1.18.4) 85 | msgpack (~> 1.2) 86 | brakeman (7.0.0) 87 | racc 88 | builder (3.3.0) 89 | capybara (3.40.0) 90 | addressable 91 | matrix 92 | mini_mime (>= 0.1.3) 93 | nokogiri (~> 1.11) 94 | rack (>= 1.6.0) 95 | rack-test (>= 0.6.3) 96 | regexp_parser (>= 1.5, < 3.0) 97 | xpath (~> 3.2) 98 | concurrent-ruby (1.3.5) 99 | connection_pool (2.5.0) 100 | crass (1.0.6) 101 | date (3.4.1) 102 | debug (1.10.0) 103 | irb (~> 1.10) 104 | reline (>= 0.3.8) 105 | diff-lcs (1.6.0) 106 | dotenv (3.1.7) 107 | drb (2.2.1) 108 | ed25519 (1.3.0) 109 | erubi (1.13.1) 110 | et-orbi (1.2.11) 111 | tzinfo 112 | faraday (2.12.2) 113 | faraday-net_http (>= 2.0, < 3.5) 114 | json 115 | logger 116 | faraday-net_http (3.4.0) 117 | net-http (>= 0.5.0) 118 | fugit (1.11.1) 119 | et-orbi (~> 1, >= 1.2.11) 120 | raabro (~> 1.4) 121 | globalid (1.2.1) 122 | activesupport (>= 6.1) 123 | i18n (1.14.7) 124 | concurrent-ruby (~> 1.0) 125 | importmap-rails (2.1.0) 126 | actionpack (>= 6.0.0) 127 | activesupport (>= 6.0.0) 128 | railties (>= 6.0.0) 129 | io-console (0.8.0) 130 | irb (1.15.1) 131 | pp (>= 0.6.0) 132 | rdoc (>= 4.0.0) 133 | reline (>= 0.4.2) 134 | jbuilder (2.13.0) 135 | actionview (>= 5.0.0) 136 | activesupport (>= 5.0.0) 137 | json (2.10.1) 138 | kamal (2.5.2) 139 | activesupport (>= 7.0) 140 | base64 (~> 0.2) 141 | bcrypt_pbkdf (~> 1.0) 142 | concurrent-ruby (~> 1.2) 143 | dotenv (~> 3.1) 144 | ed25519 (~> 1.2) 145 | net-ssh (~> 7.3) 146 | sshkit (>= 1.23.0, < 2.0) 147 | thor (~> 1.3) 148 | zeitwerk (>= 2.6.18, < 3.0) 149 | language_server-protocol (3.17.0.4) 150 | lint_roller (1.1.0) 151 | logger (1.6.6) 152 | loofah (2.24.0) 153 | crass (~> 1.0.2) 154 | nokogiri (>= 1.12.0) 155 | mail (2.8.1) 156 | mini_mime (>= 0.1.1) 157 | net-imap 158 | net-pop 159 | net-smtp 160 | marcel (1.0.4) 161 | matrix (0.4.2) 162 | mini_mime (1.1.5) 163 | minitest (5.25.4) 164 | msgpack (1.8.0) 165 | net-http (0.6.0) 166 | uri 167 | net-imap (0.5.6) 168 | date 169 | net-protocol 170 | net-pop (0.1.2) 171 | net-protocol 172 | net-protocol (0.2.2) 173 | timeout 174 | net-scp (4.1.0) 175 | net-ssh (>= 2.6.5, < 8.0.0) 176 | net-sftp (4.0.0) 177 | net-ssh (>= 5.0.0, < 8.0.0) 178 | net-smtp (0.5.1) 179 | net-protocol 180 | net-ssh (7.3.0) 181 | nio4r (2.7.4) 182 | nokogiri (1.18.3-aarch64-linux-gnu) 183 | racc (~> 1.4) 184 | nokogiri (1.18.3-aarch64-linux-musl) 185 | racc (~> 1.4) 186 | nokogiri (1.18.3-arm-linux-gnu) 187 | racc (~> 1.4) 188 | nokogiri (1.18.3-arm-linux-musl) 189 | racc (~> 1.4) 190 | nokogiri (1.18.3-arm64-darwin) 191 | racc (~> 1.4) 192 | nokogiri (1.18.3-x86_64-linux-gnu) 193 | racc (~> 1.4) 194 | nokogiri (1.18.3-x86_64-linux-musl) 195 | racc (~> 1.4) 196 | ostruct (0.6.1) 197 | parallel (1.26.3) 198 | parser (3.3.7.1) 199 | ast (~> 2.4.1) 200 | racc 201 | pp (0.6.2) 202 | prettyprint 203 | prettyprint (0.2.0) 204 | propshaft (1.1.0) 205 | actionpack (>= 7.0.0) 206 | activesupport (>= 7.0.0) 207 | rack 208 | railties (>= 7.0.0) 209 | psych (5.2.3) 210 | date 211 | stringio 212 | public_suffix (6.0.1) 213 | puma (6.6.0) 214 | nio4r (~> 2.0) 215 | raabro (1.4.0) 216 | racc (1.8.1) 217 | rack (3.1.10) 218 | rack-session (2.1.0) 219 | base64 (>= 0.1.0) 220 | rack (>= 3.0.0) 221 | rack-test (2.2.0) 222 | rack (>= 1.3) 223 | rackup (2.2.1) 224 | rack (>= 3) 225 | rails (8.0.1) 226 | actioncable (= 8.0.1) 227 | actionmailbox (= 8.0.1) 228 | actionmailer (= 8.0.1) 229 | actionpack (= 8.0.1) 230 | actiontext (= 8.0.1) 231 | actionview (= 8.0.1) 232 | activejob (= 8.0.1) 233 | activemodel (= 8.0.1) 234 | activerecord (= 8.0.1) 235 | activestorage (= 8.0.1) 236 | activesupport (= 8.0.1) 237 | bundler (>= 1.15.0) 238 | railties (= 8.0.1) 239 | rails-dom-testing (2.2.0) 240 | activesupport (>= 5.0.0) 241 | minitest 242 | nokogiri (>= 1.6) 243 | rails-html-sanitizer (1.6.2) 244 | loofah (~> 2.21) 245 | nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0) 246 | railties (8.0.1) 247 | actionpack (= 8.0.1) 248 | activesupport (= 8.0.1) 249 | irb (~> 1.13) 250 | rackup (>= 1.0.0) 251 | rake (>= 12.2) 252 | thor (~> 1.0, >= 1.2.2) 253 | zeitwerk (~> 2.6) 254 | rainbow (3.1.1) 255 | rake (13.2.1) 256 | rdoc (6.12.0) 257 | psych (>= 4.0.0) 258 | regexp_parser (2.10.0) 259 | reline (0.6.0) 260 | io-console (~> 0.5) 261 | rexml (3.4.1) 262 | rspec-core (3.13.3) 263 | rspec-support (~> 3.13.0) 264 | rspec-expectations (3.13.3) 265 | diff-lcs (>= 1.2.0, < 2.0) 266 | rspec-support (~> 3.13.0) 267 | rspec-mocks (3.13.2) 268 | diff-lcs (>= 1.2.0, < 2.0) 269 | rspec-support (~> 3.13.0) 270 | rspec-rails (7.1.1) 271 | actionpack (>= 7.0) 272 | activesupport (>= 7.0) 273 | railties (>= 7.0) 274 | rspec-core (~> 3.13) 275 | rspec-expectations (~> 3.13) 276 | rspec-mocks (~> 3.13) 277 | rspec-support (~> 3.13) 278 | rspec-support (3.13.2) 279 | rubocop (1.72.2) 280 | json (~> 2.3) 281 | language_server-protocol (~> 3.17.0.2) 282 | lint_roller (~> 1.1.0) 283 | parallel (~> 1.10) 284 | parser (>= 3.3.0.2) 285 | rainbow (>= 2.2.2, < 4.0) 286 | regexp_parser (>= 2.9.3, < 3.0) 287 | rubocop-ast (>= 1.38.0, < 2.0) 288 | ruby-progressbar (~> 1.7) 289 | unicode-display_width (>= 2.4.0, < 4.0) 290 | rubocop-ast (1.38.0) 291 | parser (>= 3.3.1.0) 292 | rubocop-minitest (0.37.1) 293 | lint_roller (~> 1.1) 294 | rubocop (>= 1.72.1, < 2.0) 295 | rubocop-ast (>= 1.38.0, < 2.0) 296 | rubocop-performance (1.24.0) 297 | lint_roller (~> 1.1) 298 | rubocop (>= 1.72.1, < 2.0) 299 | rubocop-ast (>= 1.38.0, < 2.0) 300 | rubocop-rails (2.30.1) 301 | activesupport (>= 4.2.0) 302 | lint_roller (~> 1.1) 303 | rack (>= 1.1) 304 | rubocop (>= 1.72.1, < 2.0) 305 | rubocop-ast (>= 1.38.0, < 2.0) 306 | rubocop-rails-omakase (1.0.0) 307 | rubocop 308 | rubocop-minitest 309 | rubocop-performance 310 | rubocop-rails 311 | ruby-progressbar (1.13.0) 312 | rubyzip (2.4.1) 313 | securerandom (0.4.1) 314 | selenium-webdriver (4.29.1) 315 | base64 (~> 0.2) 316 | logger (~> 1.4) 317 | rexml (~> 3.2, >= 3.2.5) 318 | rubyzip (>= 1.2.2, < 3.0) 319 | websocket (~> 1.0) 320 | solid_cable (3.0.7) 321 | actioncable (>= 7.2) 322 | activejob (>= 7.2) 323 | activerecord (>= 7.2) 324 | railties (>= 7.2) 325 | solid_cache (1.0.7) 326 | activejob (>= 7.2) 327 | activerecord (>= 7.2) 328 | railties (>= 7.2) 329 | solid_queue (1.1.3) 330 | activejob (>= 7.1) 331 | activerecord (>= 7.1) 332 | concurrent-ruby (>= 1.3.1) 333 | fugit (~> 1.11.0) 334 | railties (>= 7.1) 335 | thor (~> 1.3.1) 336 | sqlite3 (2.6.0-aarch64-linux-gnu) 337 | sqlite3 (2.6.0-aarch64-linux-musl) 338 | sqlite3 (2.6.0-arm-linux-gnu) 339 | sqlite3 (2.6.0-arm-linux-musl) 340 | sqlite3 (2.6.0-arm64-darwin) 341 | sqlite3 (2.6.0-x86_64-linux-gnu) 342 | sqlite3 (2.6.0-x86_64-linux-musl) 343 | sshkit (1.24.0) 344 | base64 345 | logger 346 | net-scp (>= 1.1.2) 347 | net-sftp (>= 2.1.2) 348 | net-ssh (>= 2.8.0) 349 | ostruct 350 | stimulus-rails (1.3.4) 351 | railties (>= 6.0.0) 352 | stringio (3.1.5) 353 | thor (1.3.2) 354 | thruster (0.1.11) 355 | thruster (0.1.11-aarch64-linux) 356 | thruster (0.1.11-arm64-darwin) 357 | thruster (0.1.11-x86_64-linux) 358 | timeout (0.4.3) 359 | turbo-rails (2.0.11) 360 | actionpack (>= 6.0.0) 361 | railties (>= 6.0.0) 362 | tzinfo (2.0.6) 363 | concurrent-ruby (~> 1.0) 364 | unicode-display_width (3.1.4) 365 | unicode-emoji (~> 4.0, >= 4.0.4) 366 | unicode-emoji (4.0.4) 367 | uri (1.0.2) 368 | useragent (0.16.11) 369 | web-console (4.2.1) 370 | actionview (>= 6.0.0) 371 | activemodel (>= 6.0.0) 372 | bindex (>= 0.4.0) 373 | railties (>= 6.0.0) 374 | websocket (1.2.11) 375 | websocket-driver (0.7.7) 376 | base64 377 | websocket-extensions (>= 0.1.0) 378 | websocket-extensions (0.1.5) 379 | xpath (3.2.0) 380 | nokogiri (~> 1.8) 381 | zeitwerk (2.7.2) 382 | 383 | PLATFORMS 384 | aarch64-linux 385 | aarch64-linux-gnu 386 | aarch64-linux-musl 387 | arm-linux-gnu 388 | arm-linux-musl 389 | arm64-darwin-24 390 | x86_64-linux 391 | x86_64-linux-gnu 392 | x86_64-linux-musl 393 | 394 | DEPENDENCIES 395 | addressable (~> 2.8) 396 | bcrypt (~> 3.1.16) 397 | bootsnap 398 | brakeman 399 | capybara 400 | debug 401 | faraday (~> 2.7) 402 | importmap-rails 403 | jbuilder 404 | kamal 405 | propshaft 406 | puma (>= 5.0) 407 | rails (~> 8.0.1) 408 | rspec-rails (~> 7.1) 409 | rubocop-rails-omakase 410 | selenium-webdriver 411 | solid_cable 412 | solid_cache 413 | solid_queue 414 | sqlite3 (>= 2.1) 415 | stimulus-rails 416 | thruster 417 | turbo-rails 418 | tzinfo-data 419 | web-console 420 | 421 | RUBY VERSION 422 | ruby 3.4.2p28 423 | 424 | BUNDLED WITH 425 | 2.6.2 426 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Davide Santangelo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # YLL - Your Link Shortener 2 | 3 | [![Ruby](https://img.shields.io/badge/Ruby-3.4.0-red)](https://www.ruby-lang.org/) 4 | [![Rails](https://img.shields.io/badge/Rails-8.0.0-red)](https://rubyonrails.org/) 5 | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) 6 | 7 | YLL is a powerful, lightweight URL shortener built with Ruby on Rails. Create and manage shortened URLs with advanced features like expiration dates and password protection. 8 | 9 | ## 🚀 Features 10 | 11 | - **Short Links**: Generate unique 8-character codes for any URL 12 | - **Expiration Control**: Set optional expiration times for temporary links 13 | - **Security**: Password-protect links with HTTP Basic Authentication 14 | - **Analytics**: Track clicks for each shortened URL 15 | - **API Support**: Full JSON API for programmatic access 16 | 17 | ## 📋 Requirements 18 | 19 | - Ruby 3.0+ 20 | - Rails 8.0+ 21 | - PostgreSQL (recommended) 22 | 23 | ## 🛠️ Installation 24 | 25 | ```bash 26 | # Clone the repository 27 | git clone https://github.com/davidesantangelo/yll.git 28 | 29 | # Navigate to the project directory 30 | cd yll 31 | 32 | # Install dependencies 33 | bundle install 34 | 35 | # Set up the database 36 | rails db:create 37 | rails db:migrate 38 | 39 | # Start the server 40 | rails server 41 | ``` 42 | 43 | ## 📝 Configuration 44 | 45 | Create a `.env` file in the root directory with the following variables: 46 | 47 | ``` 48 | DATABASE_URL=postgres://username:password@localhost/yll_development 49 | HOST_URL=http://localhost:3000 50 | ``` 51 | 52 | ## 🔍 Usage 53 | 54 | ### Creating a Shortened URL 55 | 56 | #### Via Web Interface 57 | Visit `http://localhost:3000` and use the web form to create a new shortened link. 58 | 59 | #### Via API 60 | ```bash 61 | curl -X POST "http://localhost:3000/api/v1/links" \ 62 | -H "Content-Type: application/json" \ 63 | -d '{ 64 | "url": "https://example.com", 65 | "password": "optional_password", 66 | "expires_at": "2025-12-31T23:59:59Z" 67 | }' 68 | ``` 69 | 70 | ### Accessing a Shortened URL 71 | 72 | Simply visit the generated short URL: 73 | ``` 74 | http://localhost:3000/r/{code} 75 | ``` 76 | 77 | ### Using Password-Protected Links 78 | 79 | When a link is password-protected, the browser will prompt for credentials: 80 | - **Username**: The short code 81 | - **Password**: The password you set when creating the link 82 | 83 | Via cURL: 84 | ```bash 85 | curl -u "{code}:{password}" http://localhost:3000/r/{code} 86 | ``` 87 | 88 | ## 🔌 API Reference 89 | 90 | ### Create a Shortened Link 91 | 92 | ```http 93 | POST /api/v1/links 94 | ``` 95 | 96 | **Request Body:** 97 | ```json 98 | { 99 | "url": "https://example.com", 100 | "password": "mysecurepassword", // Optional 101 | "expires_at": "2025-12-31T23:59:59Z" // Optional 102 | } 103 | ``` 104 | 105 | **Response:** 106 | ```json 107 | { 108 | "original_url": "https://example.com", 109 | "short_url": "http://localhost:3000/r/Abc12345", 110 | "created_at": "2023-01-15T12:34:56Z", 111 | "expires_at": "2025-12-31T23:59:59Z", 112 | "code": "Abc12345", 113 | "clicks": 0 114 | } 115 | ``` 116 | 117 | ### Retrieve Link Information 118 | 119 | ```http 120 | GET /api/v1/links/{code} 121 | ``` 122 | 123 | **Response:** 124 | ```json 125 | { 126 | "original_url": "https://example.com", 127 | "short_url": "http://localhost:3000/r/Abc12345", 128 | "created_at": "2023-01-15T12:34:56Z", 129 | "expires_at": "2025-12-31T23:59:59Z", 130 | "code": "Abc12345", 131 | "clicks": 42 132 | } 133 | ``` 134 | 135 | ## 🧪 Development 136 | 137 | ```bash 138 | # Run tests 139 | rspec 140 | 141 | # Run linting 142 | rubocop 143 | ``` 144 | 145 | ## 🚢 Deployment with Kamal 146 | 147 | YLL supports deployment with [Kamal](https://kamal-deploy.org/), a deployment tool for Ruby on Rails applications that uses Docker and SSH. 148 | 149 | ### Prerequisites 150 | 151 | - Docker installed locally 152 | - SSH access to your production server 153 | - Docker installed on your production server 154 | 155 | ### Configuration 156 | 157 | 1. Install the Kamal gem: 158 | ```bash 159 | gem install kamal 160 | ``` 161 | 162 | 2. Initialize Kamal in your project: 163 | ```bash 164 | kamal init 165 | ``` 166 | 167 | 3. Edit the generated `config/deploy.yml` file: 168 | ```yaml 169 | service: yll 170 | image: username/yll 171 | registry: 172 | username: registry_username 173 | password: 174 | - KAMAL_REGISTRY_PASSWORD 175 | servers: 176 | web: 177 | hosts: 178 | - your-server-ip 179 | labels: 180 | traefik.http.routers.yll.rule: Host(`yll.yourdomain.com`) 181 | env: 182 | clear: 183 | DATABASE_URL: postgres://username:password@db-host/yll_production 184 | RAILS_ENV: production 185 | HOST_URL: https://yll.yourdomain.com 186 | volumes: 187 | - /path/on/host/storage:/rails/storage 188 | ``` 189 | 190 | ### Deployment Commands 191 | 192 | ```bash 193 | # Build and push the Docker image 194 | kamal setup 195 | 196 | # Deploy the application 197 | kamal deploy 198 | 199 | # Check deployment status 200 | kamal status 201 | 202 | # View logs 203 | kamal logs 204 | 205 | # Rollback to previous version 206 | kamal rollback 207 | ``` 208 | 209 | ### Continuous Deployment 210 | 211 | You can set up continuous deployment using GitHub Actions: 212 | 213 | 1. Create `.github/workflows/deploy.yml`: 214 | ```yaml 215 | name: Deploy 216 | 217 | on: 218 | push: 219 | branches: [ main ] 220 | 221 | jobs: 222 | deploy: 223 | runs-on: ubuntu-latest 224 | steps: 225 | - uses: actions/checkout@v3 226 | - uses: ruby/setup-ruby@v1 227 | with: 228 | ruby-version: '3.4' 229 | - name: Install Kamal 230 | run: gem install kamal 231 | - name: Deploy 232 | run: kamal deploy 233 | env: 234 | KAMAL_REGISTRY_PASSWORD: ${{ secrets.KAMAL_REGISTRY_PASSWORD }} 235 | SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} 236 | ``` 237 | 238 | ## 🛡️ Security Features 239 | 240 | - HTTPS enforcement for destination URLs 241 | - URL validation and availability checking 242 | - Rate limiting to prevent abuse 243 | - Password protection using secure hashing 244 | 245 | ## 🤝 Contributing 246 | 247 | 1. Fork the repository 248 | 2. Create your feature branch (`git checkout -b feature/amazing-feature`) 249 | 3. Commit your changes (`git commit -m 'Add some amazing feature'`) 250 | 4. Push to the branch (`git push origin feature/amazing-feature`) 251 | 5. Open a Pull Request 252 | 253 | ## 📄 License 254 | 255 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. 256 | 257 | ## 💡 Implementation Details 258 | 259 | YLL uses Rails' `has_secure_password` for password protection and implements various validations: 260 | 261 | - URL format and security validation 262 | - Unique code generation 263 | - Expiration time validation 264 | - URL availability checking 265 | 266 | Rate limiting is implemented to prevent abuse, and the application follows Rails best practices for security and performance. -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 3 | 4 | require_relative "config/application" 5 | 6 | Rails.application.load_tasks 7 | -------------------------------------------------------------------------------- /app/assets/images/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/app/assets/images/.keep -------------------------------------------------------------------------------- /app/assets/stylesheets/application.css: -------------------------------------------------------------------------------- 1 | /* 2 | * This is a manifest file that'll be compiled into application.css. 3 | * 4 | * With Propshaft, assets are served efficiently without preprocessing steps. You can still include 5 | * application-wide styles in this file, but keep in mind that CSS precedence will follow the standard 6 | * cascading order, meaning styles declared later in the document or manifest will override earlier ones, 7 | * depending on specificity. 8 | * 9 | * Consider organizing styles into separate files for maintainability. 10 | */ 11 | -------------------------------------------------------------------------------- /app/controllers/api/v1/links_controller.rb: -------------------------------------------------------------------------------- 1 | module Api 2 | module V1 3 | class LinksController < ApplicationController 4 | rate_limit to: 10, within: 3.minutes, only: :create, with: -> { render_rejection :too_many_requests } 5 | protect_from_forgery with: :null_session 6 | 7 | # POST /api/v1/links 8 | def create 9 | link = Link.new(link_params) 10 | if link.save 11 | render json: link.to_json, status: :created 12 | else 13 | render json: { errors: link.errors.full_messages }, status: :unprocessable_entity 14 | end 15 | end 16 | 17 | # GET /api/v1/links/:code 18 | def show 19 | link = Link.find_by(code: params[:code]) 20 | if link 21 | render json: link.to_json 22 | else 23 | render json: { error: "Link non trovato" }, status: :not_found 24 | end 25 | end 26 | 27 | private 28 | 29 | def link_params 30 | params.permit(:url, :password, :expires_at) 31 | end 32 | end 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::Base 2 | # Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has. 3 | allow_browser versions: :modern 4 | 5 | protect_from_forgery with: :exception, unless: -> { request.format.json? } 6 | before_action :set_cache_control_headers 7 | 8 | private 9 | 10 | def set_cache_control_headers 11 | response.headers["Cache-Control"] = "no-store" 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /app/controllers/concerns/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/app/controllers/concerns/.keep -------------------------------------------------------------------------------- /app/controllers/redirects_controller.rb: -------------------------------------------------------------------------------- 1 | class RedirectsController < ApplicationController 2 | rescue_from ActiveRecord::RecordNotFound, with: :link_not_found 3 | before_action :set_link, only: :show 4 | before_action :authenticate, only: :show, if: -> { @link.password_digest.present? } 5 | after_action :increment_clicks, only: :show, if: -> { response.status == 302 } 6 | 7 | def show 8 | if @link.expired? 9 | render file: Rails.root.join("public", "410.html"), status: :gone, layout: false 10 | else 11 | # Brakeman: ignore 12 | redirect_to @link.url, allow_other_host: true 13 | end 14 | end 15 | 16 | private 17 | 18 | def authenticate 19 | authenticate_or_request_with_http_basic("Links") do |username, password| 20 | username == @link.code && @link.authenticate(password) 21 | end 22 | end 23 | 24 | def increment_clicks 25 | @link.increment!(:clicks) 26 | end 27 | 28 | def set_link 29 | @link = Link.find_by!(code: params[:code]) 30 | end 31 | 32 | def link_not_found 33 | render json: { error: "Link not found" }, status: :not_found 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /app/helpers/application_helper.rb: -------------------------------------------------------------------------------- 1 | module ApplicationHelper 2 | end 3 | -------------------------------------------------------------------------------- /app/javascript/application.js: -------------------------------------------------------------------------------- 1 | // Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails 2 | import "@hotwired/turbo-rails" 3 | import "controllers" 4 | -------------------------------------------------------------------------------- /app/javascript/controllers/application.js: -------------------------------------------------------------------------------- 1 | import { Application } from "@hotwired/stimulus" 2 | 3 | const application = Application.start() 4 | 5 | // Configure Stimulus development experience 6 | application.debug = false 7 | window.Stimulus = application 8 | 9 | export { application } 10 | -------------------------------------------------------------------------------- /app/javascript/controllers/hello_controller.js: -------------------------------------------------------------------------------- 1 | import { Controller } from "@hotwired/stimulus" 2 | 3 | export default class extends Controller { 4 | connect() { 5 | this.element.textContent = "Hello World!" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /app/javascript/controllers/index.js: -------------------------------------------------------------------------------- 1 | // Import and register all your controllers from the importmap via controllers/**/*_controller 2 | import { application } from "controllers/application" 3 | import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading" 4 | eagerLoadControllersFrom("controllers", application) 5 | -------------------------------------------------------------------------------- /app/jobs/application_job.rb: -------------------------------------------------------------------------------- 1 | class ApplicationJob < ActiveJob::Base 2 | # Automatically retry jobs that encountered a deadlock 3 | # retry_on ActiveRecord::Deadlocked 4 | 5 | # Most jobs are safe to ignore if the underlying records are no longer available 6 | # discard_on ActiveJob::DeserializationError 7 | end 8 | -------------------------------------------------------------------------------- /app/mailers/application_mailer.rb: -------------------------------------------------------------------------------- 1 | class ApplicationMailer < ActionMailer::Base 2 | default from: "from@example.com" 3 | layout "mailer" 4 | end 5 | -------------------------------------------------------------------------------- /app/models/application_record.rb: -------------------------------------------------------------------------------- 1 | class ApplicationRecord < ActiveRecord::Base 2 | primary_abstract_class 3 | end 4 | -------------------------------------------------------------------------------- /app/models/concerns/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/app/models/concerns/.keep -------------------------------------------------------------------------------- /app/models/link.rb: -------------------------------------------------------------------------------- 1 | class Link < ApplicationRecord 2 | has_secure_password validations: false 3 | 4 | # Validations 5 | validates :url, presence: true, 6 | format: { 7 | with: URI::DEFAULT_PARSER.make_regexp(%w[http https]), 8 | message: "must be a valid HTTP/HTTPS URL" 9 | } 10 | 11 | validates :code, presence: true, 12 | uniqueness: true, 13 | length: { is: 8 } 14 | 15 | validate :validate_url_security 16 | validate :validate_url_availability, if: -> { url.present? && errors[:url].none? } 17 | validate :expires_at_must_be_in_future, if: -> { expires_at.present? } 18 | 19 | # Callbacks 20 | before_validation :normalize_url 21 | before_validation :generate_unique_code, on: :create 22 | 23 | def to_param 24 | code 25 | end 26 | 27 | def to_json(*) 28 | { 29 | original_url: url, 30 | short_url: short_url, 31 | created_at: created_at, 32 | expires_at: expires_at, 33 | code: code, 34 | clicks: clicks 35 | }.to_json 36 | end 37 | 38 | def expired? 39 | expires_at.present? && expires_at <= Time.current 40 | end 41 | 42 | def short_url 43 | Rails.application.routes.url_helpers.redirect_url(code) 44 | end 45 | 46 | private 47 | 48 | def normalize_url 49 | return if url.blank? 50 | 51 | begin 52 | uri = Addressable::URI.parse(url).normalize 53 | self.url = uri.to_s 54 | rescue Addressable::URI::InvalidURIError => e 55 | errors.add(:url, "contains invalid characters or format", e.message) 56 | end 57 | end 58 | 59 | def generate_unique_code 60 | self.code ||= loop do 61 | random_code = SecureRandom.alphanumeric(8) 62 | break random_code unless self.class.exists?(code: random_code) 63 | end 64 | end 65 | 66 | def validate_url_security 67 | return if errors[:url].any? 68 | 69 | uri = URI.parse(url) 70 | errors.add(:url, "must use HTTPS protocol") unless uri.scheme == "https" 71 | rescue URI::InvalidURIError 72 | # Already handled by format validation 73 | end 74 | 75 | def validate_url_availability 76 | response = Faraday.head(url) do |req| 77 | req.options.open_timeout = 3 78 | req.options.timeout = 5 79 | end 80 | 81 | unless response.success? || response.status == 301 || response.status == 302 82 | errors.add(:url, "could not be verified (HTTP #{response.status})") 83 | end 84 | rescue Faraday::Error => e 85 | errors.add(:url, "could not be reached: #{e.message}") 86 | end 87 | 88 | def expires_at_must_be_in_future 89 | errors.add(:expires_at, "must be in the future") if expires_at <= Time.current 90 | end 91 | end 92 | -------------------------------------------------------------------------------- /app/views/layouts/application.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | <%= content_for(:title) || "Yll" %> 5 | 6 | 7 | 8 | <%= csrf_meta_tags %> 9 | <%= csp_meta_tag %> 10 | 11 | <%= yield :head %> 12 | 13 | <%# Enable PWA manifest for installable apps (make sure to enable in config/routes.rb too!) %> 14 | <%#= tag.link rel: "manifest", href: pwa_manifest_path(format: :json) %> 15 | 16 | 17 | 18 | 19 | 20 | <%# Includes all stylesheet files in app/assets/stylesheets %> 21 | <%= stylesheet_link_tag :app, "data-turbo-track": "reload" %> 22 | <%= javascript_importmap_tags %> 23 | 24 | 25 | 26 | <%= yield %> 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/views/layouts/mailer.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | <%= yield %> 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/views/layouts/mailer.text.erb: -------------------------------------------------------------------------------- 1 | <%= yield %> 2 | -------------------------------------------------------------------------------- /app/views/pwa/manifest.json.erb: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Yll", 3 | "icons": [ 4 | { 5 | "src": "/icon.png", 6 | "type": "image/png", 7 | "sizes": "512x512" 8 | }, 9 | { 10 | "src": "/icon.png", 11 | "type": "image/png", 12 | "sizes": "512x512", 13 | "purpose": "maskable" 14 | } 15 | ], 16 | "start_url": "/", 17 | "display": "standalone", 18 | "scope": "/", 19 | "description": "Yll.", 20 | "theme_color": "red", 21 | "background_color": "red" 22 | } 23 | -------------------------------------------------------------------------------- /app/views/pwa/service-worker.js: -------------------------------------------------------------------------------- 1 | // Add a service worker for processing Web Push notifications: 2 | // 3 | // self.addEventListener("push", async (event) => { 4 | // const { title, options } = await event.data.json() 5 | // event.waitUntil(self.registration.showNotification(title, options)) 6 | // }) 7 | // 8 | // self.addEventListener("notificationclick", function(event) { 9 | // event.notification.close() 10 | // event.waitUntil( 11 | // clients.matchAll({ type: "window" }).then((clientList) => { 12 | // for (let i = 0; i < clientList.length; i++) { 13 | // let client = clientList[i] 14 | // let clientPath = (new URL(client.url)).pathname 15 | // 16 | // if (clientPath == event.notification.data.path && "focus" in client) { 17 | // return client.focus() 18 | // } 19 | // } 20 | // 21 | // if (clients.openWindow) { 22 | // return clients.openWindow(event.notification.data.path) 23 | // } 24 | // }) 25 | // ) 26 | // }) 27 | -------------------------------------------------------------------------------- /bin/brakeman: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require "rubygems" 3 | require "bundler/setup" 4 | 5 | ARGV.unshift("--ensure-latest") 6 | 7 | load Gem.bin_path("brakeman", "brakeman") 8 | -------------------------------------------------------------------------------- /bin/bundle: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | # 5 | # This file was generated by Bundler. 6 | # 7 | # The application 'bundle' is installed as part of a gem, and 8 | # this file is here to facilitate running it. 9 | # 10 | 11 | require "rubygems" 12 | 13 | m = Module.new do 14 | module_function 15 | 16 | def invoked_as_script? 17 | File.expand_path($0) == File.expand_path(__FILE__) 18 | end 19 | 20 | def env_var_version 21 | ENV["BUNDLER_VERSION"] 22 | end 23 | 24 | def cli_arg_version 25 | return unless invoked_as_script? # don't want to hijack other binstubs 26 | return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update` 27 | bundler_version = nil 28 | update_index = nil 29 | ARGV.each_with_index do |a, i| 30 | if update_index && update_index.succ == i && a.match?(Gem::Version::ANCHORED_VERSION_PATTERN) 31 | bundler_version = a 32 | end 33 | next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/ 34 | bundler_version = $1 35 | update_index = i 36 | end 37 | bundler_version 38 | end 39 | 40 | def gemfile 41 | gemfile = ENV["BUNDLE_GEMFILE"] 42 | return gemfile if gemfile && !gemfile.empty? 43 | 44 | File.expand_path("../Gemfile", __dir__) 45 | end 46 | 47 | def lockfile 48 | lockfile = 49 | case File.basename(gemfile) 50 | when "gems.rb" then gemfile.sub(/\.rb$/, ".locked") 51 | else "#{gemfile}.lock" 52 | end 53 | File.expand_path(lockfile) 54 | end 55 | 56 | def lockfile_version 57 | return unless File.file?(lockfile) 58 | lockfile_contents = File.read(lockfile) 59 | return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/ 60 | Regexp.last_match(1) 61 | end 62 | 63 | def bundler_requirement 64 | @bundler_requirement ||= 65 | env_var_version || 66 | cli_arg_version || 67 | bundler_requirement_for(lockfile_version) 68 | end 69 | 70 | def bundler_requirement_for(version) 71 | return "#{Gem::Requirement.default}.a" unless version 72 | 73 | bundler_gem_version = Gem::Version.new(version) 74 | 75 | bundler_gem_version.approximate_recommendation 76 | end 77 | 78 | def load_bundler! 79 | ENV["BUNDLE_GEMFILE"] ||= gemfile 80 | 81 | activate_bundler 82 | end 83 | 84 | def activate_bundler 85 | gem_error = activation_error_handling do 86 | gem "bundler", bundler_requirement 87 | end 88 | return if gem_error.nil? 89 | require_error = activation_error_handling do 90 | require "bundler/version" 91 | end 92 | return if require_error.nil? && Gem::Requirement.new(bundler_requirement).satisfied_by?(Gem::Version.new(Bundler::VERSION)) 93 | warn "Activating bundler (#{bundler_requirement}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_requirement}'`" 94 | exit 42 95 | end 96 | 97 | def activation_error_handling 98 | yield 99 | nil 100 | rescue StandardError, LoadError => e 101 | e 102 | end 103 | end 104 | 105 | m.load_bundler! 106 | 107 | if m.invoked_as_script? 108 | load Gem.bin_path("bundler", "bundle") 109 | end 110 | -------------------------------------------------------------------------------- /bin/dev: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | exec "./bin/rails", "server", *ARGV 3 | -------------------------------------------------------------------------------- /bin/docker-entrypoint: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | # Enable jemalloc for reduced memory usage and latency. 4 | if [ -z "${LD_PRELOAD+x}" ]; then 5 | LD_PRELOAD=$(find /usr/lib -name libjemalloc.so.2 -print -quit) 6 | export LD_PRELOAD 7 | fi 8 | 9 | # If running the rails server then create or migrate existing database 10 | if [ "${@: -2:1}" == "./bin/rails" ] && [ "${@: -1:1}" == "server" ]; then 11 | ./bin/rails db:prepare 12 | fi 13 | 14 | exec "${@}" 15 | -------------------------------------------------------------------------------- /bin/importmap: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require_relative "../config/application" 4 | require "importmap/commands" 5 | -------------------------------------------------------------------------------- /bin/jobs: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require_relative "../config/environment" 4 | require "solid_queue/cli" 5 | 6 | SolidQueue::Cli.start(ARGV) 7 | -------------------------------------------------------------------------------- /bin/kamal: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | # 5 | # This file was generated by Bundler. 6 | # 7 | # The application 'kamal' is installed as part of a gem, and 8 | # this file is here to facilitate running it. 9 | # 10 | 11 | ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) 12 | 13 | bundle_binstub = File.expand_path("bundle", __dir__) 14 | 15 | if File.file?(bundle_binstub) 16 | if File.read(bundle_binstub, 300).include?("This file was generated by Bundler") 17 | load(bundle_binstub) 18 | else 19 | abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. 20 | Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") 21 | end 22 | end 23 | 24 | require "rubygems" 25 | require "bundler/setup" 26 | 27 | load Gem.bin_path("kamal", "kamal") 28 | -------------------------------------------------------------------------------- /bin/rails: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | APP_PATH = File.expand_path("../config/application", __dir__) 3 | require_relative "../config/boot" 4 | require "rails/commands" 5 | -------------------------------------------------------------------------------- /bin/rake: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require_relative "../config/boot" 3 | require "rake" 4 | Rake.application.run 5 | -------------------------------------------------------------------------------- /bin/rubocop: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require "rubygems" 3 | require "bundler/setup" 4 | 5 | # explicit rubocop config increases performance slightly while avoiding config confusion. 6 | ARGV.unshift("--config", File.expand_path("../.rubocop.yml", __dir__)) 7 | 8 | load Gem.bin_path("rubocop", "rubocop") 9 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require "fileutils" 3 | 4 | APP_ROOT = File.expand_path("..", __dir__) 5 | 6 | def system!(*args) 7 | system(*args, exception: true) 8 | end 9 | 10 | FileUtils.chdir APP_ROOT do 11 | # This script is a way to set up or update your development environment automatically. 12 | # This script is idempotent, so that you can run it at any time and get an expectable outcome. 13 | # Add necessary setup steps to this file. 14 | 15 | puts "== Installing dependencies ==" 16 | system("bundle check") || system!("bundle install") 17 | 18 | # puts "\n== Copying sample files ==" 19 | # unless File.exist?("config/database.yml") 20 | # FileUtils.cp "config/database.yml.sample", "config/database.yml" 21 | # end 22 | 23 | puts "\n== Preparing database ==" 24 | system! "bin/rails db:prepare" 25 | 26 | puts "\n== Removing old logs and tempfiles ==" 27 | system! "bin/rails log:clear tmp:clear" 28 | 29 | unless ARGV.include?("--skip-server") 30 | puts "\n== Starting development server ==" 31 | STDOUT.flush # flush the output before exec(2) so that it displays 32 | exec "bin/dev" 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /bin/thrust: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require "rubygems" 3 | require "bundler/setup" 4 | 5 | load Gem.bin_path("thruster", "thrust") 6 | -------------------------------------------------------------------------------- /config.ru: -------------------------------------------------------------------------------- 1 | # This file is used by Rack-based servers to start the application. 2 | 3 | require_relative "config/environment" 4 | 5 | run Rails.application 6 | Rails.application.load_server 7 | -------------------------------------------------------------------------------- /config/application.rb: -------------------------------------------------------------------------------- 1 | require_relative "boot" 2 | 3 | require "rails/all" 4 | 5 | # Require the gems listed in Gemfile, including any gems 6 | # you've limited to :test, :development, or :production. 7 | Bundler.require(*Rails.groups) 8 | 9 | module Yll 10 | class Application < Rails::Application 11 | # Initialize configuration defaults for originally generated Rails version. 12 | config.load_defaults 8.0 13 | 14 | # Please, add to the `ignore` list any other `lib` subdirectories that do 15 | # not contain `.rb` files, or that should not be reloaded or eager loaded. 16 | # Common ones are `templates`, `generators`, or `middleware`, for example. 17 | config.autoload_lib(ignore: %w[assets tasks]) 18 | 19 | # Configuration for the application, engines, and railties goes here. 20 | # 21 | # These settings can be overridden in specific environments using the files 22 | # in config/environments, which are processed later. 23 | # 24 | # config.time_zone = "Central Time (US & Canada)" 25 | # config.eager_load_paths << Rails.root.join("extras") 26 | config.before_initialize do 27 | Rails.application.routes.default_url_options[:host] = "http://localhost:3000" 28 | end 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /config/boot.rb: -------------------------------------------------------------------------------- 1 | ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) 2 | 3 | require "bundler/setup" # Set up gems listed in the Gemfile. 4 | require "bootsnap/setup" # Speed up boot time by caching expensive operations. 5 | -------------------------------------------------------------------------------- /config/cable.yml: -------------------------------------------------------------------------------- 1 | # Async adapter only works within the same process, so for manually triggering cable updates from a console, 2 | # and seeing results in the browser, you must do so from the web console (running inside the dev process), 3 | # not a terminal started via bin/rails console! Add "console" to any action or any ERB template view 4 | # to make the web console appear. 5 | development: 6 | adapter: async 7 | 8 | test: 9 | adapter: test 10 | 11 | production: 12 | adapter: solid_cable 13 | connects_to: 14 | database: 15 | writing: cable 16 | polling_interval: 0.1.seconds 17 | message_retention: 1.day 18 | -------------------------------------------------------------------------------- /config/cache.yml: -------------------------------------------------------------------------------- 1 | default: &default 2 | store_options: 3 | # Cap age of oldest cache entry to fulfill retention policies 4 | # max_age: <%= 60.days.to_i %> 5 | max_size: <%= 256.megabytes %> 6 | namespace: <%= Rails.env %> 7 | 8 | development: 9 | <<: *default 10 | 11 | test: 12 | <<: *default 13 | 14 | production: 15 | database: cache 16 | <<: *default 17 | -------------------------------------------------------------------------------- /config/credentials.yml.enc: -------------------------------------------------------------------------------- 1 | PwbeyT0C4jmu2u8gHo98uXtqOKUb2sIoaP7ZG2k5aqmBe8oQ9R29GuE3jJ4DeQkGUCEJ5Y20QGAiHMyQmqGLkFa+eK3Xj6sJQnkQaW5GsAyz2b7Iy7Tv71snQy/tordL342F4KjCdTcHFgmyRVwfU3EVujyT0/ueayWj+mzfPMQbrXn5BE4JRLzh1aEa1CZPRyzslng+B6hXit8EZxW9zdCjciAbynV+R4Fx1ljG0P6jyG6W5HPgKL7yUigv7LyY8eSe0Peq25VnIfrV816j4n+Mp97SwIoQ8zSKsFu/HS8JHvNOuJqkEWNukCOB10SnnYFk4Ghu4okHw4LmvuM0Sg63gPNKK2Mw51IWTWNb27xF3ptKd59m1D4RXYxMTC8j6XzaATHG19n5wzcsQGoA8IjS1S8HuPCSNTjb9NBXglNV9LcXKOCw4IUK7DT3DAfKZlEurCt8NZ0biZNc1M+6Vy79zZ+yGEVpZMfOx/dkouhBx0jiyk9RS4tQ--GKlABrWlMt7xFBol--/liFX9P9Yg+vFN+LYqUZ+Q== -------------------------------------------------------------------------------- /config/database.yml: -------------------------------------------------------------------------------- 1 | # SQLite. Versions 3.8.0 and up are supported. 2 | # gem install sqlite3 3 | # 4 | # Ensure the SQLite 3 gem is defined in your Gemfile 5 | # gem "sqlite3" 6 | # 7 | default: &default 8 | adapter: sqlite3 9 | pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 10 | timeout: 5000 11 | 12 | development: 13 | <<: *default 14 | database: storage/development.sqlite3 15 | 16 | # Warning: The database defined as "test" will be erased and 17 | # re-generated from your development database when you run "rake". 18 | # Do not set this db to the same as development or production. 19 | test: 20 | <<: *default 21 | database: storage/test.sqlite3 22 | 23 | 24 | # Store production database in the storage/ directory, which by default 25 | # is mounted as a persistent Docker volume in config/deploy.yml. 26 | production: 27 | primary: 28 | <<: *default 29 | database: storage/production.sqlite3 30 | cache: 31 | <<: *default 32 | database: storage/production_cache.sqlite3 33 | migrations_paths: db/cache_migrate 34 | queue: 35 | <<: *default 36 | database: storage/production_queue.sqlite3 37 | migrations_paths: db/queue_migrate 38 | cable: 39 | <<: *default 40 | database: storage/production_cable.sqlite3 41 | migrations_paths: db/cable_migrate 42 | -------------------------------------------------------------------------------- /config/deploy.yml: -------------------------------------------------------------------------------- 1 | # Name of your application. Used to uniquely configure containers. 2 | service: yll 3 | 4 | # Name of the container image. 5 | image: your-user/yll 6 | 7 | # Deploy to these servers. 8 | servers: 9 | web: 10 | - 192.168.0.1 11 | # job: 12 | # hosts: 13 | # - 192.168.0.1 14 | # cmd: bin/jobs 15 | 16 | # Enable SSL auto certification via Let's Encrypt and allow for multiple apps on a single web server. 17 | # Remove this section when using multiple web servers and ensure you terminate SSL at your load balancer. 18 | # 19 | # Note: If using Cloudflare, set encryption mode in SSL/TLS setting to "Full" to enable CF-to-app encryption. 20 | proxy: 21 | ssl: true 22 | host: app.example.com 23 | 24 | # Credentials for your image host. 25 | registry: 26 | # Specify the registry server, if you're not using Docker Hub 27 | # server: registry.digitalocean.com / ghcr.io / ... 28 | username: your-user 29 | 30 | # Always use an access token rather than real password when possible. 31 | password: 32 | - KAMAL_REGISTRY_PASSWORD 33 | 34 | # Inject ENV variables into containers (secrets come from .kamal/secrets). 35 | env: 36 | secret: 37 | - RAILS_MASTER_KEY 38 | clear: 39 | # Run the Solid Queue Supervisor inside the web server's Puma process to do jobs. 40 | # When you start using multiple servers, you should split out job processing to a dedicated machine. 41 | SOLID_QUEUE_IN_PUMA: true 42 | 43 | # Set number of processes dedicated to Solid Queue (default: 1) 44 | # JOB_CONCURRENCY: 3 45 | 46 | # Set number of cores available to the application on each server (default: 1). 47 | # WEB_CONCURRENCY: 2 48 | 49 | # Match this to any external database server to configure Active Record correctly 50 | # Use yll-db for a db accessory server on same machine via local kamal docker network. 51 | # DB_HOST: 192.168.0.2 52 | 53 | # Log everything from Rails 54 | # RAILS_LOG_LEVEL: debug 55 | 56 | # Aliases are triggered with "bin/kamal ". You can overwrite arguments on invocation: 57 | # "bin/kamal logs -r job" will tail logs from the first server in the job section. 58 | aliases: 59 | console: app exec --interactive --reuse "bin/rails console" 60 | shell: app exec --interactive --reuse "bash" 61 | logs: app logs -f 62 | dbc: app exec --interactive --reuse "bin/rails dbconsole" 63 | 64 | 65 | # Use a persistent storage volume for sqlite database files and local Active Storage files. 66 | # Recommended to change this to a mounted volume path that is backed up off server. 67 | volumes: 68 | - "yll_storage:/rails/storage" 69 | 70 | 71 | # Bridge fingerprinted assets, like JS and CSS, between versions to avoid 72 | # hitting 404 on in-flight requests. Combines all files from new and old 73 | # version inside the asset_path. 74 | asset_path: /rails/public/assets 75 | 76 | # Configure the image builder. 77 | builder: 78 | arch: amd64 79 | 80 | # # Build image via remote server (useful for faster amd64 builds on arm64 computers) 81 | # remote: ssh://docker@docker-builder-server 82 | # 83 | # # Pass arguments and secrets to the Docker build process 84 | # args: 85 | # RUBY_VERSION: 86 | # secrets: 87 | # - GITHUB_TOKEN 88 | # - RAILS_MASTER_KEY 89 | 90 | # Use a different ssh user than root 91 | # ssh: 92 | # user: app 93 | 94 | # Use accessory services (secrets come from .kamal/secrets). 95 | # accessories: 96 | # db: 97 | # image: mysql:8.0 98 | # host: 192.168.0.2 99 | # # Change to 3306 to expose port to the world instead of just local network. 100 | # port: "127.0.0.1:3306:3306" 101 | # env: 102 | # clear: 103 | # MYSQL_ROOT_HOST: '%' 104 | # secret: 105 | # - MYSQL_ROOT_PASSWORD 106 | # files: 107 | # - config/mysql/production.cnf:/etc/mysql/my.cnf 108 | # - db/production.sql:/docker-entrypoint-initdb.d/setup.sql 109 | # directories: 110 | # - data:/var/lib/mysql 111 | # redis: 112 | # image: redis:7.0 113 | # host: 192.168.0.2 114 | # port: 6379 115 | # directories: 116 | # - data:/data 117 | -------------------------------------------------------------------------------- /config/environment.rb: -------------------------------------------------------------------------------- 1 | # Load the Rails application. 2 | require_relative "application" 3 | 4 | # Initialize the Rails application. 5 | Rails.application.initialize! 6 | -------------------------------------------------------------------------------- /config/environments/development.rb: -------------------------------------------------------------------------------- 1 | require "active_support/core_ext/integer/time" 2 | 3 | Rails.application.configure do 4 | # Settings specified here will take precedence over those in config/application.rb. 5 | 6 | # Make code changes take effect immediately without server restart. 7 | config.enable_reloading = true 8 | 9 | # Do not eager load code on boot. 10 | config.eager_load = false 11 | 12 | # Show full error reports. 13 | config.consider_all_requests_local = true 14 | 15 | # Enable server timing. 16 | config.server_timing = true 17 | 18 | # Enable/disable Action Controller caching. By default Action Controller caching is disabled. 19 | # Run rails dev:cache to toggle Action Controller caching. 20 | if Rails.root.join("tmp/caching-dev.txt").exist? 21 | config.action_controller.perform_caching = true 22 | config.action_controller.enable_fragment_cache_logging = true 23 | config.public_file_server.headers = { "cache-control" => "public, max-age=#{2.days.to_i}" } 24 | else 25 | config.action_controller.perform_caching = false 26 | end 27 | 28 | # Change to :null_store to avoid any caching. 29 | config.cache_store = :memory_store 30 | 31 | # Store uploaded files on the local file system (see config/storage.yml for options). 32 | config.active_storage.service = :local 33 | 34 | # Don't care if the mailer can't send. 35 | config.action_mailer.raise_delivery_errors = false 36 | 37 | # Make template changes take effect immediately. 38 | config.action_mailer.perform_caching = false 39 | 40 | # Set localhost to be used by links generated in mailer templates. 41 | config.action_mailer.default_url_options = { host: "localhost", port: 3000 } 42 | 43 | # Print deprecation notices to the Rails logger. 44 | config.active_support.deprecation = :log 45 | 46 | # Raise an error on page load if there are pending migrations. 47 | config.active_record.migration_error = :page_load 48 | 49 | # Highlight code that triggered database queries in logs. 50 | config.active_record.verbose_query_logs = true 51 | 52 | # Append comments with runtime information tags to SQL queries in logs. 53 | config.active_record.query_log_tags_enabled = true 54 | 55 | # Highlight code that enqueued background job in logs. 56 | config.active_job.verbose_enqueue_logs = true 57 | 58 | # Raises error for missing translations. 59 | # config.i18n.raise_on_missing_translations = true 60 | 61 | # Annotate rendered view with file names. 62 | config.action_view.annotate_rendered_view_with_filenames = true 63 | 64 | # Uncomment if you wish to allow Action Cable access from any origin. 65 | # config.action_cable.disable_request_forgery_protection = true 66 | 67 | # Raise error when a before_action's only/except options reference missing actions. 68 | config.action_controller.raise_on_missing_callback_actions = true 69 | 70 | # Apply autocorrection by RuboCop to files generated by `bin/rails generate`. 71 | # config.generators.apply_rubocop_autocorrect_after_generate! 72 | end 73 | -------------------------------------------------------------------------------- /config/environments/production.rb: -------------------------------------------------------------------------------- 1 | require "active_support/core_ext/integer/time" 2 | 3 | Rails.application.configure do 4 | # Settings specified here will take precedence over those in config/application.rb. 5 | 6 | # Code is not reloaded between requests. 7 | config.enable_reloading = false 8 | 9 | # Eager load code on boot for better performance and memory savings (ignored by Rake tasks). 10 | config.eager_load = true 11 | 12 | # Full error reports are disabled. 13 | config.consider_all_requests_local = false 14 | 15 | # Turn on fragment caching in view templates. 16 | config.action_controller.perform_caching = true 17 | 18 | # Cache assets for far-future expiry since they are all digest stamped. 19 | config.public_file_server.headers = { "cache-control" => "public, max-age=#{1.year.to_i}" } 20 | 21 | # Enable serving of images, stylesheets, and JavaScripts from an asset server. 22 | # config.asset_host = "http://assets.example.com" 23 | 24 | # Store uploaded files on the local file system (see config/storage.yml for options). 25 | config.active_storage.service = :local 26 | 27 | # Assume all access to the app is happening through a SSL-terminating reverse proxy. 28 | config.assume_ssl = true 29 | 30 | # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 31 | config.force_ssl = true 32 | 33 | # Skip http-to-https redirect for the default health check endpoint. 34 | # config.ssl_options = { redirect: { exclude: ->(request) { request.path == "/up" } } } 35 | 36 | # Log to STDOUT with the current request id as a default log tag. 37 | config.log_tags = [ :request_id ] 38 | config.logger = ActiveSupport::TaggedLogging.logger(STDOUT) 39 | 40 | # Change to "debug" to log everything (including potentially personally-identifiable information!) 41 | config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info") 42 | 43 | # Prevent health checks from clogging up the logs. 44 | config.silence_healthcheck_path = "/up" 45 | 46 | # Don't log any deprecations. 47 | config.active_support.report_deprecations = false 48 | 49 | # Replace the default in-process memory cache store with a durable alternative. 50 | config.cache_store = :solid_cache_store 51 | 52 | # Replace the default in-process and non-durable queuing backend for Active Job. 53 | config.active_job.queue_adapter = :solid_queue 54 | config.solid_queue.connects_to = { database: { writing: :queue } } 55 | 56 | # Ignore bad email addresses and do not raise email delivery errors. 57 | # Set this to true and configure the email server for immediate delivery to raise delivery errors. 58 | # config.action_mailer.raise_delivery_errors = false 59 | 60 | # Set host to be used by links generated in mailer templates. 61 | config.action_mailer.default_url_options = { host: "example.com" } 62 | 63 | # Specify outgoing SMTP server. Remember to add smtp/* credentials via rails credentials:edit. 64 | # config.action_mailer.smtp_settings = { 65 | # user_name: Rails.application.credentials.dig(:smtp, :user_name), 66 | # password: Rails.application.credentials.dig(:smtp, :password), 67 | # address: "smtp.example.com", 68 | # port: 587, 69 | # authentication: :plain 70 | # } 71 | 72 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 73 | # the I18n.default_locale when a translation cannot be found). 74 | config.i18n.fallbacks = true 75 | 76 | # Do not dump schema after migrations. 77 | config.active_record.dump_schema_after_migration = false 78 | 79 | # Only use :id for inspections in production. 80 | config.active_record.attributes_for_inspect = [ :id ] 81 | 82 | # Enable DNS rebinding protection and other `Host` header attacks. 83 | # config.hosts = [ 84 | # "example.com", # Allow requests from example.com 85 | # /.*\.example\.com/ # Allow requests from subdomains like `www.example.com` 86 | # ] 87 | # 88 | # Skip DNS rebinding protection for the default health check endpoint. 89 | # config.host_authorization = { exclude: ->(request) { request.path == "/up" } } 90 | end 91 | -------------------------------------------------------------------------------- /config/environments/test.rb: -------------------------------------------------------------------------------- 1 | # The test environment is used exclusively to run your application's 2 | # test suite. You never need to work with it otherwise. Remember that 3 | # your test database is "scratch space" for the test suite and is wiped 4 | # and recreated between test runs. Don't rely on the data there! 5 | 6 | Rails.application.configure do 7 | # Settings specified here will take precedence over those in config/application.rb. 8 | 9 | # While tests run files are not watched, reloading is not necessary. 10 | config.enable_reloading = false 11 | 12 | # Eager loading loads your entire application. When running a single test locally, 13 | # this is usually not necessary, and can slow down your test suite. However, it's 14 | # recommended that you enable it in continuous integration systems to ensure eager 15 | # loading is working properly before deploying your code. 16 | config.eager_load = ENV["CI"].present? 17 | 18 | # Configure public file server for tests with cache-control for performance. 19 | config.public_file_server.headers = { "cache-control" => "public, max-age=3600" } 20 | 21 | # Show full error reports. 22 | config.consider_all_requests_local = true 23 | config.cache_store = :null_store 24 | 25 | # Render exception templates for rescuable exceptions and raise for other exceptions. 26 | config.action_dispatch.show_exceptions = :rescuable 27 | 28 | # Disable request forgery protection in test environment. 29 | config.action_controller.allow_forgery_protection = false 30 | 31 | # Store uploaded files on the local file system in a temporary directory. 32 | config.active_storage.service = :test 33 | 34 | # Tell Action Mailer not to deliver emails to the real world. 35 | # The :test delivery method accumulates sent emails in the 36 | # ActionMailer::Base.deliveries array. 37 | config.action_mailer.delivery_method = :test 38 | 39 | # Set host to be used by links generated in mailer templates. 40 | config.action_mailer.default_url_options = { host: "example.com" } 41 | 42 | # Print deprecation notices to the stderr. 43 | config.active_support.deprecation = :stderr 44 | 45 | # Raises error for missing translations. 46 | # config.i18n.raise_on_missing_translations = true 47 | 48 | # Annotate rendered view with file names. 49 | # config.action_view.annotate_rendered_view_with_filenames = true 50 | 51 | # Raise error when a before_action's only/except options reference missing actions. 52 | config.action_controller.raise_on_missing_callback_actions = true 53 | end 54 | -------------------------------------------------------------------------------- /config/importmap.rb: -------------------------------------------------------------------------------- 1 | # Pin npm packages by running ./bin/importmap 2 | 3 | pin "application" 4 | pin "@hotwired/turbo-rails", to: "turbo.min.js" 5 | pin "@hotwired/stimulus", to: "stimulus.min.js" 6 | pin "@hotwired/stimulus-loading", to: "stimulus-loading.js" 7 | pin_all_from "app/javascript/controllers", under: "controllers" 8 | -------------------------------------------------------------------------------- /config/initializers/assets.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Version of your assets, change this if you want to expire all your assets. 4 | Rails.application.config.assets.version = "1.0" 5 | 6 | # Add additional assets to the asset load path. 7 | # Rails.application.config.assets.paths << Emoji.images_path 8 | -------------------------------------------------------------------------------- /config/initializers/content_security_policy.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Define an application-wide content security policy. 4 | # See the Securing Rails Applications Guide for more information: 5 | # https://guides.rubyonrails.org/security.html#content-security-policy-header 6 | 7 | # Rails.application.configure do 8 | # config.content_security_policy do |policy| 9 | # policy.default_src :self, :https 10 | # policy.font_src :self, :https, :data 11 | # policy.img_src :self, :https, :data 12 | # policy.object_src :none 13 | # policy.script_src :self, :https 14 | # policy.style_src :self, :https 15 | # # Specify URI for violation reports 16 | # # policy.report_uri "/csp-violation-report-endpoint" 17 | # end 18 | # 19 | # # Generate session nonces for permitted importmap, inline scripts, and inline styles. 20 | # config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s } 21 | # config.content_security_policy_nonce_directives = %w(script-src style-src) 22 | # 23 | # # Report violations without enforcing the policy. 24 | # # config.content_security_policy_report_only = true 25 | # end 26 | -------------------------------------------------------------------------------- /config/initializers/filter_parameter_logging.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Configure parameters to be partially matched (e.g. passw matches password) and filtered from the log file. 4 | # Use this to limit dissemination of sensitive information. 5 | # See the ActiveSupport::ParameterFilter documentation for supported notations and behaviors. 6 | Rails.application.config.filter_parameters += [ 7 | :passw, :email, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn, :cvv, :cvc 8 | ] 9 | -------------------------------------------------------------------------------- /config/initializers/inflections.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new inflection rules using the following format. Inflections 4 | # are locale specific, and you may define rules for as many different 5 | # locales as you wish. All of these examples are active by default: 6 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 7 | # inflect.plural /^(ox)$/i, "\\1en" 8 | # inflect.singular /^(ox)en/i, "\\1" 9 | # inflect.irregular "person", "people" 10 | # inflect.uncountable %w( fish sheep ) 11 | # end 12 | 13 | # These inflection rules are supported but not enabled by default: 14 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 15 | # inflect.acronym "RESTful" 16 | # end 17 | -------------------------------------------------------------------------------- /config/locales/en.yml: -------------------------------------------------------------------------------- 1 | # Files in the config/locales directory are used for internationalization and 2 | # are automatically loaded by Rails. If you want to use locales other than 3 | # English, add the necessary files in this directory. 4 | # 5 | # To use the locales, use `I18n.t`: 6 | # 7 | # I18n.t "hello" 8 | # 9 | # In views, this is aliased to just `t`: 10 | # 11 | # <%= t("hello") %> 12 | # 13 | # To use a different locale, set it with `I18n.locale`: 14 | # 15 | # I18n.locale = :es 16 | # 17 | # This would use the information in config/locales/es.yml. 18 | # 19 | # To learn more about the API, please read the Rails Internationalization guide 20 | # at https://guides.rubyonrails.org/i18n.html. 21 | # 22 | # Be aware that YAML interprets the following case-insensitive strings as 23 | # booleans: `true`, `false`, `on`, `off`, `yes`, `no`. Therefore, these strings 24 | # must be quoted to be interpreted as strings. For example: 25 | # 26 | # en: 27 | # "yes": yup 28 | # enabled: "ON" 29 | 30 | en: 31 | hello: "Hello world" 32 | -------------------------------------------------------------------------------- /config/puma.rb: -------------------------------------------------------------------------------- 1 | # This configuration file will be evaluated by Puma. The top-level methods that 2 | # are invoked here are part of Puma's configuration DSL. For more information 3 | # about methods provided by the DSL, see https://puma.io/puma/Puma/DSL.html. 4 | # 5 | # Puma starts a configurable number of processes (workers) and each process 6 | # serves each request in a thread from an internal thread pool. 7 | # 8 | # You can control the number of workers using ENV["WEB_CONCURRENCY"]. You 9 | # should only set this value when you want to run 2 or more workers. The 10 | # default is already 1. 11 | # 12 | # The ideal number of threads per worker depends both on how much time the 13 | # application spends waiting for IO operations and on how much you wish to 14 | # prioritize throughput over latency. 15 | # 16 | # As a rule of thumb, increasing the number of threads will increase how much 17 | # traffic a given process can handle (throughput), but due to CRuby's 18 | # Global VM Lock (GVL) it has diminishing returns and will degrade the 19 | # response time (latency) of the application. 20 | # 21 | # The default is set to 3 threads as it's deemed a decent compromise between 22 | # throughput and latency for the average Rails application. 23 | # 24 | # Any libraries that use a connection pool or another resource pool should 25 | # be configured to provide at least as many connections as the number of 26 | # threads. This includes Active Record's `pool` parameter in `database.yml`. 27 | threads_count = ENV.fetch("RAILS_MAX_THREADS", 3) 28 | threads threads_count, threads_count 29 | 30 | # Specifies the `port` that Puma will listen on to receive requests; default is 3000. 31 | port ENV.fetch("PORT", 3000) 32 | 33 | # Allow puma to be restarted by `bin/rails restart` command. 34 | plugin :tmp_restart 35 | 36 | # Run the Solid Queue supervisor inside of Puma for single-server deployments 37 | plugin :solid_queue if ENV["SOLID_QUEUE_IN_PUMA"] 38 | 39 | # Specify the PID file. Defaults to tmp/pids/server.pid in development. 40 | # In other environments, only set the PID file if requested. 41 | pidfile ENV["PIDFILE"] if ENV["PIDFILE"] 42 | -------------------------------------------------------------------------------- /config/queue.yml: -------------------------------------------------------------------------------- 1 | default: &default 2 | dispatchers: 3 | - polling_interval: 1 4 | batch_size: 500 5 | workers: 6 | - queues: "*" 7 | threads: 3 8 | processes: <%= ENV.fetch("JOB_CONCURRENCY", 1) %> 9 | polling_interval: 0.1 10 | 11 | development: 12 | <<: *default 13 | 14 | test: 15 | <<: *default 16 | 17 | production: 18 | <<: *default 19 | -------------------------------------------------------------------------------- /config/recurring.yml: -------------------------------------------------------------------------------- 1 | # production: 2 | # periodic_cleanup: 3 | # class: CleanSoftDeletedRecordsJob 4 | # queue: background 5 | # args: [ 1000, { batch_size: 500 } ] 6 | # schedule: every hour 7 | # periodic_command: 8 | # command: "SoftDeletedRecord.due.delete_all" 9 | # priority: 2 10 | # schedule: at 5am every day 11 | -------------------------------------------------------------------------------- /config/routes.rb: -------------------------------------------------------------------------------- 1 | Rails.application.routes.draw do 2 | # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html 3 | 4 | # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. 5 | # Can be used by load balancers and uptime monitors to verify that the app is live. 6 | get "up" => "rails/health#show", as: :rails_health_check 7 | 8 | # Render dynamic PWA files from app/views/pwa/* (remember to link manifest in application.html.erb) 9 | # get "manifest" => "rails/pwa#manifest", as: :pwa_manifest 10 | # get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker 11 | 12 | # Defines the root path route ("/") 13 | # root "posts#index" 14 | # 15 | namespace :api do 16 | namespace :v1 do 17 | resources :links, only: [ :create, :show ], defaults: { format: :json }, param: :code 18 | end 19 | end 20 | 21 | # Route to handle redirection from shortened URLs. 22 | get "/:code", to: "redirects#show", as: :redirect 23 | end 24 | -------------------------------------------------------------------------------- /config/storage.yml: -------------------------------------------------------------------------------- 1 | test: 2 | service: Disk 3 | root: <%= Rails.root.join("tmp/storage") %> 4 | 5 | local: 6 | service: Disk 7 | root: <%= Rails.root.join("storage") %> 8 | 9 | # Use bin/rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) 10 | # amazon: 11 | # service: S3 12 | # access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> 13 | # secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> 14 | # region: us-east-1 15 | # bucket: your_own_bucket-<%= Rails.env %> 16 | 17 | # Remember not to checkin your GCS keyfile to a repository 18 | # google: 19 | # service: GCS 20 | # project: your_project 21 | # credentials: <%= Rails.root.join("path/to/gcs.keyfile") %> 22 | # bucket: your_own_bucket-<%= Rails.env %> 23 | 24 | # Use bin/rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key) 25 | # microsoft: 26 | # service: AzureStorage 27 | # storage_account_name: your_account_name 28 | # storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %> 29 | # container: your_container_name-<%= Rails.env %> 30 | 31 | # mirror: 32 | # service: Mirror 33 | # primary: local 34 | # mirrors: [ amazon, google, microsoft ] 35 | -------------------------------------------------------------------------------- /db/cable_schema.rb: -------------------------------------------------------------------------------- 1 | ActiveRecord::Schema[7.1].define(version: 1) do 2 | create_table "solid_cable_messages", force: :cascade do |t| 3 | t.binary "channel", limit: 1024, null: false 4 | t.binary "payload", limit: 536870912, null: false 5 | t.datetime "created_at", null: false 6 | t.integer "channel_hash", limit: 8, null: false 7 | t.index ["channel"], name: "index_solid_cable_messages_on_channel" 8 | t.index ["channel_hash"], name: "index_solid_cable_messages_on_channel_hash" 9 | t.index ["created_at"], name: "index_solid_cable_messages_on_created_at" 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /db/cache_schema.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | ActiveRecord::Schema[7.2].define(version: 1) do 4 | create_table "solid_cache_entries", force: :cascade do |t| 5 | t.binary "key", limit: 1024, null: false 6 | t.binary "value", limit: 536870912, null: false 7 | t.datetime "created_at", null: false 8 | t.integer "key_hash", limit: 8, null: false 9 | t.integer "byte_size", limit: 4, null: false 10 | t.index ["byte_size"], name: "index_solid_cache_entries_on_byte_size" 11 | t.index ["key_hash", "byte_size"], name: "index_solid_cache_entries_on_key_hash_and_byte_size" 12 | t.index ["key_hash"], name: "index_solid_cache_entries_on_key_hash", unique: true 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /db/migrate/20250223174204_create_links.rb: -------------------------------------------------------------------------------- 1 | class CreateLinks < ActiveRecord::Migration[8.0] 2 | def change 3 | create_table :links do |t| 4 | t.string :url 5 | t.string :password_digest 6 | t.datetime :expires_at 7 | t.string :code 8 | t.integer :clicks, default: 0 9 | 10 | t.timestamps 11 | end 12 | 13 | add_index :links, :url 14 | add_index :links, :code, unique: true 15 | add_index :links, :clicks 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /db/queue_schema.rb: -------------------------------------------------------------------------------- 1 | ActiveRecord::Schema[7.1].define(version: 1) do 2 | create_table "solid_queue_blocked_executions", force: :cascade do |t| 3 | t.bigint "job_id", null: false 4 | t.string "queue_name", null: false 5 | t.integer "priority", default: 0, null: false 6 | t.string "concurrency_key", null: false 7 | t.datetime "expires_at", null: false 8 | t.datetime "created_at", null: false 9 | t.index [ "concurrency_key", "priority", "job_id" ], name: "index_solid_queue_blocked_executions_for_release" 10 | t.index [ "expires_at", "concurrency_key" ], name: "index_solid_queue_blocked_executions_for_maintenance" 11 | t.index [ "job_id" ], name: "index_solid_queue_blocked_executions_on_job_id", unique: true 12 | end 13 | 14 | create_table "solid_queue_claimed_executions", force: :cascade do |t| 15 | t.bigint "job_id", null: false 16 | t.bigint "process_id" 17 | t.datetime "created_at", null: false 18 | t.index [ "job_id" ], name: "index_solid_queue_claimed_executions_on_job_id", unique: true 19 | t.index [ "process_id", "job_id" ], name: "index_solid_queue_claimed_executions_on_process_id_and_job_id" 20 | end 21 | 22 | create_table "solid_queue_failed_executions", force: :cascade do |t| 23 | t.bigint "job_id", null: false 24 | t.text "error" 25 | t.datetime "created_at", null: false 26 | t.index [ "job_id" ], name: "index_solid_queue_failed_executions_on_job_id", unique: true 27 | end 28 | 29 | create_table "solid_queue_jobs", force: :cascade do |t| 30 | t.string "queue_name", null: false 31 | t.string "class_name", null: false 32 | t.text "arguments" 33 | t.integer "priority", default: 0, null: false 34 | t.string "active_job_id" 35 | t.datetime "scheduled_at" 36 | t.datetime "finished_at" 37 | t.string "concurrency_key" 38 | t.datetime "created_at", null: false 39 | t.datetime "updated_at", null: false 40 | t.index [ "active_job_id" ], name: "index_solid_queue_jobs_on_active_job_id" 41 | t.index [ "class_name" ], name: "index_solid_queue_jobs_on_class_name" 42 | t.index [ "finished_at" ], name: "index_solid_queue_jobs_on_finished_at" 43 | t.index [ "queue_name", "finished_at" ], name: "index_solid_queue_jobs_for_filtering" 44 | t.index [ "scheduled_at", "finished_at" ], name: "index_solid_queue_jobs_for_alerting" 45 | end 46 | 47 | create_table "solid_queue_pauses", force: :cascade do |t| 48 | t.string "queue_name", null: false 49 | t.datetime "created_at", null: false 50 | t.index [ "queue_name" ], name: "index_solid_queue_pauses_on_queue_name", unique: true 51 | end 52 | 53 | create_table "solid_queue_processes", force: :cascade do |t| 54 | t.string "kind", null: false 55 | t.datetime "last_heartbeat_at", null: false 56 | t.bigint "supervisor_id" 57 | t.integer "pid", null: false 58 | t.string "hostname" 59 | t.text "metadata" 60 | t.datetime "created_at", null: false 61 | t.string "name", null: false 62 | t.index [ "last_heartbeat_at" ], name: "index_solid_queue_processes_on_last_heartbeat_at" 63 | t.index [ "name", "supervisor_id" ], name: "index_solid_queue_processes_on_name_and_supervisor_id", unique: true 64 | t.index [ "supervisor_id" ], name: "index_solid_queue_processes_on_supervisor_id" 65 | end 66 | 67 | create_table "solid_queue_ready_executions", force: :cascade do |t| 68 | t.bigint "job_id", null: false 69 | t.string "queue_name", null: false 70 | t.integer "priority", default: 0, null: false 71 | t.datetime "created_at", null: false 72 | t.index [ "job_id" ], name: "index_solid_queue_ready_executions_on_job_id", unique: true 73 | t.index [ "priority", "job_id" ], name: "index_solid_queue_poll_all" 74 | t.index [ "queue_name", "priority", "job_id" ], name: "index_solid_queue_poll_by_queue" 75 | end 76 | 77 | create_table "solid_queue_recurring_executions", force: :cascade do |t| 78 | t.bigint "job_id", null: false 79 | t.string "task_key", null: false 80 | t.datetime "run_at", null: false 81 | t.datetime "created_at", null: false 82 | t.index [ "job_id" ], name: "index_solid_queue_recurring_executions_on_job_id", unique: true 83 | t.index [ "task_key", "run_at" ], name: "index_solid_queue_recurring_executions_on_task_key_and_run_at", unique: true 84 | end 85 | 86 | create_table "solid_queue_recurring_tasks", force: :cascade do |t| 87 | t.string "key", null: false 88 | t.string "schedule", null: false 89 | t.string "command", limit: 2048 90 | t.string "class_name" 91 | t.text "arguments" 92 | t.string "queue_name" 93 | t.integer "priority", default: 0 94 | t.boolean "static", default: true, null: false 95 | t.text "description" 96 | t.datetime "created_at", null: false 97 | t.datetime "updated_at", null: false 98 | t.index [ "key" ], name: "index_solid_queue_recurring_tasks_on_key", unique: true 99 | t.index [ "static" ], name: "index_solid_queue_recurring_tasks_on_static" 100 | end 101 | 102 | create_table "solid_queue_scheduled_executions", force: :cascade do |t| 103 | t.bigint "job_id", null: false 104 | t.string "queue_name", null: false 105 | t.integer "priority", default: 0, null: false 106 | t.datetime "scheduled_at", null: false 107 | t.datetime "created_at", null: false 108 | t.index [ "job_id" ], name: "index_solid_queue_scheduled_executions_on_job_id", unique: true 109 | t.index [ "scheduled_at", "priority", "job_id" ], name: "index_solid_queue_dispatch_all" 110 | end 111 | 112 | create_table "solid_queue_semaphores", force: :cascade do |t| 113 | t.string "key", null: false 114 | t.integer "value", default: 1, null: false 115 | t.datetime "expires_at", null: false 116 | t.datetime "created_at", null: false 117 | t.datetime "updated_at", null: false 118 | t.index [ "expires_at" ], name: "index_solid_queue_semaphores_on_expires_at" 119 | t.index [ "key", "value" ], name: "index_solid_queue_semaphores_on_key_and_value" 120 | t.index [ "key" ], name: "index_solid_queue_semaphores_on_key", unique: true 121 | end 122 | 123 | add_foreign_key "solid_queue_blocked_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade 124 | add_foreign_key "solid_queue_claimed_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade 125 | add_foreign_key "solid_queue_failed_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade 126 | add_foreign_key "solid_queue_ready_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade 127 | add_foreign_key "solid_queue_recurring_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade 128 | add_foreign_key "solid_queue_scheduled_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade 129 | end 130 | -------------------------------------------------------------------------------- /db/schema.rb: -------------------------------------------------------------------------------- 1 | # This file is auto-generated from the current state of the database. Instead 2 | # of editing this file, please use the migrations feature of Active Record to 3 | # incrementally modify your database, and then regenerate this schema definition. 4 | # 5 | # This file is the source Rails uses to define your schema when running `bin/rails 6 | # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to 7 | # be faster and is potentially less error prone than running all of your 8 | # migrations from scratch. Old migrations may fail to apply correctly if those 9 | # migrations use external dependencies or application code. 10 | # 11 | # It's strongly recommended that you check this file into your version control system. 12 | 13 | ActiveRecord::Schema[8.0].define(version: 2025_02_23_174204) do 14 | create_table "links", force: :cascade do |t| 15 | t.string "url" 16 | t.string "password_digest" 17 | t.datetime "expires_at" 18 | t.string "code" 19 | t.integer "clicks", default: 0 20 | t.datetime "created_at", null: false 21 | t.datetime "updated_at", null: false 22 | t.index ["clicks"], name: "index_links_on_clicks" 23 | t.index ["code"], name: "index_links_on_code", unique: true 24 | t.index ["url"], name: "index_links_on_url" 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /db/seeds.rb: -------------------------------------------------------------------------------- 1 | # This file should ensure the existence of records required to run the application in every environment (production, 2 | # development, test). The code here should be idempotent so that it can be executed at any point in every environment. 3 | # The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup). 4 | # 5 | # Example: 6 | # 7 | # ["Action", "Comedy", "Drama", "Horror"].each do |genre_name| 8 | # MovieGenre.find_or_create_by!(name: genre_name) 9 | # end 10 | -------------------------------------------------------------------------------- /lib/tasks/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/lib/tasks/.keep -------------------------------------------------------------------------------- /log/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/log/.keep -------------------------------------------------------------------------------- /public/400.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | The server cannot process the request due to a client error (400 Bad Request) 8 | 9 | 10 | 11 | 12 | 13 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 |
104 |
105 | 106 |
107 |
108 |

The server cannot process the request due to a client error. Please check the request and try again. If you’re the application owner check the logs for more information.

109 |
110 |
111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /public/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | The page you were looking for doesn’t exist (404 Not found) 8 | 9 | 10 | 11 | 12 | 13 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 |
104 |
105 | 106 |
107 |
108 |

The page you were looking for doesn’t exist. You may have mistyped the address or the page may have moved. If you’re the application owner check the logs for more information.

109 |
110 |
111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /public/406-unsupported-browser.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Your browser is not supported (406 Not Acceptable) 8 | 9 | 10 | 11 | 12 | 13 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 |
104 |
105 | 106 |
107 |
108 |

Your browser is not supported.
Please upgrade your browser to continue.

109 |
110 |
111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /public/410.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 410 - Link Gone 7 | 8 | 9 | 19 | 20 | 21 |
22 |

410

23 |

24 | This link has been permanently removed or expires. 25 |

26 |
27 | yll.cx 28 |
29 |
30 | 31 | -------------------------------------------------------------------------------- /public/422.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | The change you wanted was rejected (422 Unprocessable Entity) 8 | 9 | 10 | 11 | 12 | 13 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 |
104 |
105 | 106 |
107 |
108 |

The change you wanted was rejected. Maybe you tried to change something you didn’t have access to. If you’re the application owner check the logs for more information.

109 |
110 |
111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /public/500.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | We’re sorry, but something went wrong (500 Internal Server Error) 8 | 9 | 10 | 11 | 12 | 13 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 |
104 |
105 | 106 |
107 |
108 |

We’re sorry, but something went wrong.
If you’re the application owner check the logs for more information.

109 |
110 |
111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /public/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/public/icon.png -------------------------------------------------------------------------------- /public/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file 2 | -------------------------------------------------------------------------------- /script/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/script/.keep -------------------------------------------------------------------------------- /spec/controllers/api/v1/links_controller_spec.rb: -------------------------------------------------------------------------------- 1 | require "rails_helper" 2 | 3 | RSpec.describe "Api::V1::Links", type: :request do 4 | let(:valid_url) { "https://example.com" } 5 | let(:valid_attributes) { { url: valid_url, expires_at: 1.day.from_now } } 6 | let(:invalid_attributes) { { url: "invalid_url" } } 7 | 8 | describe "POST /api/v1/links" do 9 | context "with valid parameters" do 10 | it "creates a Link and returns JSON representation" do 11 | post "/api/v1/links", params: valid_attributes 12 | expect(response).to have_http_status(:created) 13 | json = JSON.parse(response.body) 14 | expect(json).to include("original_url", "short_url", "created_at", "expires_at", "code", "clicks") 15 | end 16 | end 17 | 18 | context "with invalid parameters" do 19 | it "returns unprocessable_entity with errors" do 20 | post "/api/v1/links", params: invalid_attributes 21 | expect(response).to have_http_status(:unprocessable_entity) 22 | json = JSON.parse(response.body) 23 | expect(json["errors"]).to be_present 24 | end 25 | end 26 | 27 | context "when rate limiting is triggered" do 28 | before do 29 | Api::V1::LinksController.class_eval do 30 | alias_method :original_create, :create 31 | def create 32 | render json: { error: "Too many requests" }, status: :too_many_requests 33 | end 34 | end 35 | end 36 | 37 | after do 38 | Api::V1::LinksController.class_eval do 39 | alias_method :create, :original_create 40 | remove_method :original_create 41 | end 42 | end 43 | 44 | it "returns too_many_requests response" do 45 | post "/api/v1/links", params: valid_attributes 46 | expect(response).to have_http_status(:too_many_requests) 47 | json = JSON.parse(response.body) 48 | expect(json["error"]).to eq("Too many requests") 49 | end 50 | end 51 | end 52 | 53 | describe "GET /api/v1/links/:code" do 54 | context "when the Link exists" do 55 | let!(:link) { Link.create!(valid_attributes.merge(code: "VALID123")) } 56 | 57 | it "returns the Link JSON representation" do 58 | get "/api/v1/links/#{link.code}" 59 | expect(response).to have_http_status(:ok) 60 | json = JSON.parse(response.body) 61 | expect(json).to include("code") 62 | expect(json["code"]).to eq(link.code) 63 | end 64 | end 65 | 66 | context "when the Link does not exist" do 67 | it "returns not found with an error message in JSON" do 68 | get "/api/v1/links/nocode" 69 | expect(response).to have_http_status(:not_found) 70 | json = JSON.parse(response.body) 71 | expect(json["error"]).to eq("Link non trovato") 72 | end 73 | end 74 | end 75 | end 76 | -------------------------------------------------------------------------------- /spec/controllers/redirects_controller_spec.rb: -------------------------------------------------------------------------------- 1 | require "rails_helper" 2 | 3 | RSpec.describe "Redirects", type: :request do 4 | let(:link_url) { "https://example.com" } 5 | 6 | context "when link exists and is not expired" do 7 | context "and does not require a password" do 8 | let!(:link) { Link.create!(url: link_url, expires_at: 1.day.from_now, code: "DUMMY123") } 9 | 10 | it "redirects to the URL and increments clicks" do 11 | clicks_before = link.clicks || 0 12 | get "/#{link.code}" 13 | expect(response).to have_http_status(302) 14 | expect(response).to redirect_to(link.url) 15 | link.reload 16 | expect(link.clicks).to eq(clicks_before + 1) 17 | end 18 | end 19 | 20 | context "and requires a password" do 21 | let!(:link) { Link.create!(url: link_url, expires_at: 1.day.from_now, code: "PASS1234", password: "secret") } 22 | 23 | it "prompts for authentication if not provided" do 24 | get "/#{link.code}" 25 | expect(response).to have_http_status(401) 26 | end 27 | 28 | it "redirects and increments clicks when correct credentials are provided" do 29 | clicks_before = link.clicks || 0 30 | credentials = ActionController::HttpAuthentication::Basic.encode_credentials(link.code, "secret") 31 | get "/#{link.code}", headers: { "HTTP_AUTHORIZATION" => credentials } 32 | expect(response).to have_http_status(302) 33 | expect(response).to redirect_to(link.url) 34 | link.reload 35 | expect(link.clicks).to eq(clicks_before + 1) 36 | end 37 | end 38 | end 39 | 40 | context "when link is expired" do 41 | let!(:link) do 42 | l = Link.new(url: link_url, expires_at: 1.day.ago, code: "EXPIRED1") 43 | l.save(validate: false) 44 | l 45 | end 46 | 47 | it "renders the 410 gone page" do 48 | get "/#{link.code}" 49 | expect(response).to have_http_status(410) 50 | end 51 | end 52 | 53 | context "when link is not found" do 54 | it "renders a JSON error" do 55 | get "/nonexistent" 56 | expect(response).to have_http_status(:not_found) 57 | json_response = JSON.parse(response.body) 58 | expect(json_response["error"]).to eq("Link not found") 59 | end 60 | end 61 | end 62 | -------------------------------------------------------------------------------- /spec/models/link_spec.rb: -------------------------------------------------------------------------------- 1 | require "rails_helper" 2 | 3 | RSpec.describe Link, type: :model do 4 | let(:valid_url) { "https://example.com" } 5 | let(:valid_attributes) { { url: valid_url, expires_at: 1.day.from_now } } 6 | subject { Link.new(valid_attributes) } 7 | 8 | before do 9 | # Stub Faraday.head to simulate a reachable URL for any URL string. 10 | allow(Faraday).to receive(:head).with(anything).and_return(double(success?: true, status: 200)) 11 | # Stub the route helper to return a dummy short URL. 12 | allow(Rails.application.routes.url_helpers).to receive(:redirect_url).with(any_args) do |code| 13 | "https://short.url/#{code}" 14 | end 15 | end 16 | 17 | describe "validations" do 18 | it "is valid with valid attributes" do 19 | subject.validate 20 | expect(subject.errors).to be_empty 21 | end 22 | 23 | it "requires a URL" do 24 | subject.url = nil 25 | subject.validate 26 | expect(subject.errors[:url]).to include("can't be blank") 27 | end 28 | 29 | it "requires a properly formatted URL" do 30 | subject.url = "invalid_url" 31 | subject.validate 32 | expect(subject.errors[:url]).to include("must be a valid HTTP/HTTPS URL") 33 | end 34 | 35 | it "requires HTTPS protocol" do 36 | subject.url = "http://example.com" 37 | subject.validate 38 | expect(subject.errors[:url]).to include("must use HTTPS protocol") 39 | end 40 | 41 | it "requires code of length 8" do 42 | subject.code = "123" 43 | subject.validate 44 | expect(subject.errors[:code]).to include("is the wrong length (should be 8 characters)") 45 | end 46 | 47 | it "generates a unique code before validation on create" do 48 | new_link = Link.new(valid_attributes) 49 | new_link.validate 50 | expect(new_link.code).to be_present 51 | expect(new_link.code.length).to eq(8) 52 | end 53 | 54 | it "validates expires_at is in the future" do 55 | subject.expires_at = 1.hour.ago 56 | subject.validate 57 | expect(subject.errors[:expires_at]).to include("must be in the future") 58 | end 59 | end 60 | 61 | describe "#expired?" do 62 | it "returns false when expires_at is nil" do 63 | subject.expires_at = nil 64 | expect(subject.expired?).to be false 65 | end 66 | 67 | it "returns true if expired" do 68 | subject.expires_at = 1.hour.ago 69 | expect(subject.expired?).to be true 70 | end 71 | 72 | it "returns false if not expired" do 73 | subject.expires_at = 1.hour.from_now 74 | expect(subject.expired?).to be false 75 | end 76 | end 77 | 78 | describe "#to_param" do 79 | it "returns the code" do 80 | subject.validate 81 | expect(subject.to_param).to eq(subject.code) 82 | end 83 | end 84 | 85 | describe "#to_json" do 86 | it "returns valid JSON with required attributes" do 87 | subject.validate 88 | parsed = JSON.parse(subject.to_json) 89 | expect(parsed).to include("original_url", "short_url", "created_at", "expires_at", "code", "clicks") 90 | end 91 | end 92 | 93 | describe "#short_url" do 94 | it "returns the shortened URL" do 95 | subject.validate 96 | expect(subject.short_url).to eq("https://short.url/#{subject.code}") 97 | end 98 | end 99 | 100 | describe "validate_url_availability" do 101 | context "when the URL is reachable" do 102 | it "does not add an error" do 103 | subject.validate 104 | expect(subject.errors[:url]).to be_empty 105 | end 106 | end 107 | 108 | context "when the URL is unreachable (Faraday error)" do 109 | before do 110 | allow(Faraday).to receive(:head).and_raise(Faraday::Error.new("Connection failed")) 111 | end 112 | 113 | it "adds an error to the URL" do 114 | subject.validate 115 | expect(subject.errors[:url].first).to match(/could not be reached/) 116 | end 117 | end 118 | 119 | context "when the URL returns an unsuccessful status" do 120 | before do 121 | fake_response = double(success?: false, status: 404) 122 | allow(Faraday).to receive(:head).with(anything).and_return(fake_response) 123 | end 124 | 125 | it "adds an error to the URL" do 126 | subject.validate 127 | expect(subject.errors[:url]).to include("could not be verified (HTTP 404)") 128 | end 129 | end 130 | end 131 | end 132 | -------------------------------------------------------------------------------- /spec/rails_helper.rb: -------------------------------------------------------------------------------- 1 | # This file is copied to spec/ when you run 'rails generate rspec:install' 2 | require 'spec_helper' 3 | ENV['RAILS_ENV'] ||= 'test' 4 | require_relative '../config/environment' 5 | # Prevent database truncation if the environment is production 6 | abort("The Rails environment is running in production mode!") if Rails.env.production? 7 | # Uncomment the line below in case you have `--require rails_helper` in the `.rspec` file 8 | # that will avoid rails generators crashing because migrations haven't been run yet 9 | # return unless Rails.env.test? 10 | require 'rspec/rails' 11 | # Add additional requires below this line. Rails is not loaded until this point! 12 | 13 | # Requires supporting ruby files with custom matchers and macros, etc, in 14 | # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are 15 | # run as spec files by default. This means that files in spec/support that end 16 | # in _spec.rb will both be required and run as specs, causing the specs to be 17 | # run twice. It is recommended that you do not name files matching this glob to 18 | # end with _spec.rb. You can configure this pattern with the --pattern 19 | # option on the command line or in ~/.rspec, .rspec or `.rspec-local`. 20 | # 21 | # The following line is provided for convenience purposes. It has the downside 22 | # of increasing the boot-up time by auto-requiring all files in the support 23 | # directory. Alternatively, in the individual `*_spec.rb` files, manually 24 | # require only the support files necessary. 25 | # 26 | # Rails.root.glob('spec/support/**/*.rb').sort_by(&:to_s).each { |f| require f } 27 | 28 | # Checks for pending migrations and applies them before tests are run. 29 | # If you are not using ActiveRecord, you can remove these lines. 30 | begin 31 | ActiveRecord::Migration.maintain_test_schema! 32 | rescue ActiveRecord::PendingMigrationError => e 33 | abort e.to_s.strip 34 | end 35 | RSpec.configure do |config| 36 | # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 37 | config.fixture_paths = [ 38 | Rails.root.join('spec/fixtures') 39 | ] 40 | 41 | # If you're not using ActiveRecord, or you'd prefer not to run each of your 42 | # examples within a transaction, remove the following line or assign false 43 | # instead of true. 44 | config.use_transactional_fixtures = true 45 | 46 | # You can uncomment this line to turn off ActiveRecord support entirely. 47 | # config.use_active_record = false 48 | 49 | # RSpec Rails uses metadata to mix in different behaviours to your tests, 50 | # for example enabling you to call `get` and `post` in request specs. e.g.: 51 | # 52 | # RSpec.describe UsersController, type: :request do 53 | # # ... 54 | # end 55 | # 56 | # The different available types are documented in the features, such as in 57 | # https://rspec.info/features/7-1/rspec-rails 58 | # 59 | # You can also this infer these behaviours automatically by location, e.g. 60 | # /spec/models would pull in the same behaviour as `type: :model` but this 61 | # behaviour is considered legacy and will be removed in a future version. 62 | # 63 | # To enable this behaviour uncomment the line below. 64 | # config.infer_spec_type_from_file_location! 65 | 66 | # Filter lines from Rails gems in backtraces. 67 | config.filter_rails_from_backtrace! 68 | # arbitrary gems may also be filtered via: 69 | # config.filter_gems_from_backtrace("gem name") 70 | end 71 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # This file was generated by the `rails generate rspec:install` command. Conventionally, all 2 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 3 | # The generated `.rspec` file contains `--require spec_helper` which will cause 4 | # this file to always be loaded, without a need to explicitly require it in any 5 | # files. 6 | # 7 | # Given that it is always loaded, you are encouraged to keep this file as 8 | # light-weight as possible. Requiring heavyweight dependencies from this file 9 | # will add to the boot time of your test suite on EVERY test run, even for an 10 | # individual file that may not need all of that loaded. Instead, consider making 11 | # a separate helper file that requires the additional dependencies and performs 12 | # the additional setup, and require it from the spec files that actually need 13 | # it. 14 | # 15 | # See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 16 | RSpec.configure do |config| 17 | # rspec-expectations config goes here. You can use an alternate 18 | # assertion/expectation library such as wrong or the stdlib/minitest 19 | # assertions if you prefer. 20 | config.expect_with :rspec do |expectations| 21 | # This option will default to `true` in RSpec 4. It makes the `description` 22 | # and `failure_message` of custom matchers include text for helper methods 23 | # defined using `chain`, e.g.: 24 | # be_bigger_than(2).and_smaller_than(4).description 25 | # # => "be bigger than 2 and smaller than 4" 26 | # ...rather than: 27 | # # => "be bigger than 2" 28 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 29 | end 30 | 31 | # rspec-mocks config goes here. You can use an alternate test double 32 | # library (such as bogus or mocha) by changing the `mock_with` option here. 33 | config.mock_with :rspec do |mocks| 34 | # Prevents you from mocking or stubbing a method that does not exist on 35 | # a real object. This is generally recommended, and will default to 36 | # `true` in RSpec 4. 37 | mocks.verify_partial_doubles = true 38 | end 39 | 40 | # This option will default to `:apply_to_host_groups` in RSpec 4 (and will 41 | # have no way to turn it off -- the option exists only for backwards 42 | # compatibility in RSpec 3). It causes shared context metadata to be 43 | # inherited by the metadata hash of host groups and examples, rather than 44 | # triggering implicit auto-inclusion in groups with matching metadata. 45 | config.shared_context_metadata_behavior = :apply_to_host_groups 46 | 47 | # The settings below are suggested to provide a good initial experience 48 | # with RSpec, but feel free to customize to your heart's content. 49 | =begin 50 | # This allows you to limit a spec run to individual examples or groups 51 | # you care about by tagging them with `:focus` metadata. When nothing 52 | # is tagged with `:focus`, all examples get run. RSpec also provides 53 | # aliases for `it`, `describe`, and `context` that include `:focus` 54 | # metadata: `fit`, `fdescribe` and `fcontext`, respectively. 55 | config.filter_run_when_matching :focus 56 | 57 | # Allows RSpec to persist some state between runs in order to support 58 | # the `--only-failures` and `--next-failure` CLI options. We recommend 59 | # you configure your source control system to ignore this file. 60 | config.example_status_persistence_file_path = "spec/examples.txt" 61 | 62 | # Limits the available syntax to the non-monkey patched syntax that is 63 | # recommended. For more details, see: 64 | # https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/ 65 | config.disable_monkey_patching! 66 | 67 | # Many RSpec users commonly either run the entire suite or an individual 68 | # file, and it's useful to allow more verbose output when running an 69 | # individual spec file. 70 | if config.files_to_run.one? 71 | # Use the documentation formatter for detailed output, 72 | # unless a formatter has already been configured 73 | # (e.g. via a command-line flag). 74 | config.default_formatter = "doc" 75 | end 76 | 77 | # Print the 10 slowest examples and example groups at the 78 | # end of the spec run, to help surface which specs are running 79 | # particularly slow. 80 | config.profile_examples = 10 81 | 82 | # Run specs in random order to surface order dependencies. If you find an 83 | # order dependency and want to debug it, you can fix the order by providing 84 | # the seed, which is printed after each run. 85 | # --seed 1234 86 | config.order = :random 87 | 88 | # Seed global randomization in this process using the `--seed` CLI option. 89 | # Setting this allows you to use `--seed` to deterministically reproduce 90 | # test failures related to randomization by passing the same `--seed` value 91 | # as the one that triggered the failure. 92 | Kernel.srand config.seed 93 | =end 94 | end 95 | -------------------------------------------------------------------------------- /storage/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/storage/.keep -------------------------------------------------------------------------------- /test/application_system_test_case.rb: -------------------------------------------------------------------------------- 1 | require "test_helper" 2 | 3 | class ApplicationSystemTestCase < ActionDispatch::SystemTestCase 4 | driven_by :selenium, using: :headless_chrome, screen_size: [ 1400, 1400 ] 5 | end 6 | -------------------------------------------------------------------------------- /test/controllers/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/test/controllers/.keep -------------------------------------------------------------------------------- /test/fixtures/files/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/test/fixtures/files/.keep -------------------------------------------------------------------------------- /test/fixtures/links.yml: -------------------------------------------------------------------------------- 1 | # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html 2 | 3 | one: 4 | url: MyString 5 | password: MyString 6 | expires_at: 2025-02-23 18:42:04 7 | code: MyString 8 | clicks: 1 9 | 10 | two: 11 | url: MyString 12 | password: MyString 13 | expires_at: 2025-02-23 18:42:04 14 | code: MyString 15 | clicks: 1 16 | -------------------------------------------------------------------------------- /test/helpers/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/test/helpers/.keep -------------------------------------------------------------------------------- /test/integration/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/test/integration/.keep -------------------------------------------------------------------------------- /test/mailers/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/test/mailers/.keep -------------------------------------------------------------------------------- /test/models/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/test/models/.keep -------------------------------------------------------------------------------- /test/models/link_test.rb: -------------------------------------------------------------------------------- 1 | require "test_helper" 2 | 3 | class LinkTest < ActiveSupport::TestCase 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /test/system/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/test/system/.keep -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | ENV["RAILS_ENV"] ||= "test" 2 | require_relative "../config/environment" 3 | require "rails/test_help" 4 | 5 | module ActiveSupport 6 | class TestCase 7 | # Run tests in parallel with specified workers 8 | parallelize(workers: :number_of_processors) 9 | 10 | # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. 11 | fixtures :all 12 | 13 | # Add more helper methods to be used by all tests here... 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /tmp/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/tmp/.keep -------------------------------------------------------------------------------- /tmp/pids/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/tmp/pids/.keep -------------------------------------------------------------------------------- /tmp/storage/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/tmp/storage/.keep -------------------------------------------------------------------------------- /vendor/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/vendor/.keep -------------------------------------------------------------------------------- /vendor/javascript/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidesantangelo/yll/7d6222bc4cf9e2b7b1e826208106682f87a569b0/vendor/javascript/.keep --------------------------------------------------------------------------------