Crate wasmcloud_actor_http_server[][src]

HTTP Server wasmCloud Actor Interface

This crate provides wasmCloud actors with an interface to the HTTP Server capability provider. Actors using this interface must have the claim wasmcloud:httpserver in order to have permission to handle requests, and they must have an active, configured binding to an HTTP Server capability provider.

The HTTP Server provider is one-way, and only delivers messages to actors. Actors cannot make host calls to this provider. To make outbound http requests, actors will need to use a wasmcloud:httpclient provider.

Example:

use wasmcloud_actor_http_server as http;
use wasmcloud_actor_core as actor;
use wapc_guest::HandlerResult;
use http::{Request, Response, Handlers, Method};

#[actor::init]
fn init() {
    http::Handlers::register_handle_request(req_handler);
}

fn req_handler(req: http::Request) -> HandlerResult<http::Response> {
    let method = req.method();
    let segments = req.path_segments();

    match (method, &*segments)  {
        (Method::Get, &["v0", "users", id]) => get_user(id),
        (Method::Put, &["v1", "users", id]) => update_user(id, &req.body),
        _ => Ok(http::Response::not_found())
    }
}

fn get_user(id: &str) -> HandlerResult<http::Response> {
    Ok(http::Response::ok())
}
fn update_user(id: &str, body: &[u8]) -> HandlerResult<http::Response> {
    Ok(http::Response::ok())
}

Re-exports

pub use generated::Handlers;
pub use generated::deserialize;
pub use generated::serialize;
pub use generated::Request;
pub use generated::Response;

Modules

generated

Enums

Method

Valid values for an HTTP method