http api
How it works:
Traditionally in lua we write this kind of handler:
local function api(req)
local body = process_response(req.method, req.query, req.proto, req.headers, req:read())
return {
status = 200,
body = json.encode(body)
}
end
_G.pico.httpd:route({ path = '/api', method = 'GET' }, api_hadler)
With hyper we can write handlers in a well-documented hyper style:
pub async fn api(req: Request<Body>) -> Result<Response<Body>, ServerError> {
let body: Body = process_response(&req).await?;
Ok(Response::builder()
.status(201)
.body(body)?)
}
server
.route(
http::RouterOpts {
path: "/api",
method: "GET",
},
api,
)
.expect("route /api added");
oha http://127.0.0.1:8080/api -z 10m -c 100
:
Benchmarks - Picodata with hyper::Request and hyper::Response. Debug build.
Kind | Value |
---|---|
Slowest | 0.2619 secs |
Fastest | 0.0000 secs |
Average | 0.0052 secs |
Requests/sec | 19206.4781 |
- Picodata with standard lua handler. Release build.
Kind | Value |
---|---|
Slowest | 0.3174 secs |
Fastest | 0.0004 secs |
Average | 0.0048 secs |
Requests/sec | 21006.4999 |
Edited by Dmitry Travyan