-
-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Webrick with custom simple http server (#1030)
This commit removes Webrick and replaces it with a (simple) custom HttpServer. This is a single-process/singe-thread server that behaves simmilary to webrick.
- Loading branch information
Showing
9 changed files
with
85 additions
and
2,616 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# frozen_string_literal: true | ||
|
||
module GoodJob | ||
class HttpServer | ||
SOCKET_READ_TIMEOUT = 5 # in seconds | ||
|
||
def initialize(app, options = {}) | ||
@app = app | ||
@port = options[:port] | ||
@logger = options[:logger] | ||
|
||
@running = Concurrent::AtomicBoolean.new(false) | ||
end | ||
|
||
def run | ||
@running.make_true | ||
start_server | ||
handle_connections if @running.true? | ||
rescue StandardError => e | ||
@logger.error "Server encountered an error: #{e}" | ||
ensure | ||
stop | ||
end | ||
|
||
def stop | ||
@running.make_false | ||
@server&.close | ||
end | ||
|
||
def running? | ||
@running.true? | ||
end | ||
|
||
private | ||
|
||
def start_server | ||
@server = TCPServer.new('0.0.0.0', @port) | ||
rescue StandardError => e | ||
@logger.error "Failed to start server: #{e}" | ||
@running.make_false | ||
end | ||
|
||
def handle_connections | ||
while @running.true? | ||
begin | ||
ready_sockets, = IO.select([@server], nil, nil, SOCKET_READ_TIMEOUT) | ||
return unless ready_sockets | ||
|
||
client = @server.accept_nonblock(exception: false) | ||
request = client.gets | ||
|
||
status, headers, body = @app.call(parse_request(request)) | ||
respond(client, status, headers, body) | ||
|
||
client.close | ||
rescue IO::WaitReadable, Errno::EINTR | ||
retry | ||
end | ||
end | ||
end | ||
|
||
def parse_request(request) | ||
method, full_path = request.split | ||
path, query = full_path.split('?') | ||
{ 'REQUEST_METHOD' => method, 'PATH_INFO' => path, 'QUERY_STRING' => query || '' } | ||
end | ||
|
||
def respond(client, status, headers, body) | ||
client.write "HTTP/1.1 #{status}\r\n" | ||
headers.each { |key, value| client.write "#{key}: #{value}\r\n" } | ||
client.write "\r\n" | ||
body.each { |part| client.write part.to_s } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.