From 74ac9396c90be4ac9cadd063189491cfcdc6fc26 Mon Sep 17 00:00:00 2001 From: sam Date: Fri, 28 Jul 2023 02:21:33 +0200 Subject: [PATCH] polish prns.cc code --- Cargo.lock | 60 +++++++++++++++++++++++++---- Cargo.toml | 1 - exporter/Cargo.toml | 14 ------- exporter/src/main.rs | 90 -------------------------------------------- exporter/src/user.rs | 24 ------------ prns/Cargo.toml | 4 +- prns/src/main.rs | 38 +++++++++++++------ 7 files changed, 82 insertions(+), 149 deletions(-) delete mode 100644 exporter/Cargo.toml delete mode 100644 exporter/src/main.rs delete mode 100644 exporter/src/user.rs diff --git a/Cargo.lock b/Cargo.lock index 688e3b0..f345dea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -194,6 +194,17 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "colored" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2674ec482fbc38012cf31e6c42ba0177b431a0cb6f15fe40efa5aab1bda516f6" +dependencies = [ + "is-terminal", + "lazy_static", + "windows-sys 0.48.0", +] + [[package]] name = "cookie" version = "0.17.0" @@ -801,12 +812,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.17" +version = "0.4.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" -dependencies = [ - "cfg-if", -] +checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" [[package]] name = "loom" @@ -936,6 +944,15 @@ dependencies = [ "libc", ] +[[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", +] + [[package]] name = "once_cell" version = "1.17.1" @@ -1074,13 +1091,13 @@ name = "prns" version = "0.1.0" dependencies = [ "dotenvy", + "log", "pronouns", "rocket", + "simple_logger", "sqlx", "time", "tokio", - "tracing", - "tracing-subscriber", ] [[package]] @@ -1512,6 +1529,18 @@ dependencies = [ "libc", ] +[[package]] +name = "simple_logger" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2230cd5c29b815c9b699fb610b49a5ed65588f3509d9f0108be3a885da629333" +dependencies = [ + "colored", + "log", + "time", + "windows-sys 0.42.0", +] + [[package]] name = "slab" version = "0.4.8" @@ -1766,6 +1795,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59e399c068f43a5d116fedaf73b203fa4f9c519f17e2b34f63221d3792f81446" dependencies = [ "itoa", + "libc", + "num_threads", "serde", "time-core", "time-macros", @@ -2231,6 +2262,21 @@ dependencies = [ "windows-targets 0.48.1", ] +[[package]] +name = "windows-sys" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + [[package]] name = "windows-sys" version = "0.45.0" diff --git a/Cargo.toml b/Cargo.toml index f0856a4..fd18838 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,5 @@ [workspace] members = [ "pronouns", - "exporter", "prns", ] diff --git a/exporter/Cargo.toml b/exporter/Cargo.toml deleted file mode 100644 index 42301fa..0000000 --- a/exporter/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -name = "exporter" -version = "0.1.0" -edition = "2021" - -[dependencies] -axum = "0.6.12" -dotenvy = "0.15.7" -serde = { version = "1.0.159", features = ["derive"] } -serde_json = "1.0.95" -sqlx = { version = "0.6.3", features= ["runtime-tokio-rustls", "postgres", "json"]} -tokio = { version = "1.27.0", features = ["full"] } -tracing = "0.1.37" -tracing-subscriber = "0.3.16" diff --git a/exporter/src/main.rs b/exporter/src/main.rs deleted file mode 100644 index 3fc5d6c..0000000 --- a/exporter/src/main.rs +++ /dev/null @@ -1,90 +0,0 @@ -use std::env; -use std::net::SocketAddr; -use std::sync::Arc; - -use axum::{ - extract::{Path, State}, - http::StatusCode, - routing::get, - Json, Router, -}; -use sqlx::{postgres::PgPoolOptions, Pool, Postgres}; -use tracing::{error, info}; -use tracing_subscriber; -use user::ExportUser; - -use crate::user::DBUser; - -pub mod user; - -struct AppState { - pool: Pool, -} - -#[tokio::main] -async fn main() { - tracing_subscriber::fmt::init(); - - info!("Loading .env"); - - dotenvy::dotenv().unwrap(); - - info!("Connecting to database"); - - let db_dsn = env::var("DATABASE_URL").expect("DATABASE_URL is not set or invalid!"); - - let pool = PgPoolOptions::new() - .max_connections(10) - .connect(db_dsn.as_str()) - .await - .expect("Could not connect do database!"); - - info!("Initializing exporter server"); - - let shared_state = Arc::new(AppState { pool }); - - let app = Router::new() - .route("/start/:id", get(handler)) - .with_state(shared_state); - - let port = match env::var("EXPORTER_PORT") { - Ok(val) => val - .parse::() - .expect("EXPORTER_PORT is not a valid integer!"), - Err(_) => { - error!("EXPORTER_PORT is not set, falling back to port 8081"); - 8081 - } - }; - - info!(port, "Starting server"); - - let addr = SocketAddr::from(([127, 0, 0, 1], port)); - axum::Server::bind(&addr) - .serve(app.into_make_service()) - .await - .unwrap(); -} - -async fn handler( - State(state): State>, - Path(user_id): Path, -) -> Result<(StatusCode, Json), (StatusCode, String)> { - info!(user_id = user_id, "Received thing :)"); - - let user = match sqlx::query_as!( - DBUser, - "SELECT id, username, display_name FROM users WHERE id = $1", - user_id - ) - .fetch_one(&state.pool) - .await - { - Ok(val) => val, - Err(why) => return Err((StatusCode::INTERNAL_SERVER_ERROR, format!("{}", why))), - }; - - info!(id = user.id, username = user.username, "Got user!"); - - Ok((StatusCode::ACCEPTED, Json(user.to_export()))) -} diff --git a/exporter/src/user.rs b/exporter/src/user.rs deleted file mode 100644 index 34bc5f2..0000000 --- a/exporter/src/user.rs +++ /dev/null @@ -1,24 +0,0 @@ -use serde::Serialize; - -pub struct DBUser { - pub id: String, - pub username: String, - pub display_name: Option, -} - -impl DBUser { - pub fn to_export(self) -> ExportUser { - ExportUser { - id: self.id, - username: self.username, - display_name: self.display_name, - } - } -} - -#[derive(Serialize, Debug)] -pub struct ExportUser { - pub id: String, - pub username: String, - pub display_name: Option, -} diff --git a/prns/Cargo.toml b/prns/Cargo.toml index 72ee5c8..16620f9 100644 --- a/prns/Cargo.toml +++ b/prns/Cargo.toml @@ -11,6 +11,6 @@ time = { version = "0.3.23", features = ["formatting", "serde"] } sqlx = { version = "0.6.3", features = ["runtime-tokio-rustls", "postgres", "json"]} rocket = "0.5.0-rc.3" tokio = { version = "1.27.0", features = ["full"] } -tracing = "0.1.37" -tracing-subscriber = "0.3.16" dotenvy = "0.15.7" +simple_logger = "4.2.0" +log = "0.4.19" diff --git a/prns/src/main.rs b/prns/src/main.rs index 4a54c07..cf8ef55 100644 --- a/prns/src/main.rs +++ b/prns/src/main.rs @@ -1,9 +1,8 @@ use pronouns::models::{member::Member, user::User}; -use rocket::{response::Redirect, State}; +use rocket::{response::Redirect, Config, State}; +use simple_logger; use sqlx::{postgres::PgPoolOptions, Pool, Postgres}; -use std::env; -use tracing::info; -use tracing_subscriber; +use std::{env, path::PathBuf}; #[macro_use] extern crate rocket; @@ -12,8 +11,10 @@ struct AppState { base_url: String, } -#[get("/")] -async fn redirect(state: &State, sid: &str) -> Redirect { +#[get("/")] +async fn redirect(state: &State, path: PathBuf) -> Redirect { + let sid = path.to_str().unwrap_or(""); + match sid.len() { 5 => sqlx::query_as!( User, @@ -23,7 +24,7 @@ async fn redirect(state: &State, sid: &str) -> Redirect { .fetch_one(&state.pool) .await .map_or_else( - |_| Redirect::temporary(format!("{}", state.base_url)), + |_| Redirect::temporary(state.base_url.clone()), |u| Redirect::temporary(format!("{}/@{}", state.base_url, u.username)), ), 6 => { @@ -35,7 +36,7 @@ async fn redirect(state: &State, sid: &str) -> Redirect { .fetch_one(&state.pool) .await { - Err(_) => return Redirect::temporary(format!("{}", state.base_url)), + Err(_) => return Redirect::temporary(state.base_url.clone()), Ok(val) => val, }; @@ -47,7 +48,7 @@ async fn redirect(state: &State, sid: &str) -> Redirect { .fetch_one(&state.pool) .await .map_or_else( - |_| Redirect::temporary(format!("{}", state.base_url)), + |_| Redirect::temporary(state.base_url.clone()), |u| { Redirect::temporary(format!( "{}/@{}/{}", @@ -56,13 +57,13 @@ async fn redirect(state: &State, sid: &str) -> Redirect { }, ) } - _ => Redirect::temporary(format!("{}", state.base_url)), + _ => Redirect::temporary(state.base_url.clone()), } } #[launch] async fn rocket() -> _ { - tracing_subscriber::fmt::init(); + simple_logger::SimpleLogger::new().env().init().unwrap(); info!("Loading .env"); @@ -72,6 +73,15 @@ async fn rocket() -> _ { let db_dsn = env::var("DATABASE_URL").expect("DATABASE_URL is not set or invalid!"); let base_url = env::var("BASE_URL").expect("BASE_URL is not set or invalid!"); + let port = match env::var("PRNS_PORT") { + Ok(val) => val + .parse::() + .expect("PRNS_PORT is not a valid integer!"), + Err(_) => { + error!("PRNS_PORT is not set, falling back to port 8087"); + 8087 + } + }; let pool = PgPoolOptions::new() .max_connections(10) @@ -81,7 +91,13 @@ async fn rocket() -> _ { info!("Initializing prns.cc server"); + let config = Config { + port, + ..Config::default() + }; + rocket::build() + .configure(config) .manage(AppState { pool, base_url }) .mount("/", routes![redirect]) }