minor refactoring
This commit is contained in:
+16
-72
@@ -10,7 +10,8 @@ use rocket::{Build, Rocket};
|
||||
use rocket_cors::{AllowedOrigins, CorsOptions};
|
||||
use rocket_db_pools::{Connection, Database};
|
||||
use rocket_dyn_templates::Template;
|
||||
use std::sync::Arc;
|
||||
use std::env;
|
||||
use std::sync::{Arc, LazyLock};
|
||||
|
||||
use crate::auth::Session;
|
||||
use crate::db::{Postgres, Redis};
|
||||
@@ -20,33 +21,18 @@ pub mod cdn;
|
||||
pub mod db;
|
||||
pub mod handlers;
|
||||
pub mod llm;
|
||||
pub mod messages;
|
||||
pub mod messenger;
|
||||
pub mod user;
|
||||
|
||||
#[get("/users", rank = 2)]
|
||||
async fn users(_ag: Session, mut db: Connection<Postgres>) -> Json<Vec<i32>> {
|
||||
sqlx::query!("SELECT id FROM users")
|
||||
.fetch_all(&mut **db)
|
||||
.await
|
||||
.unwrap_or_else(|_| Vec::new())
|
||||
.into_iter()
|
||||
.map(|row| row.id)
|
||||
.collect::<Vec<i32>>()
|
||||
.into()
|
||||
}
|
||||
|
||||
#[get("/users/<id>", rank = 1)]
|
||||
async fn display_name(
|
||||
id: usize,
|
||||
_ag: Session,
|
||||
mut pgsql_conn: Connection<Postgres>,
|
||||
mut redis_conn: Connection<Redis>,
|
||||
) -> String {
|
||||
UserCache::username(id, &mut redis_conn, &mut pgsql_conn).await
|
||||
}
|
||||
static LMSTUDIO_URL: LazyLock<String> =
|
||||
LazyLock::new(|| env::var("LMSTUDIO_URL").expect("Ensure LMSTUDIO_URL is set!"));
|
||||
|
||||
#[launch]
|
||||
fn rocket() -> Rocket<Build> {
|
||||
let chat = Arc::new(crate::messages::ChatBroadcaster::new(32));
|
||||
// make sure the env is loaded
|
||||
dotenv::dotenv().expect("Failed to load env! aborting launch!");
|
||||
|
||||
let chat = Arc::new(crate::messenger::ChatBroadcaster::new(32));
|
||||
|
||||
let cors = CorsOptions::default()
|
||||
.allowed_origins(AllowedOrigins::all())
|
||||
@@ -70,7 +56,7 @@ fn rocket() -> Rocket<Build> {
|
||||
"/",
|
||||
routes![
|
||||
favicon,
|
||||
messages::chat_page,
|
||||
messenger::chat_page,
|
||||
auth::signup_page,
|
||||
auth::login_page,
|
||||
auth::mfa_page,
|
||||
@@ -81,11 +67,11 @@ fn rocket() -> Rocket<Build> {
|
||||
"/api",
|
||||
routes![
|
||||
cdn::upload_profile_pic,
|
||||
messages::get_messages,
|
||||
messages::post_message,
|
||||
messages::event_stream,
|
||||
users,
|
||||
display_name,
|
||||
messenger::get_messages,
|
||||
messenger::post_message,
|
||||
messenger::event_stream,
|
||||
user::users,
|
||||
user::display_name,
|
||||
auth::signup,
|
||||
auth::login,
|
||||
auth::get_totp,
|
||||
@@ -107,45 +93,3 @@ fn rocket() -> Rocket<Build> {
|
||||
async fn favicon() -> NamedFile {
|
||||
NamedFile::open("static/favicon.ico").await.unwrap()
|
||||
}
|
||||
|
||||
pub struct UserCache {}
|
||||
|
||||
impl UserCache {
|
||||
pub async fn username(
|
||||
id: usize,
|
||||
redis_conn: &mut Connection<Redis>,
|
||||
pgsql_conn: &mut Connection<Postgres>,
|
||||
) -> String {
|
||||
if let Ok(val) = cmd("GET")
|
||||
.arg(&[format!("users:{id}")])
|
||||
.query_async(&mut **redis_conn)
|
||||
.await
|
||||
{
|
||||
return val;
|
||||
}
|
||||
|
||||
if let Ok(v) = sqlx::query!("SELECT username FROM users WHERE id = $1", id as i32)
|
||||
.fetch_one(&mut ***pgsql_conn)
|
||||
.await
|
||||
{
|
||||
let username = v.username;
|
||||
Self::insert(id, &username, redis_conn).await;
|
||||
username
|
||||
} else {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn insert(id: usize, username: &str, conn: &mut Connection<Redis>) {
|
||||
cmd("SET")
|
||||
.arg(&[
|
||||
format!("users:{id}"),
|
||||
username.to_string(),
|
||||
"EX".to_string(),
|
||||
"1800".to_string(),
|
||||
])
|
||||
.query_async(&mut **conn)
|
||||
.await
|
||||
.expect("failed to insert key")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user