Class: Lifer::Dev::Server

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

Overview

This server is used in development and test modes to preview and serve a Lifer project. It’s for convenience and is not super sophisticated. The server wraps a Puma process with some reasonable, default settings. It also listens for file changes and rebuilds the project on the next request made to the web server.

Constant Summary collapse

DEFAULT_PORT =

The default port to run the Puma server on.

9292

Class Method Summary collapse

Class Method Details

.rack_appArray

A proc that follows the [Rack server specification]. Because we don’t want to commit a rackup configuration file at this time, any “middleware” we want to insert should be a part of this method.

[1]: github.com/rack/rack/blob/main/SPEC.rdoc

Returns:

  • (Array)

    A Rack server-compatible array.



51
52
53
54
55
56
# File 'lib/lifer/dev/server.rb', line 51

def rack_app
  -> (env) {
    reload!
    router.response_for(env)
  }
end

.start!(port:) ⇒ void

This method returns an undefined value.

Start a Puma server to preview your Lifer project locally.

Parameters:

  • port (Integer)

    The port to start the Puma server with.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lifer/dev/server.rb', line 29

def start!(port:)
  puma_configuration = Puma::Configuration.new do |config|
    config.app rack_app
    config.bind "tcp://127.0.0.1:#{port || DEFAULT_PORT}"
    config.environment "development"
    config.log_requests true
  end

  Lifer.build!(environment: :serve)

  listener.start

  Puma::Launcher.new(puma_configuration).run
end