diff --git a/crates/server/Cargo.toml b/crates/server/Cargo.toml index d53cd72..ab19094 100644 --- a/crates/server/Cargo.toml +++ b/crates/server/Cargo.toml @@ -4,4 +4,6 @@ version = "0.1.0" edition = "2021" [dependencies] -miniserve = { path = "../miniserve" } \ No newline at end of file +miniserve = { path = "../miniserve" } +serde = { version = "1.0.204", features = ["derive"] } +serde_json = "1.0.121" diff --git a/crates/server/src/main.rs b/crates/server/src/main.rs index d191abb..0ea2fa8 100644 --- a/crates/server/src/main.rs +++ b/crates/server/src/main.rs @@ -1,10 +1,32 @@ -use miniserve::{Content, Request, Response}; +use miniserve::{http::StatusCode, Content, Request, Response}; +use serde::{Deserialize, Serialize}; fn index(_req: Request) -> Response { let content = include_str!("../index.html").to_string(); Ok(Content::Html(content)) } +#[derive(Serialize, Deserialize)] +struct Messages { + messages: Vec<String>, +} + +fn chat(req: Request) -> Response { + let Request::Post(body) = req else { + return Err(StatusCode::METHOD_NOT_ALLOWED); + }; + let Ok(mut messages) = serde_json::from_str::<Messages>(&body) else { + return Err(StatusCode::INTERNAL_SERVER_ERROR); + }; + messages + .messages + .push("And how does that make you feel?".into()); + Ok(Content::Json(serde_json::to_string(&messages).unwrap())) +} + fn main() { - miniserve::Server::new().route("/", index).run() + miniserve::Server::new() + .route("/", index) + .route("/chat", chat) + .run() }