Halo REST Usage (English)
Chinese version: README.zh.md
Quick Start
- Run example:
cargo run -p halo-rest --example hello --release - Structure: build a
Serverwith config → set prefixes → attach middlewares → register routes → start.
let mut conf = default;
// Optional: tweak built-ins
// conf.middlewares.gzip = false;
// conf.max_bytes = 8 * 1024 * 1024;
let mut server = new
.expect
.with_root_prefix
.with_prefix;
// attach global middleware(s)
server = server.with_middleware;
// register routes
server
.add_routes
.expect;
let handle = server.start.await.expect;
// block until Ctrl+C, then stop gracefully
handle.stop.await.expect;
Handlers & Middleware
- Use attribute macros for zero-boilerplate definitions:
- Handler:
#[rest::handler] async fn foo(ctx: AppCtx, req: Request<Body>) -> Response<Body> { ... } - Middleware:
#[rest::middleware] async fn bar(req: Request<Body>, next: HandlerFunc) -> Response<Body> { ... }
- Handler:
- The helper
with_middleware/with_middlewareschain global middleware; route-scoped middleware usewith_handlers(mws, routes)when building route lists.
Built-in Middlewares (default state)
max_bytes: ON, limitconf.max_bytes(default 16 MiB). Disable viaconf.middlewares.max_bytes = false.gzip(decode/encode): ON. Disable viaconf.middlewares.gzip = false.rate_limit: OFF by default. Enable withconf.rate_limit = Some(RateLimitConf { permits_per_second, burst }).concurrency_limit: OFF by default. Enable withconf.concurrency_limit = Some(limit).timeout: ON by default (conf.timeout = Some(3000)ms). Disable withconf.timeout = None.
Built-ins run in order: max_bytes → rate_limit → concurrency_limit → timeout → gzip → user middlewares.
Configuration (TOML example)
[]
= true
= true
= 16777216 # 16 MiB
= 3000 # ms, None to disable
[]
= 50000
= 100000
= 10000
Running Benchmarks
- Start three services (halo-rest, axum, gin) as described in
examples/bench.sh. - Run
./bench.shto execute wrk benchmarks; results are written toexamples/bench_out/*.txt.