-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathh.cr
42 lines (33 loc) · 1.12 KB
/
h.cr
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
38
39
40
41
42
require "./src/server"
port = ENV.fetch("PORT", "9292").to_i
host = ENV.fetch("HOST", "::")
tls = ENV.fetch("TLS", "false") == "true"
server = HTTP::Server.new(host, port) do |context|
request, response = context.request, context.response
authority = request.headers[":authority"]? || request.headers["Host"]?
response.headers["Content-Type"] = "text/plain"
response << "Received #{request.method} #{request.path} (#{authority})\n"
response << "Served with #{request.version}\n"
if request.method == "PUT" && request.path == "/upload"
buffer = uninitialized UInt8[8192]
response << "Reading DATA:\n"
size = 0
body = request.body.not_nil!
loop do
read_bytes = body.read(buffer.to_slice)
break if read_bytes == 0
Fiber.yield
end
end
end
if ENV["CI"]?
server.logger = Logger::Dummy.new(File.open("/dev/null"))
end
if tls
tls_context = HTTP::Server.default_tls_context
tls_context.certificate_chain = File.join("ssl", "server.crt")
tls_context.private_key = File.join("ssl", "server.key")
server.tls = tls_context
end
puts "Listening on #{host}:#{port} tls=#{tls}"
server.listen