Class: Wytch::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/wytch/server.rb

Overview

Development server for Wytch sites.

The Server provides a local development environment with:

  • Hot reloading of site code and content
  • Static file serving from public/
  • Page rendering on each request

Examples:

Starting via CLI

$ wytch server
$ wytch server --port 3000 --host 0.0.0.0

Starting programmatically

Wytch::Server.new(port: 3000).start

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Creates a new server instance.

Parameters:

  • options (Hash) (defaults to: {})

    server options

Options Hash (options):

  • :port (Integer)

    the port to listen on (default: 6969)

  • :host (String)

    the host to bind to (default: "localhost")



28
29
30
# File 'lib/wytch/server.rb', line 28

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsHash (readonly)

Returns server options (port, host).

Returns:

  • (Hash)

    server options (port, host)



21
22
23
# File 'lib/wytch/server.rb', line 21

def options
  @options
end

Instance Method Details

#startvoid

This method returns an undefined value.

Starts the development server.

Loads the site configuration, starts file watchers for hot reloading, and begins serving requests. This method blocks until the server is stopped.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wytch/server.rb', line 38

def start
  ENV["RACK_ENV"] = "development"
  Site.load!

  require "puma"
  require "puma/server"

  port = options[:port] || 6969
  host = options[:host] || "localhost"

  puts "Starting Wytch development server on http://#{host}:#{port}"
  puts "Press Ctrl+C to stop"

  server = Puma::Server.new(app)
  server.add_tcp_listener(host, port)
  server.run.join
end