// Warning: This method is somewhat complicated, since it needs to deal with async stuff
// There should be no state here beyond the handler, so no need for an actual hyper service
// (We don't want to lock into hyper that hard anyway)
This method is somewhat complicated, since it needs to deal with async stuff
There should be no state here beyond the handler, so no need for an actual hyper service
(We don't want to lock into hyper that hard anyway)
This method is somewhat complicated since it needs to deal with async stuff There should be no state here beyond the handler so no need for an actual hyper service ( We don ' t want to lock into hyper that hard anyway )
pub async fn global_request_entrypoint(
    handler: Arc<HttpRequestHandler>,
    req: Request<Body>,
) -> Result<Response<Body>, WorkerError> {
    debug!("{:?}", req);
    // Pull the verb, uri, and query stuff out of the request
    // (It's okay to do this, since it's all quite quick to execute)
    let http_verb = req.method().clone();
    let uri = req.uri().clone();
    let query = uri.query().unwrap_or("").to_string();
    // Get a stream of Bytes representing the body of the request
    let mut body_stream = req.into_body();
    // Turn that stream into a concrete String
    let mut body = String::new();
    while let Some(chunk) = body_stream.next().await {
        body.push_str(str::from_utf8(&chunk?)?);
    }
    debug!("body = {:?}", body);
    // We want to do the actual handling in a "spawn_blocking" closure, since many operations there can block
    // This allows us to handle a ton of requests at once, since we're not blocking the executor
    let resp = spawn_blocking(move || {
        // Delegate to the handler to actually deal with this request
        // NOTE: We cannot handle panics here, since it could leave the handler in an inconsistent state
        // Better to just bomb out
        // TODO: Investigate handling panics at a lower level
        handler.handle(http_verb, &uri, query, body)
    })
    .await?
    .unwrap_or_else(|e| {
        warn!("Forced to convert error {:?} into a http response", e);
        e.into()
    });
    if resp.status() == StatusCode::INTERNAL_SERVER_ERROR {
        error!("INTERNAL SERVER ERROR -- {:?}", resp);
    } else {
        debug!("{:?}", resp);
    }
    Ok(resp)
}
pub async fn global_request_entrypoint ( handler : Arc < HttpRequestHandler > , req : Request < Body > , ) -> Result < Response < Body > , WorkerError > { debug ! ( " " req ) ; // Pull the verb, uri, and query stuff out of the request // (It's okay to do this, since it's all quite quick to execute) let http_verb = req . method ( ) . clone ( ) ; let uri = req . uri ( ) . clone ( ) ; let query = uri . query ( ) . unwrap_or ( " " ) . to_string ( ) ; // Get a stream of Bytes representing the body of the request let mut body_stream = req . into_body ( ) ; // Turn that stream into a concrete String let mut body = String :: new ( ) ; while let Some ( chunk ) = body_stream . next ( ) . await { body . push_str ( str :: from_utf8 ( & chunk ? ) ? ) ; } debug ! ( " " body ) ; // We want to do the actual handling in a "spawn_blocking" closure, since many operations there can block // This allows us to handle a ton of requests at once, since we're not blocking the executor let resp = spawn_blocking ( move | | { // Delegate to the handler to actually deal with this request // NOTE: We cannot handle panics here, since it could leave the handler in an inconsistent state // Better to just bomb out // TODO: Investigate handling panics at a lower level handler . handle ( http_verb , & uri , query , body ) } ) . await ? . unwrap_or_else ( | e | { warn ! ( " " e ) ; e . into ( ) } ) ; if resp . status ( ) == StatusCode :: INTERNAL_SERVER_ERROR { error ! ( " " resp ) ; } else { debug ! ( " " resp ) ; } Ok ( resp ) }
Warning: This method is somewhat complicated, since it needs to deal with async stuff
There should be no state here beyond the handler, so no need for an actual hyper service
(We don't want to lock into hyper that hard anyway)
Warning : This method is somewhat complicated since it needs to deal with async stuff There should be no state here beyond the handler so no need for an actual hyper service ( We don ' t want to lock into hyper that hard anyway )
handler
Arc<HttpRequestHandler>
req
Request<Body>