Skip to main content

omnia_wasi_http/
host.rs

1//! #WASI HTTP Host
2//!
3//! This module implements a host-side service for `wasi:http`
4
5mod default_impl;
6mod server;
7
8use anyhow::Result;
9pub use default_impl::HttpDefault;
10use omnia::{Host, Server, State};
11use wasmtime::component::Linker;
12pub use wasmtime_wasi_http::p3::{WasiHttpCtxView, WasiHttpView};
13
14/// Host-side service for `wasi:http`.
15#[derive(Debug)]
16pub struct WasiHttp;
17
18impl<T> Host<T> for WasiHttp
19where
20    T: WasiHttpView + 'static,
21{
22    fn add_to_linker(linker: &mut Linker<T>) -> Result<()> {
23        Ok(wasmtime_wasi_http::p3::add_to_linker(linker)?)
24    }
25}
26
27impl<S> Server<S> for WasiHttp
28where
29    S: State,
30    S::StoreCtx: WasiHttpView,
31{
32    async fn run(&self, state: &S) -> Result<()> {
33        server::serve(state).await
34    }
35}
36
37/// Implementation of the `WasiHttpView` trait for the store context.
38#[macro_export]
39macro_rules! omnia_wasi_view {
40    ($store_ctx:ty, $field_name:ident) => {
41        impl omnia_wasi_http::WasiHttpView for $store_ctx {
42            fn http(&mut self) -> omnia_wasi_http::WasiHttpCtxView<'_> {
43                omnia_wasi_http::WasiHttpCtxView {
44                    ctx: &mut self.$field_name,
45                    table: &mut self.table,
46                }
47            }
48        }
49    };
50}