- #[embassy_executor::task(pool_size = 3)]
- pub async fn task(stack: &'static Stack<WifiDevice<'static, WifiStaDevice>>) {
- let mut rx_buffer = [0; RX_BUFFER_SIZE];
- let mut tx_buffer = [0; TX_BUFFER_SIZE];
-
- let router = Router::new()
- .post(("valve", u8::segment()), control_valve)
- .get("/panic", || async { panic!("oops") })
- .get("/sleep", || async { Timer::after_secs(3).await })
- .get("/ping", || async { "pong" })
- .with_state(AppState);
-
- loop {
- let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
-
- if let Err(e) = socket.accept(80).await {
- log::warn!("accept error: {:?}", e);
- continue;
- }
-
- if let Some(IpEndpoint { addr, port }) = socket.remote_endpoint() {
- log::info!("connection from: {addr}:{port}");
- }
-
- let (read, write) = socket.split();
- if let Err(err) = router.serve(read, write).await {
- log::warn!("failed to serve request: {err:?}");
- };
-
- socket.flush().await.log_ignore();
- socket.close();
- Timer::after(Duration::from_millis(100)).await;
- socket.abort();
- }
- }