1. Basic Web Server

Based on the following docs:

code/crystal/src/web_server/web_server.cr
require "http/server"

server = HTTP::Server.new do |context|
  # get incomming info:
  path   = context.request.path
  query  = context.request.query

  # log request info (to stdout - screen)
  puts "HTTP request for path: #{path}"
  puts "HTTP request for query: #{query}"

  # build text response
  text_response = "Hello world, got '#{path}'"
  text_response += " and '#{query}'" unless query == ""

  # send response to the web client
  context.response.content_type = "text/plain"
  context.response.print text_response
end

puts "Listening on http://127.0.0.1:8080"
server.listen(8080)

Run with

$ crystal code/crystal/src/web_server/web_server.cr

Now open your browser and point it to:

Notice in the last request the server 'logs' the query as:

dog=Nyima&cat=Shin%C3%A9

This has to do with how web clients escape all non-ascii data into ascii characters. We will explore a fix for processing in the our next version.