Created
January 19, 2023 16:47
-
-
Save havenwood/309f35bedef35d0f835f73fbf5963152 to your computer and use it in GitHub Desktop.
Curious about line 66 and line 70 differences between Falcon and Puma handling of the stream.
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
# frozen_string_literal: true | |
require 'trenni/template' | |
## | |
# Streaming template | |
TEMPLATE = Trenni::Template.load(<<~'HTML') | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title><?r Model::TITLE ?></title> | |
<style> | |
table { | |
border-collapse: collapse; | |
box-shadow: 0 0 20px Gainsboro; | |
font-family: system-ui; | |
} | |
table tr:nth-of-type(even) { | |
background-color: AliceBlue; | |
} | |
table td { | |
padding: 1rem; | |
} | |
</style> | |
</head> | |
<body> | |
<table> | |
<?r Model.rows.each do |row| ?> | |
<tr> | |
<?r row.each do |column| ?> | |
<?r sleep 1 ?> | |
<td>#{column}</td> | |
<?r end ?> | |
</tr> | |
<?r end ?> | |
</table> | |
</body> | |
</html> | |
HTML | |
## | |
# Data for the template | |
module Model | |
TITLE = 'Table of Numbers' | |
def self.rows = Array.new(3) { Array.new(4) { rand 10..99 } } | |
end | |
## | |
# 404 page | |
NOT_FOUND_HTML = <<~'HTML' | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head><title>Not Found</title></head> | |
<body><h1>Not Found</h1></body> | |
</html> | |
HTML | |
## | |
# Rack app | |
HEADERS = {'content-type' => 'text/html'}.freeze | |
BODY_NOT_FOUND = [NOT_FOUND_HTML].freeze | |
## | |
# With Falcon, this closes the stream. With Puma, it doesn't. | |
BODY = TEMPLATE.to_proc | |
## | |
# With Puma, this closes the stream. With Falcon, `stream` is an Integer. | |
BODY = TEMPLATE.to_proc >> ->(stream) { stream.close } | |
run do |env| | |
case env.transform_keys { _1.to_sym.downcase } | |
in {path_info: '/', request_method: 'GET' | 'HEAD'} | |
[200, HEADERS.dup, BODY] | |
else | |
[404, HEADERS.dup, BODY_NOT_FOUND] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment