Class: Pytty::Daemon::Cli::ServeCommand

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/pytty/daemon/cli/serve_command.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



8
9
10
11
12
13
14
15
16
17
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
# File 'lib/pytty/daemon/cli/serve_command.rb', line 8

def execute
  puts "🚽 pyttyd #{Pytty::VERSION}"

  url_parts = ["http://"]
  url_parts << if ENV["PYTTY_BIND"]
    ENV["PYTTY_BIND"]
  else
    "127.0.0.1"
  end
  url_parts << ":"
  url_parts << if ENV["PYTTY_PORT"]
    if ENV["PYTTY_PORT"] == "PORT"
      ENV.fetch "PORT"
    else
      ENV["PYTTY_PORT"]
    end
  else
    "1234"
  end

  Async::Reactor.run do |task|
    Pytty::Daemon.load
    server_task = task.async do
      Pytty::Daemon::Api::Server.run url: url_parts.join("")
    end

    shutdown = lambda do |signo|
      puts "\r"
      puts "Got: #{Signal.signame(signo)}"
      server_task.stop
      Pytty::Daemon.yields.each do |id,process_yield|
        process_yield.signal "kill"
        puts id
      end
      puts "bye."
    end

    Signal.trap "INT", shutdown
    Signal.trap "TERM", shutdown
  end
end