From 8e00b432c475be5a55bc63aebd51912b15abe1a1 Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Tue, 30 Jul 2024 15:30:41 -0700 Subject: [PATCH] Port server to async --- crates/server/Cargo.toml | 1 + crates/server/src/main.rs | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/server/Cargo.toml b/crates/server/Cargo.toml index ab19094..8ebffb1 100644 --- a/crates/server/Cargo.toml +++ b/crates/server/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" miniserve = { path = "../miniserve" } serde = { version = "1.0.204", features = ["derive"] } serde_json = "1.0.121" +tokio = { workspace = true, features = ["full"] } diff --git a/crates/server/src/main.rs b/crates/server/src/main.rs index 0ea2fa8..2a4ad2f 100644 --- a/crates/server/src/main.rs +++ b/crates/server/src/main.rs @@ -1,7 +1,7 @@ use miniserve::{http::StatusCode, Content, Request, Response}; use serde::{Deserialize, Serialize}; -fn index(_req: Request) -> Response { +async fn index(_req: Request) -> Response { let content = include_str!("../index.html").to_string(); Ok(Content::Html(content)) } @@ -11,7 +11,7 @@ struct Messages { messages: Vec, } -fn chat(req: Request) -> Response { +async fn chat(req: Request) -> Response { let Request::Post(body) = req else { return Err(StatusCode::METHOD_NOT_ALLOWED); }; @@ -24,9 +24,11 @@ fn chat(req: Request) -> Response { Ok(Content::Json(serde_json::to_string(&messages).unwrap())) } -fn main() { +#[tokio::main] +async fn main() { miniserve::Server::new() .route("/", index) .route("/chat", chat) .run() + .await }