Examples

Complete Harbor programs demonstrating the language's features.

Hello World

The simplest Harbor program.

print("Hello, world!")

Variables and functions

Defining variables, a function, and printing the result.

x = 10
y = 20

fn add(a, b) {
    a + b
}

print(add(x, y))

Output: 30

String concatenation

Building strings from pieces.

first = "Harbor"
second = " is cool"
print(first + second)

Output: Harbor is cool

Basic HTTP server

A server with a single route.

server 8080 {
    get "/hello" {
        respond "Hello, world!"
    }
}
$ curl http://127.0.0.1:8080/hello
Hello, world!

Multi-route server

A server with several endpoints.

server 3000 {
    get "/" {
        respond "Welcome to Harbor"
    }

    get "/health" {
        respond "ok"
    }

    get "/version" {
        respond "0.2"
    }
}

Using the request object

Accessing request data inside route handlers.

server 8080 {
    get "/echo" {
        respond "You requested: " + req.path
    }

    get "/method" {
        respond "Method: " + req.method
    }
}

Functions with a server

Defining helper functions to use in route handlers.

fn greet(name) {
    "Hello, " + name + "!"
}

server 8080 {
    get "/alice" {
        respond greet("Alice")
    }

    get "/bob" {
        respond greet("Bob")
    }
}
$ curl http://127.0.0.1:8080/alice
Hello, Alice!

$ curl http://127.0.0.1:8080/bob
Hello, Bob!

Setup before server start

Running initialization code, then starting the server.

app_name = "My App"
version = "0.1"

fn banner() {
    app_name + " v" + version
}

print("Starting " + banner())

server 4000 {
    get "/" {
        respond banner()
    }
}

Console output:

Starting My App v0.1
Harbor server running on http://127.0.0.1:4000