refactor: restructure the picoserve code into HttpApiService
This commit is contained in:
+8
-23
@@ -18,8 +18,8 @@ use esp_hal::timer::timg::TimerGroup;
|
||||
use esp_println as _;
|
||||
use esp_radio::wifi::sta::StationConfig;
|
||||
use esp_radio::wifi::{ControllerConfig, Interface, WifiController};
|
||||
use picoserve::routing::get;
|
||||
use picoserve::{AppBuilder, AppRouter};
|
||||
|
||||
use cover_theif::http_api::{self, HttpApiProps, HttpApiService};
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
@@ -40,16 +40,6 @@ macro_rules! mk_static {
|
||||
const SSID: &str = env!("SSID");
|
||||
const PASSWORD: &str = env!("PASSWORD");
|
||||
|
||||
struct AppProps;
|
||||
|
||||
impl AppBuilder for AppProps {
|
||||
type PathRouter = impl picoserve::routing::PathRouter;
|
||||
|
||||
fn build_app(self) -> picoserve::Router<Self::PathRouter> {
|
||||
picoserve::Router::new().route("/", get(|| async move { "Hello World" }))
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(
|
||||
clippy::large_stack_frames,
|
||||
reason = "it's not unusual to allocate larger buffers etc. in main"
|
||||
@@ -109,17 +99,7 @@ async fn main(spawner: Spawner) -> ! {
|
||||
info!("Got IP: {}", config.address);
|
||||
}
|
||||
|
||||
let app = mk_static!(AppRouter<AppProps>, AppProps.build_app());
|
||||
|
||||
let port = 80;
|
||||
let mut tcp_rx_buffer = [0; 1024];
|
||||
let mut tcp_tx_buffer = [0; 1024];
|
||||
let mut http_buffer = [0; 2048];
|
||||
let web_serve_config = picoserve::Config::const_default().keep_connection_alive();
|
||||
|
||||
picoserve::Server::new(app, &web_serve_config, &mut http_buffer)
|
||||
.listen_and_serve(0, net_stack, port, &mut tcp_rx_buffer, &mut tcp_tx_buffer)
|
||||
.await;
|
||||
spawner.spawn(http_api_serve_task(HttpApiService::new(net_stack, Default::default(), Default::default())).unwrap());
|
||||
|
||||
loop {
|
||||
info!("Hello world");
|
||||
@@ -157,3 +137,8 @@ async fn connection(mut controller: WifiController<'static>) {
|
||||
async fn net_task(mut runner: Runner<'static, Interface<'static>>) {
|
||||
runner.run().await
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn http_api_serve_task(mut service: HttpApiService<'static, HttpApiProps>) {
|
||||
service.serve().await;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
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> {
|
||||
pub 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 serve(&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;
|
||||
}
|
||||
}
|
||||
@@ -1 +1,10 @@
|
||||
#![no_std]
|
||||
#![deny(
|
||||
clippy::mem_forget,
|
||||
reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
|
||||
holding buffers for the duration of a data transfer."
|
||||
)]
|
||||
#![deny(clippy::large_stack_frames)]
|
||||
#![feature(impl_trait_in_assoc_type)]
|
||||
|
||||
pub mod http_api;
|
||||
|
||||
Reference in New Issue
Block a user