blatherskite

a toy discord-like chat app backend written for a swe class
Log | Files | Refs | README

commit 6942ba3bcdb4a19f296d4d6d36153d5327906bce
Author: quantumish <freifeld.david@gmail.com>
Date:   Tue, 20 Sep 2022 23:31:31 -0700

Initial commit.

Diffstat:
Achatterbox/Cargo.toml | 8++++++++
Achatterbox/src/main.rs | 3+++
Ascuttlebutt/.gitignore | 1+
Ascuttlebutt/Cargo.toml | 13+++++++++++++
Ascuttlebutt/src/main.rs | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 104 insertions(+), 0 deletions(-)

diff --git a/chatterbox/Cargo.toml b/chatterbox/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "chatterbox" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/chatterbox/src/main.rs b/chatterbox/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/scuttlebutt/.gitignore b/scuttlebutt/.gitignore @@ -0,0 +1 @@ +/target diff --git a/scuttlebutt/Cargo.toml b/scuttlebutt/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "scuttlebutt" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +ctor = "0.1.23" +poem = { version = "1.3.42", features = ["test"] } +poem-openapi = { version = "2.0.12", features = ["swagger-ui"] } +tokio = { version = "1", features = ["full"] } +tracing-subscriber = "0.3.15" diff --git a/scuttlebutt/src/main.rs b/scuttlebutt/src/main.rs @@ -0,0 +1,79 @@ +use poem::{listener::TcpListener, Route, Server, Result }; +use poem_openapi::{param::Query, payload::{PlainText, Json}, OpenApi, OpenApiService, Object, ApiResponse}; + +struct Api; + +#[derive(Object)] +struct User { + id: u64, + username: String, +} + +#[derive(ApiResponse)] +enum UserResponse { + /// Returns the user requested + #[oai(status = 200)] + User(Json<User>), + /// Returns when there is no user associated with the ID + #[oai(status = 404)] + NotFound(PlainText<String>) +} + +#[OpenApi] +impl Api { + #[oai(path = "/hello", method = "get")] + async fn hi(&self) -> PlainText<String> { + PlainText(String::from("whee")) + } + + #[oai(path = "/user", method = "get")] + /// Gets the user with the given ID + /// + /// # Example + /// + /// Call `/user/1234` to get the user with id 1234 + async fn get_user(&self, id: Query<u64>) -> UserResponse { + todo!() + } + + #[oai(path = "/user", method = "post")] + /// Makes a user + async fn make_user(&self, name: Query<String>, email: Query<String>, password: Query<String>) -> Result<Json<User>> { + todo!() + } +} + +#[tokio::main] +async fn main() -> Result<(), std::io::Error> { + if std::env::var_os("RUST_LOG").is_none() { + std::env::set_var("RUST_LOG", "poem=debug"); + } + tracing_subscriber::fmt::init(); + + let api_service = + OpenApiService::new(Api, "Hello World", "1.0").server("http://localhost:3000/api"); + let ui = api_service.swagger_ui(); + + Server::new(TcpListener::bind("127.0.0.1:3000")) + .run(Route::new().nest("/api", api_service).nest("/", ui)) + .await +} + +#[cfg(test)] +mod tests { + use super::*; + use poem::test::TestClient; + + fn setup() -> TestClient<Route> { + let app = OpenApiService::new(Api, "Hello World", "1.0").server("http://localhost:3000/api"); + TestClient::new(Route::new().nest("/api", app)) + } + + #[tokio::test] + async fn sanity() { + let cli = setup(); + let resp = cli.get("/api/hello").send().await; + resp.assert_status_is_ok(); + resp.assert_text("whee").await; + } +}