refactor: make all the services started in the same way with a start_ func and a handler task

This commit is contained in:
2026-08-27 23:16:11 -05:00
parent 51a450dad4
commit cbcb5b0b5f
3 changed files with 63 additions and 50 deletions
+19 -2
View File
@@ -1,3 +1,4 @@
use embassy_executor::Spawner;
use embassy_net::Stack;
use picoserve::routing::get;
use picoserve::{AppBuilder, AppRouter, Config};
@@ -13,6 +14,22 @@ impl AppBuilder for HttpApiProps {
}
}
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,
@@ -27,7 +44,7 @@ impl Default for HttpApiConfig {
}
}
pub struct HttpApiService<'a, AB: AppBuilder> {
pub struct HttpApiApp<'a, AB: AppBuilder> {
config: HttpApiConfig,
net_stack: Stack<'a>,
@@ -38,7 +55,7 @@ pub struct HttpApiService<'a, AB: AppBuilder> {
http_buffer: [u8; 2048],
}
impl<'a, AB: AppBuilder> HttpApiService<'a, AB> {
impl<'a, AB: AppBuilder> HttpApiApp<'a, AB> {
pub fn new(net_stack: Stack<'a>, config: HttpApiConfig, app: AB) -> Self {
Self {
config: config,