Class: Simple::Httpd

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/simple/httpd/server.rb,
lib/simple/httpd.rb,
lib/simple/httpd.rb,
lib/simple/httpd/cli.rb,
lib/simple/httpd/version.rb

Overview

rubocop:disable Metrics/PerceivedComplexity, Metrics/AbcSize

Defined Under Namespace

Modules: CLI, GemHelper, Helpers, Mount, Rack, RouteDescriptions, Server, ServiceAdapter Classes: BaseController, Route

Constant Summary collapse

SELF =
self
VERSION =
GemHelper.version "postjob"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.envObject

Returns the value of attribute env.



4
5
6
# File 'lib/simple/httpd/cli.rb', line 4

def env
  @env
end

Class Method Details

.build(*mounts) ⇒ Object

Converts the passed in arguments into a Simple::Httpd application.

For a description of mounts see #add



67
68
69
# File 'lib/simple/httpd.rb', line 67

def self.build(*mounts)
  new(*mounts)
end

.custom_logger?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/simple/httpd.rb', line 31

def self.custom_logger?
  @logger && @logger != ::Simple::CLI.logger
end

.listen!(*mounts, environment: "development", host: nil, port:, &block) ⇒ Object

Converts the passed in args into a Simple::Httpd application.

The passed in arguments are used to create a Simple::Httpd object. If the function receives a rack app (determined by the ability to respond to call/3) it redirects to Server.listen! right away - this way this method can be used as a helper method to easily start a Rack server.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/simple/httpd.rb', line 46

def self.listen!(*mounts, environment: "development", host: nil, port:, &block)
  # If there is no argument but a block use the block as a rack server
  if block
    raise ArgumentError, "Can't deal w/block *and* mounts" unless mounts.empty?

    app = block
  elsif mounts.length == 1 && mounts.first.respond_to?(:call)
    # there is one argument, and that looks like a Rack app: return that.
    app = mounts.first
  else
    # Build a Httpd app, and listen
    app = build(*mounts)
    app.rack
  end

  Server.listen!(app, environment: environment, host: host, port: port)
end

.loggerObject

returns a logger for Simple::Httpd.

Initially we default to ::Simple::CLI.logger. This gives colored logging during loading and mounting. Note that Simple::Httpd::Server builds its own logger instance to to pass that along to the web server.



27
28
29
# File 'lib/simple/httpd.rb', line 27

def self.logger
  @logger ||= ::Simple::CLI.logger
end

.logger=(logger) ⇒ Object



35
36
37
# File 'lib/simple/httpd.rb', line 35

def self.logger=(logger)
  @logger = logger
end

Instance Method Details

#mount(mount, at: nil) ⇒ Object

Adds one or more mount_points

Each entry in mounts can be either:

  • a mount_point [ mount_point, path ], e.g. [ "path/to/root", "/"]

  • a string denoting a mount_point, e.g. “path/to/root:/”)

  • a string denoting a “/” mount_point (e.g. “path”, which is shorthand for “path:/”)

Raises:

  • (ArgumentError)


97
98
99
100
101
# File 'lib/simple/httpd.rb', line 97

def mount(mount, at: nil)
  raise ArgumentError, "Cannot mount onto an already built app" if built?

  @mounts << Mount.build(mount, at: at)
end

#rackObject



106
107
108
# File 'lib/simple/httpd.rb', line 106

def rack
  @rack ||= build_rack
end

#route_descriptionsObject



83
84
85
86
87
# File 'lib/simple/httpd.rb', line 83

def route_descriptions
  @mounts.inject([]) do |ary, mount|
    ary.concat mount.route_descriptions
  end
end