feat: add logging

This commit is contained in:
itsscb 2024-08-25 20:07:57 +02:00
parent 6e31804f19
commit bc2b74c401
2 changed files with 28 additions and 10 deletions

View File

@ -11,11 +11,9 @@ shuttle-axum = "0.45.0"
shuttle-runtime = "0.45.0" shuttle-runtime = "0.45.0"
tokio = "1.28.2" tokio = "1.28.2"
tower-http = { version = "0.5.2", features = ["fs", "cors"] } tower-http = { version = "0.5.2", features = ["fs", "cors"] }
tracing = "0.1.40"
yew = "0.21.0" yew = "0.21.0"
[workspace] [workspace]
members = [ members = [".", "frontend"]
".",
"frontend",
]

View File

@ -1,21 +1,29 @@
use axum::{routing::get, Router}; use axum::{
use rand::seq::SliceRandom; body::Body,
use tower_http::services::ServeDir; http::Request,
use tower_http::cors::{Any,CorsLayer}; middleware::{self, Next},
response::Response,
routing::get,
Router,
};
use http::Method; use http::Method;
use rand::seq::SliceRandom;
use std::net::SocketAddr;
use tower_http::cors::{Any, CorsLayer};
use tower_http::services::ServeDir;
use tracing::info;
#[shuttle_runtime::main] #[shuttle_runtime::main]
async fn main() -> shuttle_axum::ShuttleAxum { async fn main() -> shuttle_axum::ShuttleAxum {
let cors = CorsLayer::new() let cors = CorsLayer::new()
.allow_origin(Any) // Allow all origins; adjust as necessary .allow_origin(Any) // Allow all origins; adjust as necessary
.allow_methods(vec![Method::GET]) // Specify allowed methods .allow_methods(vec![Method::GET]) // Specify allowed methods
.allow_headers(Any); .allow_headers(Any);
let router = Router::new() let router = Router::new()
// .route("/", get(hello_world))
.nest_service("/", ServeDir::new("frontend/dist")) .nest_service("/", ServeDir::new("frontend/dist"))
.route("/word", get(word)) .route("/word", get(word))
.layer(middleware::from_fn(log_ip))
.layer(cors); .layer(cors);
Ok(router.into()) Ok(router.into())
@ -26,6 +34,18 @@ async fn word() -> String {
WORDS.choose(&mut rng).unwrap().to_string() WORDS.choose(&mut rng).unwrap().to_string()
} }
async fn log_ip(req: Request<Body>, next: Next) -> Response {
if let Some(addr) = req.extensions().get::<SocketAddr>() {
info!(
PATH = req.uri().path().to_string(),
IP = addr.ip().to_string()
);
} else {
info!(PATH = req.uri().path().to_string());
}
next.run(req).await
}
static WORDS: &[&str; 5921] = &[ static WORDS: &[&str; 5921] = &[
"aalen", "Aales", "aalst", "aalte", "Aarau", "Aaron", "Aasee", "Aases", "Abart", "abbat", "aalen", "Aales", "aalst", "aalte", "Aarau", "Aaron", "Aasee", "Aases", "Abart", "abbat",
"Abbau", "Abbey", "abbog", "Abdul", "Abeba", "Abels", "Abend", "abgab", "Abgas", "abhob", "Abbau", "Abbey", "abbog", "Abdul", "Abeba", "Abels", "Abend", "abgab", "Abgas", "abhob",