From 793560c0c4683c15c1f346bbfc5bab32f3d06889 Mon Sep 17 00:00:00 2001 From: Artur Gurgul Date: Mon, 4 Aug 2025 19:07:10 +0200 Subject: [PATCH] init --- Gemfile | 13 +++++++++++++ Gemfile.lock | 24 ++++++++++++++++++++++++ Readme.md | 3 +++ app.rb | 21 +++++++++++++++++++++ auth_middleware.rb | 15 +++++++++++++++ config.ru | 3 +++ secure_app.rb | 18 ++++++++++++++++++ 7 files changed, 97 insertions(+) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Readme.md create mode 100644 app.rb create mode 100644 auth_middleware.rb create mode 100644 config.ru create mode 100644 secure_app.rb diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..2ac5d48 --- /dev/null +++ b/Gemfile @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +# gem "rails" + +gem "roda", "~> 3.94" + +gem "rack", "~> 3.2" + +gem "rackup", "~> 2.2" + +gem "puma", "~> 6.6" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..52a55b9 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,24 @@ +GEM + remote: https://rubygems.org/ + specs: + nio4r (2.7.4) + puma (6.6.1) + nio4r (~> 2.0) + rack (3.2.0) + rackup (2.2.1) + rack (>= 3) + roda (3.94.0) + rack + +PLATFORMS + arm64-darwin-24 + ruby + +DEPENDENCIES + puma (~> 6.6) + rack (~> 3.2) + rackup (~> 2.2) + roda (~> 3.94) + +BUNDLED WITH + 2.6.9 diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..bf06abf --- /dev/null +++ b/Readme.md @@ -0,0 +1,3 @@ + +bundle install +bundle exec rackup diff --git a/app.rb b/app.rb new file mode 100644 index 0000000..f1ff2f7 --- /dev/null +++ b/app.rb @@ -0,0 +1,21 @@ +require 'roda' +require_relative './auth_middleware' +require_relative './secure_app' + +class App < Roda + route do |r| + r.root do + "Welcome to the public API" + end + + r.get "hello" do + { message: "Public hello" }.to_json + end + + # ✅ Apply middleware to the entire /secure/* route + r.on "secure" do + r.run AuthMiddleware.new(SecureApp.freeze.app) + end + end +end + diff --git a/auth_middleware.rb b/auth_middleware.rb new file mode 100644 index 0000000..a43c2cd --- /dev/null +++ b/auth_middleware.rb @@ -0,0 +1,15 @@ +class AuthMiddleware + def initialize(app) + @app = app + end + + def call(env) + auth = env['HTTP_AUTHORIZATION'] + if auth == "Bearer mysecrettoken" + @app.call(env) + else + [401, { "Content-Type" => "application/json" }, [{ error: "Unauthorized" }.to_json]] + end + end +end + diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..56d4a19 --- /dev/null +++ b/config.ru @@ -0,0 +1,3 @@ +require_relative './app' +run App.freeze.app + diff --git a/secure_app.rb b/secure_app.rb new file mode 100644 index 0000000..5c1d8e4 --- /dev/null +++ b/secure_app.rb @@ -0,0 +1,18 @@ +require 'roda' +require 'json' + +class SecureApp < Roda + route do |r| + r.on "note" do + r.get String do |id| + response['Content-Type'] = 'application/json' + { id: id, content: "This is a secure note #{id}" }.to_json + end + end + + r.get "status" do + { status: "You are authenticated" }.to_json + end + end +end +