Module: Simple::Httpd::CLI

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

Instance Method Summary collapse

Instance Method Details

#loggerObject



9
10
11
# File 'lib/simple/httpd/cli.rb', line 9

def logger
  ::Simple::CLI.logger
end

#routes(*mounts, environment: "development", services: nil) ⇒ Object

rubocop:disable Metrics/AbcSize



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/simple/httpd/cli.rb', line 59

def routes(*mounts, environment: "development", services: nil)
  prepare_environment!(environment: environment)
  app = build_app!(mounts: mounts, services: services)
  routes = app.route_descriptions

  logger.info "Found #{routes.count} routes"

  max_verb_len = routes.map(&:verb).map(&:length).max
  max_path_len = routes.map(&:path).map(&:length).max

  routes
    .sort_by { |route| [route.path, route.verb] }
    .each do |route|
      puts format("%#{max_verb_len}s %-#{max_path_len}s %s", route.verb, route.path, route.source_location_str)
    end
end

#start(*mounts, environment: "development", services: nil) ⇒ Object

Runs a simple httpd server

A mount 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:

PORT=8080 simple-httpd start 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 files or directories during startup. This
                              can be 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:/”)



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/simple/httpd/cli.rb', line 45

def start(*mounts, environment: "development", services: nil)
  host = ENV["HOST"] || "127.0.0.1"
  port = Integer(ENV["PORT"] || 8181)

  prepare_environment!(environment: environment)

  app = build_app!(mounts: mounts, services: services)
  logger.info "start to listen on #{mounts.inspect}"
  ::Simple::Httpd.listen!(app, environment: environment,
                               host: host,
                               port: port)
end