This commit is contained in:
Artur Gurgul 2025-08-04 19:07:10 +02:00
commit 793560c0c4
7 changed files with 97 additions and 0 deletions

13
Gemfile Normal file
View file

@ -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"

24
Gemfile.lock Normal file
View file

@ -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

3
Readme.md Normal file
View file

@ -0,0 +1,3 @@
bundle install
bundle exec rackup

21
app.rb Normal file
View file

@ -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

15
auth_middleware.rb Normal file
View file

@ -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

3
config.ru Normal file
View file

@ -0,0 +1,3 @@
require_relative './app'
run App.freeze.app

18
secure_app.rb Normal file
View file

@ -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