Class: Roxane::StaticServer

Inherits:
Object
  • Object
show all
Defined in:
lib/roxane/static_server.rb

Overview

Tiny loopback-only static file server — dependency-free (stdlib sockets). It serves a built frontend bundle to the embedded webview over a real http:// origin, so ES modules / fetch / SPA routing behave (which file:// breaks), and the desktop path mirrors the hosted-web one (same frontend, local origin). GET-only, bound to 127.0.0.1, path-traversal-safe.

Constant Summary collapse

MIME =
{
  ".html" => "text/html", ".htm" => "text/html",
  ".js" => "text/javascript", ".mjs" => "text/javascript",
  ".css" => "text/css", ".json" => "application/json", ".map" => "application/json",
  ".svg" => "image/svg+xml", ".png" => "image/png", ".jpg" => "image/jpeg",
  ".jpeg" => "image/jpeg", ".gif" => "image/gif", ".webp" => "image/webp",
  ".ico" => "image/x-icon", ".woff" => "font/woff", ".woff2" => "font/woff2",
  ".ttf" => "font/ttf", ".wasm" => "application/wasm", ".txt" => "text/plain"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, spa: true) ⇒ StaticServer

Returns a new instance of StaticServer.



25
26
27
28
# File 'lib/roxane/static_server.rb', line 25

def initialize(root, spa: true)
  @root = File.expand_path(root)
  @spa = spa
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



23
24
25
# File 'lib/roxane/static_server.rb', line 23

def port
  @port
end

#rootObject (readonly)

Returns the value of attribute root.



23
24
25
# File 'lib/roxane/static_server.rb', line 23

def root
  @root
end

Instance Method Details

#startObject

Start on an ephemeral loopback port in a background thread; returns base URL.



31
32
33
34
35
36
# File 'lib/roxane/static_server.rb', line 31

def start
  @server = TCPServer.new("127.0.0.1", 0)
  @port = @server.addr[1]
  @thread = Thread.new { accept_loop }
  "http://127.0.0.1:#{@port}/"
end

#stopObject



38
39
40
41
# File 'lib/roxane/static_server.rb', line 38

def stop
  @server&.close
  @thread&.kill
end