]> Lady’s Gitweb - HOMEDIR/blob - rb/public-serve.rb
Initial commit
[HOMEDIR] / rb / public-serve.rb
1 #!/usr/bin/ruby
2 # @(#)🏠📂 HOMEDIR rb/public-serve.rb 2026-03-22T02:03:39Z
3 # SPDX-FileCopyrightText: 2025, 2026 Lady <https://www.ladys.computer/about/#lady>
4 # SPDX-License-Identifier: CC0-1.0
5
6 ## ⁌ Public File Server
7 ##
8 ## This script offers a profoundly simple file server which uses
9 ## `index.xhtml´ as its index file.
10 ## It is written in what is hopefully a portable Ruby that will work
11 ## across most installations.
12 ## Ruby was chosen because Webrick is generally available.
13
14 require "webrick"
15
16 ## The first argument is the desired localhost port, defaulting to
17 ## 3000.
18 ## The second argument is the directory to serve.
19 ## This can also be provided with the `path_PUBDIR´ environment
20 ## variable.
21 ## The default directory is `@public.tmp´.
22
23 PORT = ARGV[0] || 3000
24
25 server = WEBrick::HTTPServer.new(Port: PORT,
26 DocumentRoot: ARGV[1] || ENV["path_PUBDIR"] || "@public.tmp",
27 DirectoryIndex: ["index.xhtml"])
28 trap 'INT' do server.shutdown end
29
30 ## A minimal X·H·T·M·L file handler is provided which simply serves
31 ## files with the appropriate content type.
32
33 class XHTMLHandler < WEBrick::HTTPServlet::DefaultFileHandler
34 def do_GET request, response
35 super
36 response['Content-Type'] = 'application/xhtml+xml'
37 end
38 end
39 WEBrick::HTTPServlet::FileHandler.add_handler("xhtml", XHTMLHandler)
40
41 ## A notice is printed before starting the server to clarify usage.
42
43 $stderr.puts <<INFOTEXT
44 \e[1m
45 \e[7m NOTE: \e[27m Visit <http://localhost:#{PORT}/> to view the site.
46 \e[7m \e[27m
47 \e[7m \e[27m Press control + C to exit.
48 \e[22m
49 INFOTEXT
50 server.start
This page took 0.05398 seconds and 5 git commands to generate.