🍗 Wiki

Crystal

Crystal

1. Try and Install

After you install the Crystal, you can run a playground server by running crystal play command. It opens an interactive web server which is listening port 8080.

If you’re using Nix, you can install the Crystal instantly.

nix-shell -p crystal

2. Running script and compile

The source code below is from the official home page.

## server.cr, from the official home page
# A very basic HTTP server
require "http/server"

server = HTTP::Server.new do |context|
  context.response.content_type = "text/plain"
  context.response.print "Hello world, got #{context.request.path}!"
end

address = server.bind_tcp(8080)
puts "Listening on http://#{address}"

# This call blocks until the process is terminated
server.listen

You can run it without compiling it by invoking crystal run command.

$ crystal run server.cr
Listening on http://127.0.0.1:8080

$ curl localhost:8080
Hello world, got /!

To compile the code, you can invoke crystal build command.

$ crystal build server.cr

$

And there should be server binary in the current directory. You can simply run it.

$ ./server
Listening on https://127.0.0.1:8080