Skip to content

Commit ad0ccb1

Browse files
committed
feat: Add Puma server with HTTP Basic auth in front of Jekyll site
1 parent 10fdd64 commit ad0ccb1

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

sites/site-with-errors/Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ gem "wdm", "~> 0.1", :platforms => [:mingw, :x64_mingw, :mswin]
3131
# Lock `http_parser.rb` gem to `v0.6.x` on JRuby builds since newer versions of the gem
3232
# do not have a Java counterpart.
3333
gem "http_parser.rb", "~> 0.6.0", :platforms => [:jruby]
34+
35+
# Web server
36+
gem "rack", "~> 3.2"
37+
gem "puma", "~> 6.0"

sites/site-with-errors/Gemfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,13 @@ GEM
9595
jekyll (>= 3.5, < 5.0)
9696
jekyll-feed (~> 0.9)
9797
jekyll-seo-tag (~> 2.1)
98+
nio4r (2.7.4)
9899
pathutil (0.16.2)
99100
forwardable-extended (~> 2.6)
100101
public_suffix (6.0.2)
102+
puma (6.6.1)
103+
nio4r (~> 2.0)
104+
rack (3.2.1)
101105
rake (13.3.0)
102106
rb-fsevent (0.11.2)
103107
rb-inotify (0.11.1)
@@ -167,6 +171,8 @@ DEPENDENCIES
167171
jekyll (~> 4.4.1)
168172
jekyll-feed (~> 0.12)
169173
minima (~> 2.5)
174+
puma (~> 6.0)
175+
rack (~> 3.2)
170176
tzinfo (>= 1, < 3)
171177
tzinfo-data
172178
wdm (~> 0.1)

sites/site-with-errors/config.ru

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require 'rack'
2+
require 'rack/auth/basic'
3+
require 'rack/static'
4+
5+
# Basic authentication middleware
6+
use Rack::Auth::Basic, "Protected Area" do |username, password|
7+
expected_username = ENV['TEST_USERNAME']
8+
expected_password = ENV['TEST_PASSWORD']
9+
10+
# Check if environment variables are set
11+
if expected_username.nil? || expected_password.nil?
12+
puts "Warning: TEST_USERNAME and/or TEST_PASSWORD environment variables not set"
13+
false
14+
else
15+
username == expected_username && password == expected_password
16+
end
17+
end
18+
19+
# Serve static files from _site directory
20+
use Rack::Static,
21+
urls: %w[/],
22+
root: File.expand_path('_site', __dir__),
23+
index: 'index.html'
24+
25+
# Fallback for requests that don't match static files
26+
run lambda { |env|
27+
path = env['PATH_INFO']
28+
29+
# Try to serve the requested file
30+
file_path = File.join(File.expand_path('_site', __dir__), path)
31+
32+
# If it's a directory, try to serve index.html
33+
if File.directory?(file_path)
34+
index_path = File.join(file_path, 'index.html')
35+
if File.exist?(index_path)
36+
[200, {'Content-Type' => 'text/html'}, [File.read(index_path)]]
37+
else
38+
[404, {'Content-Type' => 'text/html'}, [File.read(File.join(File.expand_path('_site', __dir__), '404.html'))]]
39+
end
40+
elsif File.exist?(file_path)
41+
content_type = case File.extname(file_path)
42+
when '.html' then 'text/html'
43+
when '.css' then 'text/css'
44+
when '.js' then 'application/javascript'
45+
when '.xml' then 'application/xml'
46+
else 'text/plain'
47+
end
48+
[200, {'Content-Type' => content_type}, [File.read(file_path)]]
49+
else
50+
# Serve 404 page
51+
[404, {'Content-Type' => 'text/html'}, [File.read(File.join(File.expand_path('_site', __dir__), '404.html'))]]
52+
end
53+
}

0 commit comments

Comments
 (0)