From b9ddec9f9e043b60d93bae3d5ae73d7597cf610e Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 19 Jan 2024 23:41:18 +0100 Subject: [PATCH] more tracing --- src/data/demo.rs | 20 ++++++++++++++++++-- src/data/maps.rs | 32 +++++++++++++++++++++++++++----- src/data/user.rs | 25 ++++++++++++++++++------- src/main.rs | 25 ++++++++++++++++--------- src/pages/about.rs | 6 ++++-- src/pages/api.rs | 1 + src/pages/demo.rs | 1 + src/pages/edit.rs | 1 + src/pages/index.rs | 24 ++++-------------------- src/pages/mod.rs | 3 ++- src/pages/plugin_section.rs | 4 +++- src/pages/profile.rs | 11 +++++------ src/pages/upload.rs | 4 +++- src/pages/uploads.rs | 11 +++++------ src/pages/viewer.rs | 1 + src/session.rs | 6 +++--- 16 files changed, 112 insertions(+), 63 deletions(-) diff --git a/src/data/demo.rs b/src/data/demo.rs index a096e07..6368d11 100644 --- a/src/data/demo.rs +++ b/src/data/demo.rs @@ -12,7 +12,7 @@ use sea_query_binder::SqlxBinder; use serde::{Deserialize, Deserializer}; use sqlx::{query_as, Executor, FromRow, Postgres}; use std::borrow::Cow; -use std::fmt::Write; +use std::fmt::{Debug, Formatter, Write}; use std::ops::Range; use std::str::FromStr; use time::format_description::well_known::Iso8601; @@ -41,6 +41,14 @@ pub struct Demo { pub chat: Vec, } +impl Debug for Demo { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Demo") + .field("id", &self.id) + .finish_non_exhaustive() + } +} + impl Demo { #[instrument(skip(connection))] pub async fn by_id( @@ -150,7 +158,7 @@ impl Render for ViewerUrl { } } -#[derive(Debug, FromRow)] +#[derive(FromRow)] pub struct ListDemo { pub id: i32, pub name: String, @@ -163,6 +171,14 @@ pub struct ListDemo { pub player_count: i32, } +impl Debug for ListDemo { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.debug_struct("ListDemo") + .field("id", &self.id) + .finish_non_exhaustive() + } +} + impl ListDemo { #[instrument(skip(connection))] pub async fn list( diff --git a/src/data/maps.rs b/src/data/maps.rs index 33cd6f1..0d58e60 100644 --- a/src/data/maps.rs +++ b/src/data/maps.rs @@ -1,12 +1,12 @@ use crate::Result; +use maud::Render; use sqlx::{query, Executor, Postgres}; +use std::fmt::{Debug, Formatter}; use tracing::instrument; #[instrument(skip(connection))] -pub async fn map_list( - connection: impl Executor<'_, Database = Postgres>, -) -> Result> { - Ok(query!( +pub async fn map_list(connection: impl Executor<'_, Database = Postgres>) -> Result { + let maps = query!( r#"SELECT map as "map!" FROM map_list @@ -15,5 +15,27 @@ pub async fn map_list( .fetch_all(connection) .await? .into_iter() - .map(|res| res.map)) + .map(|res| res.map); + Ok(MapList(maps.collect())) +} + +pub struct MapList(Vec); + +impl Debug for MapList { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{} maps", self.0.len()) + } +} + +impl Render for MapList { + fn render_to(&self, buffer: &mut String) { + let mut first = true; + for map in self.0.iter() { + if !first { + buffer.push(','); + } + buffer.push_str(map); + first = false; + } + } } diff --git a/src/data/user.rs b/src/data/user.rs index 1b1f5fc..9c81dff 100644 --- a/src/data/user.rs +++ b/src/data/user.rs @@ -1,26 +1,37 @@ use crate::data::steam_id::SteamId; use crate::{Error, Result}; +use maud::Render; use rand::distributions::Alphanumeric; use rand::Rng; use reqwest::get; -use serde::ser::SerializeStruct; use serde::{Deserialize, Serialize}; use sqlx::{query, query_as, Executor, Postgres}; use std::fmt::{Debug, Formatter}; -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Debug)] pub struct User { pub steam_id: SteamId, pub name: String, pub token: String, } -impl Debug for User { +pub struct Token(String); + +impl Debug for Token { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.debug_struct("User") - .field("steam_id", &self.steam_id) - .field("name", &self.name) - .finish_non_exhaustive() + f.write_str("redacted") + } +} + +impl Token { + pub fn new(token: String) -> Self { + Token(token) + } +} + +impl Render for Token { + fn render_to(&self, buffer: &mut String) { + self.0.render_to(buffer) } } diff --git a/src/main.rs b/src/main.rs index df51bf3..196f6cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use crate::asset::{guess_mime, serve_asset}; pub use crate::config::Config; use crate::config::Listen; use crate::data::demo::{Demo, Filter, ListDemo}; -use crate::data::maps::map_list; +use crate::data::maps::{map_list, MapList}; use crate::data::steam_id::SteamId; use crate::data::user::User; use crate::error::SetupError; @@ -52,7 +52,7 @@ use steam_openid::SteamOpenId; use tokio::signal::ctrl_c; use tonic::transport::{ClientTlsConfig, Identity}; use tower_http::trace::TraceLayer; -use tracing::{error, info, info_span}; +use tracing::{error, info, info_span, instrument}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer}; pub type Result = std::result::Result; @@ -62,7 +62,7 @@ struct App { openid: SteamOpenId, api: String, maps: String, - map_list: Vec, + map_list: MapList, pub session_store: MemoryStore, } @@ -137,7 +137,7 @@ async fn main() -> Result<()> { let config = setup()?; let connection = config.database.connect().await?; - let map_list = map_list(&connection).await?.collect(); + let map_list = map_list(&connection).await?; let session_store = MemoryStore::new(); let state = Arc::new(App { @@ -235,7 +235,7 @@ async fn main() -> Result<()> { Ok(()) } -#[axum::debug_handler] +#[instrument(skip(app))] async fn index( State(app): State>, session: SessionData, @@ -253,6 +253,7 @@ async fn index( )) } +#[instrument(skip(_app))] async fn about(State(_app): State>, session: SessionData) -> Result { Ok(render( AboutPage { @@ -262,6 +263,7 @@ async fn about(State(_app): State>, session: SessionData) -> Result>, session: SessionData) -> Result { Ok(render( ApiPage { @@ -272,6 +274,7 @@ async fn api(State(app): State>, session: SessionData) -> Result>, Path(id): Path, @@ -321,6 +324,7 @@ async fn login_callback( )) } +#[instrument(skip(app))] async fn login(State(app): State>) -> impl IntoResponse { ( StatusCode::FOUND, @@ -331,6 +335,7 @@ async fn login(State(app): State>) -> impl IntoResponse { ) } +#[instrument(skip(app, cookie))] async fn logout( State(app): State>, cookie: Option>, @@ -356,11 +361,12 @@ async fn logout( ) } +#[instrument(skip(app))] async fn upload(State(app): State>, session: SessionData) -> impl IntoResponse { if let Some(token) = session.token() { render( UploadPage { - key: token.as_str(), + key: &token, api: app.api.as_str(), }, session, @@ -375,14 +381,14 @@ async fn upload(State(app): State>, session: SessionData) -> impl IntoR } } -#[axum::debug_handler] +#[instrument(skip(app))] async fn demo_list(State(app): State>, filter: Option>) -> Result { let filter = filter.map(|filter| filter.0).unwrap_or_default(); let demos = ListDemo::list(&app.connection, filter).await?; Ok(DemoList { demos: &demos }.render()) } -#[axum::debug_handler] +#[instrument(skip(app))] async fn uploads( State(app): State>, session: SessionData, @@ -407,7 +413,7 @@ async fn uploads( )) } -#[axum::debug_handler] +#[instrument(skip(app))] async fn profiles( State(app): State>, session: SessionData, @@ -432,6 +438,7 @@ async fn profiles( )) } +#[instrument(skip(app))] async fn viewer( State(app): State>, id: Option>, diff --git a/src/pages/about.rs b/src/pages/about.rs index 98eb4cf..b11fba5 100644 --- a/src/pages/about.rs +++ b/src/pages/about.rs @@ -1,16 +1,18 @@ +use crate::data::user::Token; use crate::pages::plugin_section::PluginSection; use crate::pages::Page; use maud::{html, Markup}; use std::borrow::Cow; +#[derive(Debug)] pub struct AboutPage { - pub key: Option, + pub key: Option, } impl AboutPage { pub fn plugin_section(&self) -> PluginSection { PluginSection { - key: self.key.as_deref(), + key: self.key.as_ref(), } } } diff --git a/src/pages/api.rs b/src/pages/api.rs index a41e2b5..813657a 100644 --- a/src/pages/api.rs +++ b/src/pages/api.rs @@ -4,6 +4,7 @@ use maud::{html, Markup}; use std::borrow::Cow; use std::fmt::Display; +#[derive(Debug)] pub struct ApiPage<'a> { pub api_base: &'a str, pub steam_id: SteamId, diff --git a/src/pages/demo.rs b/src/pages/demo.rs index 64800d3..a3b8273 100644 --- a/src/pages/demo.rs +++ b/src/pages/demo.rs @@ -10,6 +10,7 @@ use std::borrow::Cow; #[asset(source = "style/pages/class-icons.css", url = "/class-icons.css")] pub struct ClassIconsStyle; +#[derive(Debug)] pub struct DemoPage { pub demo: Demo, } diff --git a/src/pages/edit.rs b/src/pages/edit.rs index 4f396bd..f67b783 100644 --- a/src/pages/edit.rs +++ b/src/pages/edit.rs @@ -3,6 +3,7 @@ use demostf_build::Asset; use maud::{html, Markup}; use std::borrow::Cow; +#[derive(Debug)] pub struct EditorPage; #[derive(Asset)] diff --git a/src/pages/index.rs b/src/pages/index.rs index 78b6ba8..8c69d63 100644 --- a/src/pages/index.rs +++ b/src/pages/index.rs @@ -1,13 +1,15 @@ use crate::data::demo::ListDemo; +use crate::data::maps::MapList; use crate::fragments::demo_list::DemoList; use crate::pages::Page; use demostf_build::Asset; use maud::{html, Markup, Render}; use std::borrow::Cow; +#[derive(Debug)] pub struct Index<'a> { pub demos: &'a [ListDemo], - pub maps: &'a [String], + pub maps: &'a MapList, pub api: &'a str, } @@ -16,9 +18,6 @@ pub struct Index<'a> { pub struct DemoListScript; impl<'a> Index<'a> { - fn map_list(&self) -> impl Render + 'a { - MapList(self.maps) - } fn demo_list(&self) -> impl Render + 'a { DemoList { demos: self.demos } } @@ -33,7 +32,7 @@ impl Page for Index<'_> { let script = DemoListScript::url(); html! { h1 { "Demos" } - #filter-bar data-maps = (self.map_list()) data-api-base = (self.api) {} + #filter-bar data-maps = (self.maps) data-api-base = (self.api) {} table.demolist { thead { tr { @@ -53,18 +52,3 @@ impl Page for Index<'_> { } } } - -pub struct MapList<'a>(pub &'a [String]); - -impl Render for MapList<'_> { - fn render_to(&self, buffer: &mut String) { - let mut first = true; - for map in self.0 { - if !first { - buffer.push(','); - } - buffer.push_str(map); - first = false; - } - } -} diff --git a/src/pages/mod.rs b/src/pages/mod.rs index 3e733da..3380104 100644 --- a/src/pages/mod.rs +++ b/src/pages/mod.rs @@ -13,6 +13,7 @@ use crate::session::SessionData; use demostf_build::Asset; use maud::{html, Markup, DOCTYPE}; use std::borrow::Cow; +use std::fmt::Debug; use tracing::instrument; pub trait Page { @@ -25,7 +26,7 @@ pub trait Page { pub struct GlobalStyle; #[instrument] -pub fn render(page: T, session: SessionData) -> Markup { +pub fn render(page: T, session: SessionData) -> Markup { let style_url = GlobalStyle::url(); html! { (DOCTYPE) diff --git a/src/pages/plugin_section.rs b/src/pages/plugin_section.rs index 9c77a41..d0feba3 100644 --- a/src/pages/plugin_section.rs +++ b/src/pages/plugin_section.rs @@ -1,7 +1,9 @@ +use crate::data::user::Token; use maud::{html, Markup, Render}; +#[derive(Debug)] pub struct PluginSection<'a> { - pub key: Option<&'a str>, + pub key: Option<&'a Token>, } impl Render for PluginSection<'_> { diff --git a/src/pages/profile.rs b/src/pages/profile.rs index dee151c..11a5c67 100644 --- a/src/pages/profile.rs +++ b/src/pages/profile.rs @@ -1,23 +1,22 @@ use crate::data::demo::ListDemo; +use crate::data::maps::MapList; use crate::data::user::User; use crate::fragments::demo_list::DemoList; -use crate::pages::index::{DemoListScript, MapList}; +use crate::pages::index::DemoListScript; use crate::pages::Page; use demostf_build::Asset; use maud::{html, Markup, Render}; use std::borrow::Cow; +#[derive(Debug)] pub struct Profile<'a> { pub user: User, pub demos: &'a [ListDemo], - pub maps: &'a [String], + pub maps: &'a MapList, pub api: &'a str, } impl<'a> Profile<'a> { - fn map_list(&self) -> impl Render + 'a { - MapList(self.maps) - } fn demo_list(&self) -> impl Render + 'a { DemoList { demos: self.demos } } @@ -35,7 +34,7 @@ impl Page for Profile<'_> { "Demos with " (self.user.name) } - #filter-bar data-maps = (self.map_list()) data-api-base = (self.api) {} + #filter-bar data-maps = (self.maps) data-api-base = (self.api) {} table.demolist { thead { tr { diff --git a/src/pages/upload.rs b/src/pages/upload.rs index 0ed74e2..4aaefba 100644 --- a/src/pages/upload.rs +++ b/src/pages/upload.rs @@ -1,11 +1,13 @@ +use crate::data::user::Token; use crate::pages::plugin_section::PluginSection; use crate::pages::Page; use demostf_build::Asset; use maud::{html, Markup}; use std::borrow::Cow; +#[derive(Debug)] pub struct UploadPage<'a> { - pub key: &'a str, + pub key: &'a Token, pub api: &'a str, } diff --git a/src/pages/uploads.rs b/src/pages/uploads.rs index f659e52..57a70b9 100644 --- a/src/pages/uploads.rs +++ b/src/pages/uploads.rs @@ -1,23 +1,22 @@ use crate::data::demo::ListDemo; +use crate::data::maps::MapList; use crate::data::user::User; use crate::fragments::demo_list::DemoList; -use crate::pages::index::{DemoListScript, MapList}; +use crate::pages::index::DemoListScript; use crate::pages::Page; use demostf_build::Asset; use maud::{html, Markup, Render}; use std::borrow::Cow; +#[derive(Debug)] pub struct Uploads<'a> { pub user: User, pub demos: &'a [ListDemo], - pub maps: &'a [String], + pub maps: &'a MapList, pub api: &'a str, } impl<'a> Uploads<'a> { - fn map_list(&self) -> impl Render + 'a { - MapList(self.maps) - } fn demo_list(&self) -> impl Render + 'a { DemoList { demos: self.demos } } @@ -35,7 +34,7 @@ impl Page for Uploads<'_> { "Uploads by " (self.user.name) } - #filter-bar data-maps = (self.map_list()) data-api-base = (self.api) {} + #filter-bar data-maps = (self.maps) data-api-base = (self.api) {} table.demolist { thead { tr { diff --git a/src/pages/viewer.rs b/src/pages/viewer.rs index e612ba0..3651ec2 100644 --- a/src/pages/viewer.rs +++ b/src/pages/viewer.rs @@ -4,6 +4,7 @@ use demostf_build::Asset; use maud::{html, Markup}; use std::borrow::Cow; +#[derive(Debug)] pub struct ViewerPage<'a> { pub demo: Option, pub maps: &'a str, diff --git a/src/session.rs b/src/session.rs index 2ef5c26..c7bde6b 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1,5 +1,5 @@ use crate::data::steam_id::SteamId; -use crate::data::user::User; +use crate::data::user::{Token, User}; use crate::{App, Result}; use async_session::SessionStore as _; use axum::extract::{FromRef, FromRequestParts}; @@ -18,9 +18,9 @@ pub enum SessionData { } impl SessionData { - pub fn token(&self) -> Option { + pub fn token(&self) -> Option { match self { - SessionData::Authenticated(user) => Some(user.token.clone()), + SessionData::Authenticated(user) => Some(Token::new(user.token.clone())), SessionData::UnAuthenticated => None, } }