Skip to content

Web Framework

Routing, templates, and request handling for Wyn web applications.

Quick Start

wyn
fn handle(method: string, path: string, body: string, fd: int) {
    var h = Web.match(method, path)
    if h == 1 {
        Http.respond(fd, 200, "text/html", Web.render("index.html", "name=World\n"))
    } else {
        Http.respond(fd, 404, "text/html", "<h1>404</h1>")
    }
}

fn main() {
    Web.get("/", 1)
    Web.templates("templates")
    var server = Http.serve(8080)
    while true {
        var req = Http.accept(server)
        spawn handle(req.split_at("|", 0), req.split_at("|", 1),
                     req.split_at("|", 2), req.split_at("|", 3).to_int())
    }
}

Routing

wyn
Web.get("/", 1)              // exact match
Web.post("/api/users", 2)    // POST route
Web.get("/static/*", 3)      // wildcard — matches /static/anything
Web.put("/api/users", 4)
Web.delete("/api/users", 5)

Web.match(method, path) returns the handler ID, or -1 if no route matches.

Templates

Templates use placeholders. Pass variables as newline-separated key=value pairs.

html
<!-- templates/page.html -->
<h1>Hello {{name}}</h1>
<p>{{message}}</p>
wyn
Web.templates("templates")
var html = Web.render("page.html", "name=Alice\nmessage=Welcome\n")

API

MethodDescription
Web.get(pattern, id)Register GET route
Web.post(pattern, id)Register POST route
Web.put(pattern, id)Register PUT route
Web.delete(pattern, id)Register DELETE route
Web.match(method, path) -> intMatch request, returns handler id or -1
Web.templates(dir)Set template directory
Web.render(name, vars) -> stringRender template with variable substitution
Web.route_count() -> intNumber of registered routes

MIT License