require "http/server" active_requests = 0 server = HTTP::Server.new do |context| active_requests += 1 sl = rand(50) puts "sleeping: #{sl}" sleep sl.seconds context.response.content_type = "text/plain" context.response.print "Hello world! The time is #{Time.now}\n" active_requests -= 1 end Signal::INT.trap do puts "Will stop in moment! (#{active_requests} active requests)" if !server.closed? server.close end end server.bind_tcp "0.0.0.0", 8080 puts "Listening on http://0.0.0.0:8080" server.listen until active_requests == 0 Fiber.yield end