-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhello-world.ru
More file actions
37 lines (31 loc) · 820 Bytes
/
hello-world.ru
File metadata and controls
37 lines (31 loc) · 820 Bytes
1
2
3
4
5
6
7
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
require 'bundler/setup'
require 'datastar'
# This is a test Rack endpoint
# with a hello world example using Datastar.
# To run:
#
# # install dependencies
# bundle install
# # run this endpoint with Puma server
# bundle exec puma ./hello-world.ru
#
# Then open http://localhost:9292
#
HTML = File.read(File.expand_path('hello-world.html', __dir__))
run do |env|
datastar = Datastar.from_rack_env(env)
if datastar.sse?
delay = (datastar.signals['delay'] || 0).to_i
delay /= 1000.0 if delay.positive?
message = 'Hello, world!'
datastar.stream do |sse|
message.size.times do |i|
sse.patch_elements(%(<div id="message">#{message[0..i]}</div>))
sleep delay
end
end
else
[200, { 'content-type' => 'text/html' }, [HTML]]
end
end
trap('INT') { exit }