Module: Simple::Httpd::CLI

Includes:
CLI
Defined in:
lib/simple/httpd/cli.rb

Instance Method Summary collapse

Instance Method Details

#main(*mount_specs, environment: "development", services: nil) ⇒ Object

Runs a simple httpd server

A mount_spec is either the location of a directory, which would then be mounted at the “/” HTTP location, or a directory followed by a colon and where to mount the directory.

Mounted directories might contain either ruby source code which is then executed or static files to be delivered verbatim. See README.md for more details.

Examples:

simple-httpd --port=8080 httpd/root --service=src/to/service.rb MyService:/ httpd/assets:assets

serves the content of ./httpd/root on 0.0.0.0/ and the content of httpd/assets on 0.0.0.0/assets.

Options:

--environment=ENV         ... the environment setting, which adjusts configuration.
--services=<path>,<path>  ... load these ruby file during startup. Used to define service objects.

simple-httpd respects the HOST and PORT environment values to determine the interface and port to listen to. Default values are “127.0.0.1” and 8181.

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:/”)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/simple/httpd/cli.rb', line 43

def main(*mount_specs, environment: "development", services: nil)
  ::Simple::Httpd.env = environment

  start_simplecov if environment == "test"

  mount_specs << "." if mount_specs.empty?

  host = ENV["HOST"] || "127.0.0.1"
  port = Integer(ENV["PORT"] || 8181)

  # late loading simple/httpd, for simplecov support
  require "simple/httpd"
  helpers = ::Simple::Httpd::Helpers

  services&.split(",")&.each do |service_path|
    paths = if Dir.exist?(service_path)
              Dir.glob("#{service_path}/**/*.rb").sort
            else
              [service_path]
            end

    paths.each do |path|
      logger.info "Loading service(s) from #{helpers.shorten_path path}"
      load path
    end
  end

  ::Simple::Httpd.listen!(*mount_specs, environment: environment,
                                        host: host,
                                        port: port,
                                        logger: logger)
end