Skip to content

Instantly share code, notes, and snippets.

@jhass jhass/server.cr
Created Jul 22, 2016

Embed
What would you like to do?
Simple debug HTTP server in Crystal
require "option_parser"
require "http/server"
require "json"
class Settings
property port = 3000
property host = "127.0.0.1"
property? show_headers = false
property? show_raw_json = false
end
settings = Settings.new
OptionParser.parse! do |p|
p.banner = "crystal server.cr -- [-p 3000] [-h 127.0.0.1] [--headers] [--rawjson]"
p.on("-p PORT", "--port=PORT", "The port to bind on (default: 3000).") do |port|
settings.port = port.to_i? || abort("Invalid port #{port}")
end
p.on("-h HOST", "--host=HOST", "The hostname to bind on (default: 127.0.0.1).") do |host|
settings.host = host
end
p.on("--headers", "If set, request headers will be shown.") do
settings.show_headers = true
end
p.on("--rawjson", "If set, JSON payload will not be formatted.") do
settings.show_raw_json = true
end
p.on("--help", "Displays this message.") do
puts p
exit
end
end rescue abort "Invalid arguments, see --help."
server = HTTP::Server.new(settings.host, settings.port) do |context|
puts "-" * 80
puts
puts Time.now
puts "#{context.request.method} #{context.request.path}"
if settings.show_headers?
puts
puts "Headers:"
puts context.request.headers.map {|k, v| " #{k}: #{v.join("; ")}" }.join("\n")
end
if body = context.request.body
puts
content_type = context.request.headers["Content-Type"]
if content_type.includes?("json") && !settings.show_raw_json?
puts JSON.parse(body).to_pretty_json
elsif content_type.includes?("text") || content_type.includes?("json")
puts body
else
puts "Got body of #{content_type} with #{context.request.headers["Content-Length"]} bytes."
end
end
puts
context.response.status_code = 204
end
puts "Listening on #{settings.host}:#{settings.port}"
server.listen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.