Skip to content

Commit

Permalink
Join solution
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Sep 4, 2024
1 parent b880f6e commit 4bcd14e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions crates/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
miniserve = { path = "../miniserve" }
chatbot = { path = "../chatbot" }
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.121"
tokio = { workspace = true, features = ["full"] }
13 changes: 10 additions & 3 deletions crates/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use miniserve::{http::StatusCode, Content, Request, Response};
use serde::{Deserialize, Serialize};
use tokio::join;

async fn index(_req: Request) -> Response {
let content = include_str!("../index.html").to_string();
Expand All @@ -18,9 +19,15 @@ async fn chat(req: Request) -> Response {
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());

let (i, mut responses) = join!(
chatbot::gen_random_number(),
chatbot::query_chat(&messages.messages)
);

let response = responses.remove(i % responses.len());
messages.messages.push(response);

Ok(Content::Json(serde_json::to_string(&messages).unwrap()))
}

Expand Down

0 comments on commit 4bcd14e

Please sign in to comment.