Skip to content

http api

Dmitry Travyan requested to merge http-api into master

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");

Benchmarks oha http://127.0.0.1:8080/api -z 10m -c 100:

  1. 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
  1. 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

Merge request reports