Skip to content

Commit

Permalink
Port server to async
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Aug 21, 2024
1 parent 23af032 commit 83efab8
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
91 changes: 91 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
8 changes: 5 additions & 3 deletions crates/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -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))
}
Expand All @@ -11,7 +11,7 @@ struct Messages {
messages: Vec<String>,
}

fn chat(req: Request) -> Response {
async fn chat(req: Request) -> Response {
let Request::Post(body) = req else {
return Err(StatusCode::METHOD_NOT_ALLOWED);
};
Expand All @@ -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
}

0 comments on commit 83efab8

Please sign in to comment.