Saltar al contenido

Servidor — Enrutador y Decoradores

En esta página

El servidor HTTP de Zolo se ejecuta en el proceso host (hyper + tokio) y no termina: al llamar a http.serve(puerto, app) el script se bloquea y comienza a atender solicitudes en bucle. Para probarlo, abre otra terminal y usa curl.

El primer enfoque usa composición por pipes: http.router() crea un enrutador vacío; cada llamada http.get("/ruta", handler) añade una ruta y devuelve el mismo enrutador, listo para el siguiente |>:

http.router() |> http.get(...) |> http.serve(puerto, app).

05-server-router.zolo
Playground
// Feature: HTTP server — router via pipes

// Syntax: `http.router() |> http.get("/path", handler) |> ...`

// When to use: simple APIs; declarative composition of routes with pipes.


use std::http

// Each handler receives `req` and returns a string, map, or `http.response(...)`.

fn handle_root(_req) {
  return "Hello from the Zolo server!"
}

fn handle_health(_req) {
  return #{status: "ok"}
}

let app = http.router()
  |> http.get("/", handle_root)
  |> http.get("/health", handle_health)

// Runs in the foreground on port 3000.

http.serve(3000, app)
// expected when running:

//   curl http://localhost:3000/        -> Hello from the Zolo server!

//   curl http://localhost:3000/health  -> {"status":"ok"}

Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.

El segundo enfoque es el estilo decorador: anotar una fn con @get, @post, @put o @delete registra la ruta automáticamente; http.serve(puerto) sin argumento de app usa el registro global:

@get/@post/@put/@delete — registro automático de rutas.

06-server-decorators.zolo
Playground
// Feature: HTTP server — `@get`/`@post`/`@put`/`@delete` decorators

// Syntax: annotate a `fn` with the route; `http.serve(port)` registers them all.

// When to use: "framework" style, APIs with many routes, ergonomics

// close to Flask/Express, without wiring up a router manually.


use std::http

@get("/")
fn index() {
  return "Hello from decorator routes!"
}

@get("/health")
fn health() {
  return #{status: "ok"}
}

@post("/echo")
fn echo(req) {
  // `req.json()` reads the body as JSON and returns it as a map.

  return req.json()
}

@put("/items/:id")
fn update_item(req) {
  return #{updated: req.params.id}
}

@delete("/items/:id")
fn delete_item(req) {
  return #{deleted: req.params.id}
}

http.serve(3001)
// expected when running:

//   curl http://localhost:3001/                       -> Hello from decorator routes!

//   curl -X POST -d '{"x":1}' http://localhost:3001/echo

//   curl -X PUT  http://localhost:3001/items/42       -> {"updated":"42"}

//   curl -X DELETE http://localhost:3001/items/42     -> {"deleted":"42"}

Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.

Cada handler recibe req y puede retornar una cadena (200 text/plain), un mapa (200 application/json) o un valor construido con los helpers de respuesta vistos más adelante.

Buscar en Zolo

9 resultados

enespt-br