diff --git a/Cargo.lock b/Cargo.lock index fc6231e..d2bcea7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,56 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + [[package]] name = "anyhow" version = "1.0.100" @@ -213,6 +263,7 @@ dependencies = [ "argon2", "axum", "chrono", + "clap", "dashmap", "jsonwebtoken", "password-hash", @@ -242,6 +293,52 @@ dependencies = [ "windows-link", ] +[[package]] +name = "clap" +version = "4.5.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9e340e012a1bf4935f5282ed1436d1489548e8f72308207ea5df0e23d2d03f8" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d76b5d13eaa18c901fd2f7fca939fefe3a0727a953561fefdf3b2922b8569d00" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d" + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + [[package]] name = "concurrent-queue" version = "2.5.0" @@ -993,6 +1090,12 @@ dependencies = [ "hashbrown 0.16.1", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + [[package]] name = "itoa" version = "1.0.15" @@ -1255,6 +1358,12 @@ version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + [[package]] name = "openssl" version = "0.10.75" @@ -2087,6 +2196,12 @@ dependencies = [ "unicode-properties", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "subtle" version = "2.6.1" @@ -2539,6 +2654,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "uuid" version = "1.19.0" diff --git a/Cargo.toml b/Cargo.toml index 0c85e87..7645ce0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ anyhow = "1.0.99" argon2 = "0.5.3" axum = { version = "0.8.4", features = ["multipart", "ws"] } chrono = { version = "0.4.42", features = ["serde"] } +clap = { version = "4.5.53", features = ["derive"] } dashmap = "6.1.0" jsonwebtoken = "9.3.1" password-hash = "0.5.0" diff --git a/src/db.rs b/src/db.rs index 4f1011f..8e50c96 100644 --- a/src/db.rs +++ b/src/db.rs @@ -20,8 +20,7 @@ pub async fn room_id_from_uuid(db: &PgPool, room_uuid: Uuid) -> Result Result Result { + sqlx::query_scalar("SELECT name FROM room_ WHERE uuid = $1") + .bind(room_uuid) + .fetch_one(db) + .await + .map_err(|_| (StatusCode::NOT_FOUND, "Failed to find room".into())) +} diff --git a/src/main.rs b/src/main.rs index 03d9f92..8b29461 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,17 +2,36 @@ use axum::{ Extension, Router, http::{Method, header}, }; -use std::{env::var, net::SocketAddr, time::Duration}; +use clap::Parser; +use std::{net::SocketAddr, time::Duration}; use tower_governor::{GovernorLayer, governor::GovernorConfigBuilder}; -use tower_http::cors::{Any, CorsLayer}; +use tower_http::{ + cors::{Any, CorsLayer}, + trace::{DefaultMakeSpan, DefaultOnResponse, TraceLayer}, +}; +use tracing::Level; mod auth; mod db; mod realtime; mod routes; +#[derive(clap::Parser, Debug)] +#[command(author, version, about, long_about = None)] +pub struct Cli { + /// Server port + #[arg(short, long, default_value = "8080")] + port: String, + + /// Verbose mode + #[arg(short, long)] + verbose: bool, +} + #[tokio::main] async fn main() -> anyhow::Result<()> { + let cli = Cli::parse(); + tracing_subscriber::fmt() .with_max_level(tracing::Level::INFO) .init(); @@ -45,7 +64,7 @@ async fn main() -> anyhow::Result<()> { let realtime = realtime::Realtime::new(); - let app = Router::new() + let mut app = Router::new() .merge(routes::users::routes()) .merge(routes::rooms::routes()) .merge(routes::messages::routes()) @@ -54,14 +73,17 @@ async fn main() -> anyhow::Result<()> { .layer(Extension(db_pool)) .layer(Extension(realtime)) .layer(GovernorLayer::new(governor_conf)) - // .layer( - // TraceLayer::new_for_http() - // .make_span_with(DefaultMakeSpan::new().level(Level::INFO)) - // .on_response(DefaultOnResponse::new().level(Level::INFO)), - // ) .layer(cors); - let port = var("CHATAPP_SERVER_PORT").unwrap_or_else(|_| "8080".to_string()); + if cli.verbose { + app = app.layer( + TraceLayer::new_for_http() + .make_span_with(DefaultMakeSpan::new().level(Level::INFO)) + .on_response(DefaultOnResponse::new().level(Level::INFO)), + ); + } + + let port = cli.port; let addr = format!("0.0.0.0:{port}"); let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); diff --git a/src/routes/rooms.rs b/src/routes/rooms.rs index d6a536c..ee95669 100644 --- a/src/routes/rooms.rs +++ b/src/routes/rooms.rs @@ -7,7 +7,7 @@ use axum::{ use sqlx::{PgPool, Pool, Postgres}; use uuid::Uuid; -use crate::db::{id_from_username, user_id_from_uuid, username_from_id}; +use crate::db::{id_from_username, room_name_from_uuid, user_id_from_uuid, username_from_id}; use crate::{auth::verify_jwt, db::room_id_from_uuid}; #[derive(sqlx::FromRow, serde::Serialize)] @@ -27,6 +27,7 @@ pub struct NewRoomPayload { #[derive(sqlx::FromRow, serde::Serialize)] pub struct RoomInvite { pub room_uuid: Uuid, + pub room_name: String, pub sender_uuid: Uuid, pub sender_username: String, } @@ -197,7 +198,11 @@ async fn list_invites( let requests = sqlx::query_as::<_, RoomInvite>( r#" - SELECT r.uuid AS room_uuid, u.uuid AS sender_uuid, u.username AS sender_username + SELECT + r.uuid AS room_uuid, + r.name AS room_name, + u.uuid AS sender_uuid, + u.username AS sender_username FROM room_invite_ AS i JOIN user_ u ON u.id = i.sender JOIN room_ r ON r.id = i.room @@ -207,7 +212,8 @@ async fn list_invites( .bind(user_id) .fetch_all(&db) .await - .map_err(|_| { + .map_err(|e| { + tracing::error!("{e}"); ( StatusCode::INTERNAL_SERVER_ERROR, "Could not list room invites".into(), @@ -267,10 +273,13 @@ async fn send_invite( tracing::info!("bro"); + let room_name = room_name_from_uuid(&db, payload.room_uuid).await?; + Ok(( StatusCode::CREATED, Json(RoomInvite { room_uuid: payload.room_uuid, + room_name, sender_uuid: claims.sub, sender_username: username_from_id(&db, receiver_id).await?, }),