diff options
| author | mo khan <mo@mokhan.ca> | 2025-12-11 16:50:23 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-12-11 16:50:23 -0700 |
| commit | 2e13b50d3205e214f8ab6c64e3e69eb0ae72b938 (patch) | |
| tree | ba5ab4539d1d8388e34dcb7f444165995300c760 /lib/gitem/server.rb | |
| parent | de14376f33de34e27176a7492050ac1f99867648 (diff) | |
Diffstat (limited to 'lib/gitem/server.rb')
| -rw-r--r-- | lib/gitem/server.rb | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/lib/gitem/server.rb b/lib/gitem/server.rb index c73ff56..82edc90 100644 --- a/lib/gitem/server.rb +++ b/lib/gitem/server.rb @@ -14,9 +14,30 @@ module Gitem puts "🌐 Server running at #{@url}" puts " Press Ctrl+C to stop\n\n" server = WEBrick::HTTPServer.new( - Port: @port, DocumentRoot: @root, - Logger: WEBrick::Log.new($stderr, WEBrick::Log::WARN), AccessLog: [] + Port: @port, + Logger: WEBrick::Log.new($stderr, WEBrick::Log::WARN), + AccessLog: [] ) + + server.mount_proc '/' do |req, res| + path = File.join(@root, req.path) + path = File.join(path, 'index.html') if File.directory?(path) + + if File.exist?(path) && !File.directory?(path) + res.body = File.read(path) + res.content_type = WEBrick::HTTPUtils.mime_type(path, WEBrick::HTTPUtils::DefaultMimeTypes) + else + index_path = File.join(@root, 'index.html') + if File.exist?(index_path) + res.body = File.read(index_path) + res.content_type = 'text/html' + else + res.status = 404 + res.body = 'Not Found' + end + end + end + trap("INT") { server.shutdown } trap("TERM") { server.shutdown } server.start |
