Use cases · Web

Web servers and APIs in Tern

Write handlers as plain functions, get an HTTP server on every core, and deploy one small binary. No async/await, no runtime to install.

curl -fsSL https://ternlang.dev/install.sh | sh
tern new myapp && cd myapp

A JSON API in 16 lines

main.tn
import http
import json

struct Link:
    code: str
    url: str

fn handle(req: http.Request) http.Response:
    if req.path == "/health":
        return http.text("ok")
    if req.path == "/link":
        return http.json(json.encode(Link("tern", "https://ternlang.dev")))
    return http.not_found()

fn main():
    http.serve(8080, handle)
tern run main.tn                 # serving http://localhost:8080
curl localhost:8080/link         # {"code":"tern","url":"https://ternlang.dev"}

The handler is an ordinary function from Request to Response, so you test it by calling it. Built on macOS, this server is a 73 KB binary. The web apps guide adds routing, state and a database.

Benchmarks

One server threadTernRust axumGo net/http
HTTP plaintext requests/s169k149k115k
With middleware requests/s163k86k97k
SSE fan-out events/s13.6M3.5M0.51M
Peak memory under load39 MB44 MB102 MB
Web server binary254 KB1.3 MB8.1 MB
Rebuild after a one-line edit optimized0.67 s26.4 s2.54 s

Tern’s numbers are for roost, the web framework, on the standard library’s http engine.

What comes with it

Questions

Is there a web framework for Tern?

Yes: roost, an axum-style framework with routing, middleware, WebSockets and SSE. The standard library's http module also serves requests on its own.

Does Tern use async/await?

No. Handlers are plain functions on green-thread tasks, so blocking-looking code doesn't block the server.

Which databases does Tern support?

PostgreSQL and SQLite, with migrations and generated, typed query functions, plus a Redis client.

More about Tern

Tern (ternlang) is an early release: the language will change, and so will these numbers. Benchmarks run on an Apple M4 Pro; the full tables, losses included, are on the home page.