#!/usr/bin/ruby
# @(#)🏠📂 HOMEDIR	rb/public-serve.rb	2026-03-22T02:03:39Z
# SPDX-FileCopyrightText: 2025, 2026 Lady <https://www.ladys.computer/about/#lady>
# SPDX-License-Identifier: CC0-1.0

## ⁌ Public File Server
##
## This script offers a profoundly simple file server which uses
##   `index.xhtml´ as its index file.
## It is written in what is hopefully a portable Ruby that will work
##   across most installations.
## Ruby was chosen because Webrick is generally available.

require "webrick"

## The first argument is the desired localhost port, defaulting to
##   3000.
## The second argument is the directory to serve.
## This can also be provided with the `path_PUBDIR´ environment
##   variable.
## The default directory is `@public.tmp´.

PORT = ARGV[0] || 3000

server = WEBrick::HTTPServer.new(Port: PORT,
  DocumentRoot: ARGV[1] || ENV["path_PUBDIR"] || "@public.tmp",
  DirectoryIndex: ["index.xhtml"])
trap 'INT' do server.shutdown end

## A minimal X·H·T·M·L file handler is provided which simply serves
##   files with the appropriate content type.

class XHTMLHandler < WEBrick::HTTPServlet::DefaultFileHandler
  def do_GET request, response
    super
    response['Content-Type'] = 'application/xhtml+xml'
  end
end
WEBrick::HTTPServlet::FileHandler.add_handler("xhtml", XHTMLHandler)

## A notice is printed before starting the server to clarify usage.

$stderr.puts <<INFOTEXT
\e[1m
\e[7m NOTE: \e[27m Visit <http://localhost:#{PORT}/> to view the site.
\e[7m       \e[27m
\e[7m       \e[27m Press control + C to exit.
\e[22m
INFOTEXT
server.start
