this repo has no description
0
fork

Configure Feed

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

Log HTTP events.

+118 -25
+10 -8
server/src/client_communication.rs
··· 1 1 use crate::user::User; 2 2 use crate::{ 3 - AppState, LuminaError, error_elog, incoming_elog, info_elog, registration_error_elog, 4 - warn_elog, 3 + error_elog, http_code_elog, incoming_elog, info_elog, registration_error_elog, warn_elog, AppState, LuminaError 5 4 }; 6 5 use cynthia_con::{CynthiaColors, CynthiaStyles}; 7 6 extern crate rocket; ··· 9 8 use uuid::Uuid; 10 9 11 10 #[get("/connection")] 12 - pub(crate) fn wsconnection<'k>(ws: ws::WebSocket, state: &'k State<AppState>) -> ws::Channel<'k> { 11 + pub(crate) async fn wsconnection<'k>(ws: ws::WebSocket, state: &'k State<AppState>) -> ws::Channel<'k> { 12 + let ev_log = { 13 + let appstate = state.0.clone(); 14 + appstate.2.clone().await 15 + }; 16 + http_code_elog!(ev_log, 200, "/connection"); 13 17 use rocket::futures::{SinkExt, StreamExt}; 14 18 15 19 ws.channel(move |mut stream| { 16 20 Box::pin(async move { 17 - let mut client_session_data: SessionData = SessionData { 21 + http_code_elog!(ev_log, 101, "/connection"); 22 + 23 + let mut client_session_data: SessionData = SessionData { 18 24 client_type: None, 19 25 user: None, 20 - }; 21 - let ev_log = { 22 - let appstate = state.0.clone(); 23 - appstate.2.clone().await 24 26 }; 25 27 while let Some(message) = stream.next().await { 26 28 match message? {
+59 -10
server/src/helpers/events.rs
··· 2 2 use crate::helpers::message_prefixes; 3 3 use crate::{LuminaError, database}; 4 4 use chrono::Utc; 5 + use cynthia_con::{CynthiaColors, CynthiaStyles}; 5 6 6 7 /// Levels of logging supported by the Logger. 7 8 #[derive(Debug)] ··· 15 16 Incoming, 16 17 RegistrationError, 17 18 SoftError, 19 + HTTPCode(u16), 18 20 } 19 21 20 22 /// A reusable logger that logs messages to stdout with colored prefixes ··· 70 72 } 71 73 } 72 74 } 75 + 76 + pub async fn clone(&self) -> Self { 77 + match self { 78 + EventLogger::WithDatabase { db } => Self::from_db(db).await, 79 + EventLogger::OnlyStdout => Self::OnlyStdout, 80 + } 81 + } 82 + 73 83 /// Logs a message with the specified log level. 74 84 /// This method prints to stdout with a colored prefix and, if a database connection is available, 75 85 /// asynchronously inserts a log entry in the logs table. ··· 90 100 EventType::Log => (log, false), 91 101 EventType::Incoming => (incoming, false), 92 102 EventType::RegistrationError => (registrationerror, true), 103 + EventType::HTTPCode(code) => { 104 + let codestring = match code { 105 + 101 => format!("[HTTP/{} (Switching Protocols)]", code) 106 + .color_blue() 107 + .style_bold(), 108 + 200..=299 => format!("[HTTP/{} (OK)]", code).color_ok_green().style_bold(), 109 + 400..=499 => format!("[HTTP/{} (Client Error)]", code) 110 + .color_yellow() 111 + .style_bold(), 112 + 113 + 500..=599 => format!("[HTTP/{} (Server Error)]", code 114 + ) 115 + .color_error_red() 116 + .style_bold(), 117 + _ => format!("[HTTP/{}]", code).color_blue().style_bold(), 118 + }; 119 + match code { 120 + 200..=499 => (codestring, false), 121 + 500..=599 => (codestring, true), 122 + _ => (codestring, false), 123 + } 124 + } 93 125 }; 94 126 95 127 let stdoutmsg = ··· 106 138 } 107 139 // Prepare the basic values for the log entry. 108 140 let level_str = match level { 109 - EventType::Info => "INFO", 110 - EventType::Warn => "WARN", 111 - EventType::SoftError | EventType::Error => "ERROR", 112 - EventType::Success => "SUCCESS", 113 - EventType::Failure => "FAILURE", 114 - EventType::Log => "LOG", 115 - EventType::Incoming => "INCOMING", 116 - EventType::RegistrationError => "REGISTRATION_ERROR", 117 - } 118 - .to_string(); 141 + EventType::Info => String::from("INFO"), 142 + EventType::Warn => String::from("WARN"), 143 + EventType::SoftError | EventType::Error => String::from("ERROR"), 144 + EventType::Success => String::from("SUCCESS"), 145 + EventType::Failure => String::from("FAILURE"), 146 + EventType::Log => String::from("LOG"), 147 + EventType::Incoming => String::from("INCOMING"), 148 + EventType::RegistrationError => String::from("REGISTRATION_ERROR"), 149 + EventType::HTTPCode(code) => 150 + format!("HTTP/{}", code) 151 + 152 + }; 119 153 let ansi_regex = regex::Regex::new(r"\x1B\[[0-?]*[ -/]*[@-~]").unwrap(); 120 154 121 155 let message_db: String = ansi_regex ··· 198 232 pub async fn registration_error(&self, message: &str) { 199 233 self.log(EventType::RegistrationError, message).await 200 234 } 235 + 236 + /// Convenience method to log an HTTP code message. 237 + pub async fn http_code(&self, code: u16, message: &str) { 238 + self.log(EventType::HTTPCode(code), message).await 239 + } 201 240 } 202 241 #[macro_export] 203 242 macro_rules! info_elog { ··· 269 308 $logger.registration_error(&format!($($arg)*)).await 270 309 }; 271 310 } 311 + 312 + #[macro_export] 313 + /// Takes an event log object and then runs .http_code on it, formatting using the other 314 + /// arguments. 315 + macro_rules! http_code_elog { 316 + ($logger:expr, $code:expr, $($arg:tt)*) => 317 + { 318 + $logger.http_code($code, &format!($($arg)*)).await 319 + }; 320 + }
+49 -7
server/src/staticroutes.rs
··· 6 6 7 7 use rocket::response::content::RawHtml; 8 8 9 + use crate::{AppState, http_code_elog}; 10 + extern crate rocket; 11 + use rocket::State; 12 + 9 13 #[get("/")] 10 - pub(crate) async fn index() -> RawHtml<String> { 14 + pub(crate) async fn index<'k>(state: &'k State<AppState>) -> RawHtml<String> { 15 + let ev_log = { 16 + let appstate = state.0.clone(); 17 + appstate.2.clone().await 18 + }; 19 + http_code_elog!(ev_log, 200, "/"); 20 + 11 21 RawHtml( 12 22 r#"<!doctype html> 13 23 <html lang="en"> ··· 33 43 } 34 44 35 45 #[get("/static/lumina.min.mjs")] 36 - pub(crate) async fn lumina_js() -> RawJavaScript<String> { 46 + pub(crate) async fn lumina_js<'k>(state: &'k State<AppState>) -> RawJavaScript<String> { 47 + let ev_log = { 48 + let appstate = state.0.clone(); 49 + appstate.2.clone().await 50 + }; 51 + http_code_elog!(ev_log, 200, "/static/lumina.min.mjs"); 52 + 37 53 RawJavaScript(include_str!("../../client/priv/static/lumina_client.min.mjs").to_string()) 38 54 } 39 55 40 56 #[get("/static/lumina.css")] 41 - pub(crate) async fn lumina_css() -> RawCss<String> { 57 + pub(crate) async fn lumina_css<'k>(state: &'k State<AppState>) -> RawCss<String> { 58 + let ev_log = { 59 + let appstate = state.0.clone(); 60 + appstate.2.clone().await 61 + }; 62 + http_code_elog!(ev_log, 200, "/static/lumina.css"); 63 + 42 64 RawCss(include_str!("../../client/priv/static/lumina_client.css").to_string()) 43 65 } 44 66 45 67 #[get("/static/logo.svg")] 46 - pub(crate) async fn logo_svg() -> (ContentType, &'static str) { 68 + pub(crate) async fn logo_svg<'k>(state: &'k State<AppState>) -> (ContentType, &'static str) { 69 + let ev_log = { 70 + let appstate = state.0.clone(); 71 + appstate.2.clone().await 72 + }; 73 + http_code_elog!(ev_log, 200, "/static/logo.svg"); 74 + 47 75 ( 48 76 ContentType::SVG, 49 77 include_str!("../../client/priv/static/logo.svg"), ··· 51 79 } 52 80 53 81 #[get("/favicon.ico")] 54 - pub(crate) async fn favicon() -> (ContentType, &'static [u8]) { 55 - logo_png().await 82 + pub(crate) async fn favicon<'k>(state: &'k State<AppState>) -> (ContentType, &'static [u8]) { 83 + let ev_log = { 84 + let appstate = state.0.clone(); 85 + appstate.2.clone().await 86 + }; 87 + http_code_elog!(ev_log, 200, "/favicon.ico"); 88 + produce_logo_png() 56 89 } 57 90 58 91 #[get("/static/logo.png")] 59 - pub(crate) async fn logo_png() -> (ContentType, &'static [u8]) { 92 + pub(crate) async fn logo_png<'k>(state: &'k State<AppState>) -> (ContentType, &'static [u8]) { 93 + let ev_log = { 94 + let appstate = state.0.clone(); 95 + appstate.2.clone().await 96 + }; 97 + http_code_elog!(ev_log, 200, "/static/logo.png"); 98 + produce_logo_png() 99 + } 100 + 101 + fn produce_logo_png() -> (ContentType, &'static [u8]) { 60 102 ( 61 103 ContentType::PNG, 62 104 include_bytes!("../../client/priv/static/logo.png"),