71 lines
1.6 KiB
Rust
71 lines
1.6 KiB
Rust
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<Self::PathRouter> {
|
|
picoserve::Router::new().route("/", get(|| async move { "Hello World" }))
|
|
}
|
|
}
|
|
|
|
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 HttpApiService<'a, AB: AppBuilder> {
|
|
config: HttpApiConfig,
|
|
|
|
net_stack: Stack<'a>,
|
|
app: AppRouter<AB>,
|
|
|
|
tcp_rx_buffer: [u8; 1024],
|
|
tcp_tx_buffer: [u8; 1024],
|
|
http_buffer: [u8; 2048],
|
|
}
|
|
|
|
impl<'a, AB: AppBuilder> HttpApiService<'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;
|
|
}
|
|
}
|