Search code examples
crystal-langkemal

Starting crystal in production mode


I've been running my Crystal webapp by building it, and then running the executable. However, it always listens on port 3000.

How do I build/run Crystal webapps listening on 80 and 443?

I'm using Kemal as well. Here is my sample app.

require "kemal"

get "/" do
  "Hello World!"
end

Kemal.run

Building:

crystal build src/myapp.cr

Running:

./myapp

Solution

  • Simply pass a port to Kemal.run:

    require "kemal"
    
    get "/" do
      "Hello World!"
    end
    
    port = ARGV[0]?.try &.to_i?
    Kemal.run port
    

    Build:

    crystal build src/myapp.cr
    

    Run:

    ./myapp # default port 3000
    ./myapp 80
    ./myapp 443