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