Files
lux/examples/http_router.lux
2026-02-17 06:52:44 -05:00

66 lines
2.6 KiB
Plaintext

fn indexHandler(): { status: Int, body: String } = httpOk("Welcome to Lux HTTP Framework!")
fn listUsersHandler(): { status: Int, body: String } = {
let user1 = jsonObject(jsonJoin([jsonNumber("id", 1), jsonString("name", "Alice")]))
let user2 = jsonObject(jsonJoin([jsonNumber("id", 2), jsonString("name", "Bob")]))
let users = jsonArray(jsonJoin([user1, user2]))
httpOk(users)
}
fn getUserHandler(path: String): { status: Int, body: String } = {
match getPathSegment(path, 1) {
Some(id) => {
let body = jsonObject(jsonJoin([jsonString("id", id), jsonString("name", "User " + id)]))
httpOk(body)
},
None => httpNotFound(jsonErrorMsg("User ID required")),
}
}
fn healthHandler(): { status: Int, body: String } = httpOk(jsonObject(jsonString("status", "healthy")))
fn router(method: String, path: String, body: String): { status: Int, body: String } = {
if method == "GET" && path == "/" then indexHandler() else if method == "GET" && path == "/health" then healthHandler() else if method == "GET" && path == "/users" then listUsersHandler() else if method == "GET" && pathMatches(path, "/users/:id") then getUserHandler(path) else httpNotFound(jsonErrorMsg("Not found: " + path))
}
fn serveLoop(remaining: Int): Unit with {Console, HttpServer} = {
if remaining <= 0 then {
Console.print("Max requests reached, stopping server.")
HttpServer.stop()
} else {
let req = HttpServer.accept()
Console.print(req.method + " " + req.path)
let resp = router(req.method, req.path, req.body)
HttpServer.respond(resp.status, resp.body)
serveLoop(remaining - 1)
}
}
fn main(): Unit with {Console, HttpServer} = {
let port = 8080
let maxRequests = 10
Console.print("========================================")
Console.print(" Lux HTTP Router Demo")
Console.print("========================================")
Console.print("")
Console.print("Endpoints:")
Console.print(" GET / - Welcome message")
Console.print(" GET /health - Health check")
Console.print(" GET /users - List all users")
Console.print(" GET /users/:id - Get user by ID")
Console.print("")
Console.print("Try:")
Console.print(" curl http://localhost:8080/")
Console.print(" curl http://localhost:8080/users")
Console.print(" curl http://localhost:8080/users/42")
Console.print("")
Console.print("Starting server on port " + toString(port) + "...")
Console.print("Will handle " + toString(maxRequests) + " requests then stop.")
Console.print("")
HttpServer.listen(port)
Console.print("Server listening!")
serveLoop(maxRequests)
}
let output = run main() with {}