Class: Portless::Runner

Inherits:
Object
  • Object
show all
Includes:
RunSupport
Defined in:
lib/portless/runner.rb

Overview

rb-portless run <cmd>: pick a free backend port, inject PORT/HOST, ensure the proxy is up, register the named route, run the child in its own process group (forwarding signals), and deregister on exit. Rails/puma respect PORT natively. Mirrors portless's runApp/spawnCommand.

Instance Method Summary collapse

Constructor Details

#initialize(command:, config: Config.load, route_store: RouteStore.new, options: {}) ⇒ Runner

Returns a new instance of Runner.



11
12
13
14
15
16
# File 'lib/portless/runner.rb', line 11

def initialize(command:, config: Config.load, route_store: RouteStore.new, options: {})
  @command = Array(command)
  @config = config
  @route_store = route_store
  @options = options # :lan, :ip, :ngrok, :tailscale, :funnel, :name, :force, :app_port
end

Instance Method Details

#runObject



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/portless/runner.rb', line 18

def run
  command = resolved_command
  raise Error, "nothing to run — pass a command, e.g. rb-portless run bin/dev" if command.empty?

  # PORTLESS=0|false|skip → run the command straight through, no proxy/route.
  return exec(*command) if Portless.skip_proxy?

  port = @options[:app_port] || @config.app_port&.to_i || FreePort.find
  command = Frameworks.inject(command, port) # --port/--host for vite/astro/etc.
  hostname = @config.hostname(@options[:name])

  warn "rb-portless: #{@config.tld_warning}" if @config.tld_warning
  ensure_trusted
  proxy_port = Daemon.ensure_running(tls: @config.tls, lan: !!@options[:lan])
  # lan: marks the route as opted into LAN serving — the proxy serves only
  # these to off-loopback clients, so --lan shares THIS app, not every app.
  @route_store.add(hostname: hostname, port: port, pid: Process.pid,
                   force: @options[:force], lan: !!@options[:lan])

  url = display_url(hostname, proxy_port)
  rows = [ [ "Local", url, :cyan ] ]
  rows.concat(lan_rows(port, proxy_port))
  rows.concat(share_rows(hostname, port))
  record_share_urls(hostname, port) # so `rb-portless list` shows the public URLs
  Banner.app(rows: rows, backend_port: port, proxy_version: Health.proxy_version(proxy_port))

  status = supervise(command, port, url)
  exit(status)
ensure
  teardown
  @route_store.remove(hostname, owner_pid: Process.pid) if hostname
end