Server Blocks

The server keyword starts an HTTP server. It's a top-level statement that binds to a port and defines route handlers.

Syntax

server 8080 {
    // route handlers go here
}

The server keyword is followed by a port number and a block containing route definitions.

Behavior

  • The server binds to 127.0.0.1:<port>
  • On startup, it prints Harbor server running on http://127.0.0.1:<port>
  • The server uses Hyper and Tokio under the hood for async HTTP
  • Unmatched routes return a 404 Not Found response

Important: When the interpreter reaches a server statement, it starts the server and blocks. No statements after the server block will execute.

Running code before the server

Place any setup logic — variable assignments, function definitions, print statements — before the server block:

version = "1.0"
print("Starting server...")

fn make_greeting(name) {
    "Hello, " + name
}

server 3000 {
    get "/" {
        respond make_greeting("visitor")
    }
}

Port

The port must be a number literal. It cannot be a variable or expression.

// ✓ This works
server 8080 { ... }

// ✗ This does not work
port = 8080
server port { ... }