feat: port prns.cc to � blazing fast � rust

This commit is contained in:
sam 2023-07-27 23:51:12 +02:00
parent d50f34529c
commit 6754296a48
No known key found for this signature in database
GPG Key ID: B4EF20DDE721CAA1
9 changed files with 945 additions and 24 deletions

838
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,6 @@
[workspace]
members = [
"pronouns",
"exporter",
"prns",
]

16
prns/Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "prns"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
pronouns = { path = "../pronouns" }
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"

87
prns/src/main.rs Normal file
View File

@ -0,0 +1,87 @@
use pronouns::models::{member::Member, user::User};
use rocket::{response::Redirect, State};
use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
use std::env;
use tracing::info;
use tracing_subscriber;
#[macro_use]
extern crate rocket;
struct AppState {
pool: Pool<Postgres>,
base_url: String,
}
#[get("/<sid>")]
async fn redirect(state: &State<AppState>, sid: &str) -> Redirect {
match sid.len() {
5 => sqlx::query_as!(
User,
"SELECT id, sid, username FROM users WHERE sid = $1",
sid
)
.fetch_one(&state.pool)
.await
.map_or_else(
|_| Redirect::temporary(format!("{}", state.base_url)),
|u| Redirect::temporary(format!("{}/@{}", state.base_url, u.username)),
),
6 => {
let member = match sqlx::query_as!(
Member,
"SELECT id, user_id, sid, name FROM members WHERE sid = $1",
sid
)
.fetch_one(&state.pool)
.await
{
Err(_) => return Redirect::temporary(format!("{}", state.base_url)),
Ok(val) => val,
};
sqlx::query_as!(
User,
"SELECT id, sid, username FROM users WHERE id = $1",
member.user_id
)
.fetch_one(&state.pool)
.await
.map_or_else(
|_| Redirect::temporary(format!("{}", state.base_url)),
|u| {
Redirect::temporary(format!(
"{}/@{}/{}",
state.base_url, u.username, member.name
))
},
)
}
_ => Redirect::temporary(format!("{}", state.base_url)),
}
}
#[launch]
async fn rocket() -> _ {
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 base_url = env::var("BASE_URL").expect("BASE_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 prns.cc server");
rocket::build()
.manage(AppState { pool, base_url })
.mount("/", routes![redirect])
}

10
pronouns/Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "pronouns"
version = "0.1.0"
edition = "2021"
[dependencies]
time = { version = "0.3.23", features = ["formatting", "serde"] }
serde = { version = "1.0.159", features = ["derive"] }
serde_json = { version = "1.0.95", features = ["raw_value"] }
sqlx = { version = "0.6.3", features = ["runtime-tokio-rustls", "postgres", "json"]}

1
pronouns/src/lib.rs Normal file
View File

@ -0,0 +1 @@
pub mod models;

2
pronouns/src/models.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod user;
pub mod member;

View File

@ -0,0 +1,7 @@
#[derive(Debug)]
pub struct Member {
pub id: String,
pub user_id: String,
pub sid: String,
pub name: String,
}

View File

@ -0,0 +1,6 @@
#[derive(Debug)]
pub struct User {
pub id: String,
pub sid: String,
pub username: String,
}