feat: web stuff

This commit is contained in:
boring_nick 2022-08-13 11:04:17 +03:00
parent bf49fedf81
commit b8febc8aff
7 changed files with 68 additions and 17 deletions

2
Cargo.lock generated
View File

@ -1097,6 +1097,7 @@ dependencies = [
"serde_json",
"thiserror",
"tokio",
"tower-http",
"tracing",
"tracing-subscriber",
"twitch-irc",
@ -1506,6 +1507,7 @@ dependencies = [
"tower",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]

View File

@ -15,6 +15,7 @@ serde = { version = "1.0.137", features = ["derive"] }
serde_json = { version = "1.0.81", features = ["preserve_order"] }
thiserror = "1.0.31"
tokio = { version = "1.19.2", features = ["full"] }
tower-http = { version = "0.3.4", features = ["trace"] }
tracing = "0.1.35"
tracing-subscriber = { version = "0.3.11", features = ["env-filter"] }
twitch-irc = "4.0.0"

View File

@ -17,7 +17,7 @@ use logs::{offsets::Offset, Logs};
use std::{path::PathBuf, sync::Arc};
use tokio::{fs::File, io::BufReader, try_join};
use tracing::info;
use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter};
use tracing_subscriber::EnvFilter;
use twitch_api2::{
twitch_oauth2::{AppAccessToken, Scope},
HelixClient,
@ -30,7 +30,7 @@ async fn main() -> anyhow::Result<()> {
.with_env_filter(
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
)
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
// .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
.init();
let config = Config::load().await?;

View File

@ -1,7 +1,7 @@
use super::schema::{
Channel, ChannelIdType, ChannelLogsParams, ChannelsList, UserIdType, UserLogsParams,
};
use crate::{app::App, config::Config, error::Error};
use crate::{app::App, config::Config, error::Error, logs::schema::ChannelLogDate};
use axum::{extract::Path, Extension, Json};
use std::sync::Arc;
@ -24,16 +24,12 @@ pub async fn get_channels(
pub async fn get_channel_logs(
app: Extension<App<'_>>,
Path(ChannelLogsParams {
channel_id_type,
channel,
channel_log_date,
}): Path<ChannelLogsParams>,
Path(channel_log_params): Path<ChannelLogsParams>,
) -> Result<String, Error> {
let channel_id = match channel_id_type {
let channel_id = match channel_log_params.channel_id_type {
ChannelIdType::Name => {
let (id, _) = app
.get_users(vec![], vec![channel])
.get_users(vec![], vec![channel_log_params.channel.clone()])
.await?
.into_iter()
.next()
@ -41,12 +37,14 @@ pub async fn get_channel_logs(
id
}
ChannelIdType::Id => channel,
ChannelIdType::Id => channel_log_params.channel.clone(),
};
let log_date = ChannelLogDate::try_from(&channel_log_params)?;
Ok(app
.logs
.read_channel(&channel_id, channel_log_date)
.read_channel(&channel_id, log_date)
.await?
.join("\n"))
}

View File

@ -1,5 +1,6 @@
mod handlers;
mod schema;
mod trace_layer;
use crate::{app::App, config::Config};
use axum::{routing::get, Extension, Router};
@ -8,6 +9,7 @@ use std::{
str::FromStr,
sync::Arc,
};
use tower_http::trace::TraceLayer;
use tracing::info;
pub async fn run(config: Config, app: App<'static>) {
@ -24,7 +26,12 @@ pub async fn run(config: Config, app: App<'static>) {
// get(handlers::get_user_logs),
// )
.layer(Extension(app))
.layer(Extension(Arc::new(config)));
.layer(Extension(Arc::new(config)))
.layer(
TraceLayer::new_for_http()
.make_span_with(trace_layer::make_span_with)
.on_response(trace_layer::on_response),
);
info!("Listening on {listen_address}");

View File

@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
use crate::logs::schema::ChannelLogDate;
use serde::{Deserialize, Serialize};
use std::num::ParseIntError;
#[derive(Serialize)]
pub struct ChannelsList {
@ -44,6 +44,19 @@ pub struct UserLogsParams {
pub struct ChannelLogsParams {
pub channel_id_type: ChannelIdType,
pub channel: String,
#[serde(flatten)]
pub channel_log_date: ChannelLogDate,
pub year: String,
pub month: String,
pub day: String,
}
impl TryFrom<&ChannelLogsParams> for ChannelLogDate {
type Error = ParseIntError;
fn try_from(params: &ChannelLogsParams) -> Result<Self, Self::Error> {
Ok(Self {
year: params.year.parse()?,
month: params.month.parse()?,
day: params.day.parse()?,
})
}
}

30
src/web/trace_layer.rs Normal file
View File

@ -0,0 +1,30 @@
use axum::{
body::{Body, BoxBody},
http::Request,
response::Response,
};
use std::time::Duration;
use tracing::{info, info_span, Span};
pub fn make_span_with(request: &Request<Body>) -> Span {
let method = request.method().to_string();
let url = request.uri().to_string();
info!("HTTP request {method} at {url}");
info_span!(
"http-request",
"http.method" = method.as_str(),
"http.uri" = url.as_str()
)
}
pub fn on_response(response: &Response<BoxBody>, latency: Duration, span: &Span) {
let status = response.status();
let ms = latency.as_millis();
span.record("http.status", &status.as_str());
span.record("http.latency", &ms.to_string().as_str());
info!("HTTP response {status} processed in {ms}ms");
}