From 1adfe3f1d06b6957c0d4d2b47eb7a1decdf286c5 Mon Sep 17 00:00:00 2001 From: Camilo Schoeningh Date: Sun, 27 Feb 2022 18:33:27 +0100 Subject: [PATCH 1/3] [HTTP] Send back response_code and header To use the web server in applications like k8s, we should return the response code. This can be used inside the readiness and livenessprobe. --- http_server.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/http_server.rb b/http_server.rb index 7a11960..203c793 100644 --- a/http_server.rb +++ b/http_server.rb @@ -10,6 +10,9 @@ puts "#{method} #{path} #{version}" + headers = "HTTP/1.1 200 OK\r\nDate: #{Time.now}\r\nContent-Type: text/html; charset=iso-8859-1\r\n\r\n" + client.puts headers + if path == "/healthcheck" client.write("OK") else From 11e08fe76945284e288a464533fb0fffa19bf4e9 Mon Sep 17 00:00:00 2001 From: Camilo Schoeningh Date: Sun, 27 Feb 2022 19:05:32 +0100 Subject: [PATCH 2/3] [ADHOC] Use puts instead of write, to append new line --- http_server.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/http_server.rb b/http_server.rb index 203c793..c0293a5 100644 --- a/http_server.rb +++ b/http_server.rb @@ -7,17 +7,17 @@ request = client.readpartial(2048) method, path, version = request.lines[0].split - puts "#{method} #{path} #{version}" - headers = "HTTP/1.1 200 OK\r\nDate: #{Time.now}\r\nContent-Type: text/html; charset=iso-8859-1\r\n\r\n" - client.puts headers - - if path == "/healthcheck" - client.write("OK") + if path == "/healthcheck" + response = "OK" else - client.write("Well, hello there!") + response = "Well, hello there!" end + headers = "HTTP/1.1 200 OK\r\nDate: #{Time.now}\r\nContent-Length: #{response.bytesize}\r\nContent-Type: text/html; charset=iso-8859-1\r\n\r\n" + client.puts headers + client.puts response + client.close } From 6c84c3b20ceb4a8126063beae0e3f3527aaebcd0 Mon Sep 17 00:00:00 2001 From: Camilo Schoeningh Date: Sun, 27 Feb 2022 20:42:03 +0100 Subject: [PATCH 3/3] [STDOUT] Make sure all output is flushed to stdout immediately Ruby is buffering output until the output buffer is full. To change the behavior we set sync=true https://ruby-doc.org/core-2.3.1/IO.html#method-i-sync --- http_server.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/http_server.rb b/http_server.rb index c0293a5..d975658 100644 --- a/http_server.rb +++ b/http_server.rb @@ -1,13 +1,14 @@ require 'socket' server = TCPServer.new('0.0.0.0', 80) +$stdout.sync = true loop { client = server.accept request = client.readpartial(2048) method, path, version = request.lines[0].split - puts "#{method} #{path} #{version}" + STDOUT.puts "#{method} #{path} #{version}" if path == "/healthcheck" response = "OK"