Module: Tipi

Defined in:
lib/tipi/cli.rb,
lib/tipi/acme.rb,
lib/tipi/handler.rb,
lib/tipi/version.rb,
lib/tipi/websocket.rb,
lib/tipi/config_dsl.rb,
lib/tipi/supervisor.rb,
lib/tipi/http2_stream.rb,
lib/tipi/rack_adapter.rb,
lib/tipi/configuration.rb,
lib/tipi/http1_adapter.rb,
lib/tipi/response_extensions.rb,
lib/tipi/controller/web_stock.rb,
lib/tipi/controller/bare_stock.rb,
lib/tipi/controller/extensions.rb,
lib/tipi/controller/web_polyphony.rb,
lib/tipi/controller/stock_http1_adapter.rb,
lib/tipi/http2_adapter.rb,
lib/tipi.rb

Defined Under Namespace

Modules: ACME, Apps, CLI, Configuration, RackAdapter, ResponseExtensions, Supervisor Classes: Connection, Controller, DefaultHandler, HTTP1Adapter, HTTP1Connection, HTTP2Adapter, HTTP2StreamHandler, Listener, StockHTTP1Adapter, Websocket

Constant Summary collapse

DEFAULT_OPTS =
{
  app_type: :web,
  mode: :polyphony,
  workers: 1,
  threads: 1,
  listen: ['http', 'localhost', 1234],
  path: '.',
}
VERSION =
'0.55'
ALPN_PROTOCOLS =
%w[h2 http/1.1].freeze
H2_PROTOCOL =
'h2'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.appObject



15
16
17
18
19
# File 'lib/tipi/controller/extensions.rb', line 15

def app
  return @app if @app

  raise 'No app define. The app to run should be set using `Tipi.app = ...`'
end

Class Method Details

.accept_loop(server, opts, &handler) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/tipi.rb', line 39

def accept_loop(server, opts, &handler)
  server.accept_loop do |client|
    spin { client_loop(client, opts, &handler) }
  rescue OpenSSL::SSL::SSLError
    # disregard
  end
end

.client_loop(client, opts, &handler) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/tipi.rb', line 47

def client_loop(client, opts, &handler)
  client.no_delay if client.respond_to?(:no_delay)
  adapter = protocol_adapter(client, opts)
  adapter.each(&handler)
rescue SystemCallError
  # disregard
ensure
  client.close rescue nil
end

.listen(host, port, opts = {}) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/tipi.rb', line 30

def listen(host, port, opts = {})
  opts[:alpn_protocols] = ALPN_PROTOCOLS
  Polyphony::Net.tcp_listen(host, port, opts).tap do |socket|
    socket.define_singleton_method(:each) do |&block|
      ::Tipi.accept_loop(socket, opts, &block)
    end
  end
end

.opts_from_argv(argv) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tipi/cli.rb', line 18

def self.opts_from_argv(argv)
  opts = DEFAULT_OPTS.dup
  parser = OptionParser.new do |o|
    o.banner = "Usage: tipi [options] path"
    o.on('-h', '--help', 'Show this help') { puts o; exit }
    o.on('-wNUM', '--workers NUM', 'Number of worker processes (default: 1)') do |v|
      opts[:workers] = v
    end
    o.on('-tNUM', '--threads NUM', 'Number of worker threads (default: 1)') do |v|
      opts[:threads] = v
      opts[:mode] = :stock
    end
    o.on('-c', '--compatibility', 'Use compatibility mode') do
      opts[:mode] = :stock
    end
    o.on('-lSPEC', '--listen SPEC', 'Setup HTTP listener') do |v|
      opts[:listen] = parse_listen_spec('http', v)
    end
    o.on('-sSPEC', '--secure SPEC', 'Setup HTTPS listener (for localhost)') do |v|
      opts[:listen] = parse_listen_spec('https', v)
    end
    o.on('-fSPEC', '--full-service SPEC', 'Setup HTTP/HTTPS listeners (with automatic certificates)') do |v|
      opts[:listen] = parse_listen_spec('full', v)
    end
    o.on('-v', '--verbose', 'Verbose output') do
      opts[:verbose] = true
    end
  end.parse!(argv)
  opts[:path] = argv.shift unless argv.empty?
  verify_path(opts[:path])
  opts
end

.parse_listen_spec(type, spec) ⇒ Object



51
52
53
# File 'lib/tipi/cli.rb', line 51

def self.parse_listen_spec(type, spec)
  [type, *spec.split(':').map { |s| str_to_native_type(s) }]
end

.protocol_adapter(socket, opts) ⇒ Object



57
58
59
60
61
62
# File 'lib/tipi.rb', line 57

def protocol_adapter(socket, opts)
  use_http2 = socket.respond_to?(:alpn_protocol) &&
              socket.alpn_protocol == H2_PROTOCOL
  klass = use_http2 ? HTTP2Adapter : HTTP1Adapter
  klass.new(socket, opts)
end

.route(&block) ⇒ Object



64
65
66
# File 'lib/tipi.rb', line 64

def route(&block)
  proc { |req| req.route(&block) }
end

.run_sites(site_map) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tipi/controller/extensions.rb', line 21

def run_sites(site_map)
  sites = site_map.each_with_object({}) { |(k, v), h| h[k] = v.to_proc }
  valid_hosts = sites.keys

  @app = ->(req) {
    handler = sites[req.host]
    if handler
      handler.call(req)
    else
      req.respond(nil, ':status' => Qeweney::Status::NOT_FOUND)
    end
  }

  @app.define_singleton_method(:valid_hosts) { valid_hosts }
end

.serve(host, port, opts = {}, &handler) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/tipi.rb', line 22

def serve(host, port, opts = {}, &handler)
  opts[:alpn_protocols] = ALPN_PROTOCOLS
  server = Polyphony::Net.tcp_listen(host, port, opts)
  accept_loop(server, opts, &handler)
ensure
  server&.close
end

.str_to_native_type(str) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/tipi/cli.rb', line 55

def self.str_to_native_type(str)
  case str
  when /^\d+$/
    str.to_i
  else
    str
  end
end

.verify_path(path) ⇒ Object



64
65
66
67
68
69
# File 'lib/tipi/cli.rb', line 64

def self.verify_path(path)
  return if File.file?(path) || File.directory?(path)

  puts "Invalid path specified #{path}"
  exit!
end