Class: Specwrk::Web::App

Inherits:
Object
  • Object
show all
Defined in:
lib/specwrk/web/app.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.rackupObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/specwrk/web/app.rb', line 68

def rackup
  Rack::Builder.new do
    if ENV["SPECWRK_SRV_VERBOSE"]
      use Rack::Runtime
      use Specwrk::Web::Logger, $stdout, %w[/health]
    end

    use Specwrk::Web::Auth, %w[/health] # global auth check
    run Specwrk::Web::App.new           # your router
  end
end

.run!Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/specwrk/web/app.rb', line 31

def run!
  Process.setproctitle "specwrk-server"

  setup!

  server_opts = {
    Port: ENV.fetch("SPECWRK_SRV_PORT", "5138").to_i,
    BindAddress: ENV.fetch("SPECWRK_SRV_BIND", "127.0.0.1"),
    Logger: WEBrick::Log.new($stdout, WEBrick::Log::FATAL),
    AccessLog: [],
    KeepAliveTimeout: 300
  }

  # rack v3 or v2
  handler_klass = defined?(Rackup::Handler) ? Rackup::Handler::WEBrick : Rack::Handler.get("webrick")

  handler_klass.run(rackup, **server_opts) do |server|
    ["INT", "TERM"].each do |sig|
      trap(sig) do
        puts "\n→ Shutting down gracefully..." unless ENV["SPECWRK_FORKED"]
        server.shutdown
      end
    end
  end
end

.setup!Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/specwrk/web/app.rb', line 57

def setup!
  if ENV["SPECWRK_OUT"]
    FileUtils.mkdir_p(ENV["SPECWRK_OUT"])
    ENV["SPECWRK_SRV_LOG"] ||= Pathname.new(File.join(ENV["SPECWRK_OUT"], "server.log")).to_s unless ENV["SPECWRK_SRV_VERBOSE"]
  end

  if ENV["SPECWRK_SRV_LOG"]
    $stdout.reopen(ENV["SPECWRK_SRV_LOG"], "w")
  end
end

Instance Method Details

#call(env) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/specwrk/web/app.rb', line 81

def call(env)
  env[:request] ||= Rack::Request.new(env)

  route(method: env[:request].request_method, path: env[:request].path_info)
    .new(env[:request])
    .response
end

#route(method:, path:) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/specwrk/web/app.rb', line 89

def route(method:, path:)
  case [method, path]
  when ["GET", "/health"], ["HEAD", "/health"]
    Endpoints::Health
  when ["GET", "/heartbeat"]
    Endpoints::Heartbeat
  when ["POST", "/pop"]
    Endpoints::Pop
  when ["POST", "/complete_and_pop"]
    Endpoints::CompleteAndPop
  when ["POST", "/seed"]
    Endpoints::Seed
  when ["GET", "/report"]
    Endpoints::Report
  when ["DELETE", "/shutdown"]
    Endpoints::Shutdown
  else
    Endpoints::NotFound
  end
end