this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: update appstate structure and improve event logging in client communication and static routes

+26 -26
+6 -6
server/src/client_communication.rs
··· 53 53 ) -> ws::Channel<'k> { 54 54 let ev_log = { 55 55 let appstate = state.0.clone(); 56 - appstate.2.clone().await 56 + appstate.event_logger.clone().await 57 57 }; 58 58 http_code_elog!(ev_log, 200, "/connection"); 59 59 use rocket::futures::{SinkExt, StreamExt}; ··· 85 85 match try_revive { 86 86 Some(token) => { 87 87 let appstate = state.0.clone(); 88 - let db = &appstate.1.lock().await; 88 + let db = &appstate.db.lock().await; 89 89 match User::revive_session_from_token(token.clone(), db).await { 90 90 Ok(user) => { 91 91 incoming_elog!(ev_log, "Session revived for user: {}", ··· 144 144 // register the user 145 145 { 146 146 let appstate = state.0.clone(); 147 - let db = &appstate.1.lock().await; 147 + let db = &appstate.db.lock().await; 148 148 match User::create_user(email.clone(), username.clone(), password, db).await 149 149 { 150 150 Ok(user) => { ··· 237 237 } 238 238 Ok(Message::RegisterPrecheck { email, username, password }) => { 239 239 let appstate = state.0.clone(); 240 - let db = &appstate.1.lock().await; 240 + let db = &appstate.db.lock().await; 241 241 match crate::user::register_validitycheck(email, username, password, db).await { 242 242 Err(LuminaError::RegisterEmailInUse) => { 243 243 let _ = stream.send(ws::Message::from(msgtojson(Message::RegisterPrecheckResponse { ··· 285 285 let _ = stream.send(ws::Message::from(msgtojson(Message::AuthFailure))).await; 286 286 } else { 287 287 let appstate = state.0.clone(); 288 - let db = &appstate.1.lock().await; 288 + let db = &appstate.db.lock().await; 289 289 let msgback = match User::authenticate(email_username.clone(), password, db, ev_log.clone().await).await { 290 290 Ok((session_reference, user)) => { 291 291 incoming_elog!(ev_log,"User {} authenticated to session with id {}.\n{}", user.username.clone().color_bright_cyan(), session_reference.session_id.to_string().color_pink(), format!("(User id: {})", user.id).style_dim()); ··· 339 339 } 340 340 Ok(Message::TimelineRequest { by_name: name, page }) => { 341 341 let appstate = state.0.clone(); 342 - let db = &appstate.1.lock().await; 342 + let db = &appstate.db.lock().await; 343 343 // Fetch post IDs for the requested timeline 344 344 match fetch_timeline_post_ids_by_timeline_name( 345 345 ev_log.clone().await,
+12 -12
server/src/main.rs
··· 40 40 use uuid::Uuid; 41 41 mod user; 42 42 use tokio_postgres as postgres; 43 - struct AppState(Arc<(ServerConfig, Mutex<DbConn>, EventLogger)>); 43 + struct AppState(Arc<InnerAppState>); 44 + struct InnerAppState { 45 + config: ServerConfig, 46 + db: Mutex<DbConn>, 47 + event_logger: EventLogger, 48 + } 44 49 mod rate_limiter; 45 50 use database::DbConn; 46 51 use rate_limiter::{AuthRateLimiter, GeneralRateLimiter}; ··· 160 165 } 161 166 // If we got here, we have a database connection. 162 167 163 - 164 168 let db = db_mut.unwrap(); 165 169 let pg = DbConn::to_pgconn(db.recreate().await.unwrap()); 166 170 let ev_log = EventLogger::from_db(&pg).await; ··· 256 260 ) 257 261 .await 258 262 .unwrap_or(()); 259 - 260 263 } 261 264 z => { 262 265 println!( 263 266 "Ran into some issues: user 1: {:?}, user 2: {:?} ", 264 267 z.0, z.1 265 268 ); 266 - 267 269 } 268 270 } 269 271 } ··· 271 273 } 272 274 } 273 275 274 - let appstate = AppState(Arc::from(( 275 - config.clone(), 276 - Mutex::from(db), 277 - ev_log.clone().await, 278 - ))); 276 + let appstate = AppState(Arc::from(InnerAppState { 277 + config: config.clone(), 278 + db: Mutex::from(db), 279 + event_logger: ev_log.clone().await, 280 + })); 279 281 280 282 // Create a simple in-memory IP-based rate limiter. 281 283 // Default: allow 5 events per 10 seconds (0.5 tokens/sec) with capacity 10. ··· 367 369 let result = { 368 370 let g = s.await; 369 371 match g { 370 - Ok(x) => x.map_err(|e| { 371 - (LuminaError::RocketFaillure, Some(e)) 372 - }), 372 + Ok(x) => x.map_err(|e| (LuminaError::RocketFaillure, Some(e))), 373 373 Err(..) => Err((LuminaError::JoinFaillure, None)), 374 374 } 375 375 };
+8 -8
server/src/staticroutes.rs
··· 36 36 pub(crate) async fn index(state: &State<AppState>) -> RawHtml<String> { 37 37 let ev_log = { 38 38 let appstate = state.0.clone(); 39 - appstate.2.clone().await 39 + appstate.event_logger.clone().await 40 40 }; 41 41 http_code_elog!(ev_log, 200, "/"); 42 42 let js = if cfg!(debug_assertions) { ··· 71 71 pub(crate) async fn lumina_js(state: &State<AppState>) -> RawJavaScript<String> { 72 72 let ev_log = { 73 73 let appstate = state.0.clone(); 74 - appstate.2.clone().await 74 + appstate.event_logger.clone().await 75 75 }; 76 76 http_code_elog!(ev_log, 200, "/static/lumina.min.mjs"); 77 77 ··· 82 82 pub(crate) async fn lumina_d_js(state: &State<AppState>) -> RawJavaScript<String> { 83 83 let ev_log = { 84 84 let appstate = state.0.clone(); 85 - appstate.2.clone().await 85 + appstate.event_logger.clone().await 86 86 }; 87 87 http_code_elog!(ev_log, 200, "/static/lumina.mjs"); 88 88 ··· 93 93 pub(crate) async fn lumina_css(state: &State<AppState>) -> RawCss<String> { 94 94 let ev_log = { 95 95 let appstate = state.0.clone(); 96 - appstate.2.clone().await 96 + appstate.event_logger.clone().await 97 97 }; 98 98 http_code_elog!(ev_log, 200, "/static/lumina.css"); 99 99 ··· 104 104 pub(crate) async fn licence(state: &State<AppState>) -> RawText<String> { 105 105 let ev_log = { 106 106 let appstate = state.0.clone(); 107 - appstate.2.clone().await 107 + appstate.event_logger.clone().await 108 108 }; 109 109 http_code_elog!(ev_log, 200, "/licence"); 110 110 ··· 119 119 pub(crate) async fn logo_svg(state: &State<AppState>) -> (ContentType, &'static str) { 120 120 let ev_log = { 121 121 let appstate = state.0.clone(); 122 - appstate.2.clone().await 122 + appstate.event_logger.clone().await 123 123 }; 124 124 http_code_elog!(ev_log, 200, "/static/logo.svg"); 125 125 ··· 133 133 pub(crate) async fn favicon(state: &State<AppState>) -> (ContentType, &'static [u8]) { 134 134 let ev_log = { 135 135 let appstate = state.0.clone(); 136 - appstate.2.clone().await 136 + appstate.event_logger.clone().await 137 137 }; 138 138 http_code_elog!(ev_log, 200, "/favicon.ico"); 139 139 produce_logo_png() ··· 143 143 pub(crate) async fn logo_png(state: &State<AppState>) -> (ContentType, &'static [u8]) { 144 144 let ev_log = { 145 145 let appstate = state.0.clone(); 146 - appstate.2.clone().await 146 + appstate.event_logger.clone().await 147 147 }; 148 148 http_code_elog!(ev_log, 200, "/static/logo.png"); 149 149 produce_logo_png()