use embassy_executor::Spawner; use embassy_net::Stack; use picoserve::routing::get; use picoserve::{AppBuilder, AppRouter, Config}; #[derive(Default)] pub struct HttpApiProps; impl AppBuilder for HttpApiProps { type PathRouter = impl picoserve::routing::PathRouter; fn build_app(self) -> picoserve::Router { picoserve::Router::new().route("/", get(|| async move { "Hello World" })) } } pub fn start_http_api(spawner: Spawner, net_stack: Stack<'static>) { spawner.spawn( handler(HttpApiApp::new( net_stack, Default::default(), Default::default(), )) .unwrap(), ); } #[embassy_executor::task] async fn handler(mut service: HttpApiApp<'static, HttpApiProps>) { service.run().await; } pub struct HttpApiConfig { pub port: u16, pub web_serve_config: Config, } impl Default for HttpApiConfig { fn default() -> Self { Self { port: 80, web_serve_config: Config::const_default().keep_connection_alive(), } } } pub struct HttpApiApp<'a, AB: AppBuilder> { config: HttpApiConfig, net_stack: Stack<'a>, app: AppRouter, tcp_rx_buffer: [u8; 1024], tcp_tx_buffer: [u8; 1024], http_buffer: [u8; 2048], } impl<'a, AB: AppBuilder> HttpApiApp<'a, AB> { pub fn new(net_stack: Stack<'a>, config: HttpApiConfig, app: AB) -> Self { Self { config: config, net_stack: net_stack, app: app.build_app(), tcp_rx_buffer: [0; 1024], tcp_tx_buffer: [0; 1024], http_buffer: [0; 2048], } } pub async fn run(&mut self) { picoserve::Server::new( &self.app, &self.config.web_serve_config, &mut self.http_buffer, ) .listen_and_serve( 0, self.net_stack, self.config.port, &mut self.tcp_rx_buffer, &mut self.tcp_tx_buffer, ) .await; } }