feat: add logging
This commit is contained in:
parent
6e31804f19
commit
bc2b74c401
@ -11,11 +11,9 @@ shuttle-axum = "0.45.0"
|
||||
shuttle-runtime = "0.45.0"
|
||||
tokio = "1.28.2"
|
||||
tower-http = { version = "0.5.2", features = ["fs", "cors"] }
|
||||
tracing = "0.1.40"
|
||||
yew = "0.21.0"
|
||||
|
||||
|
||||
[workspace]
|
||||
members = [
|
||||
".",
|
||||
"frontend",
|
||||
]
|
||||
members = [".", "frontend"]
|
||||
|
32
src/main.rs
32
src/main.rs
@ -1,21 +1,29 @@
|
||||
use axum::{routing::get, Router};
|
||||
use rand::seq::SliceRandom;
|
||||
use tower_http::services::ServeDir;
|
||||
use tower_http::cors::{Any,CorsLayer};
|
||||
use axum::{
|
||||
body::Body,
|
||||
http::Request,
|
||||
middleware::{self, Next},
|
||||
response::Response,
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
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]
|
||||
async fn main() -> shuttle_axum::ShuttleAxum {
|
||||
|
||||
let cors = CorsLayer::new()
|
||||
.allow_origin(Any) // Allow all origins; adjust as necessary
|
||||
.allow_methods(vec![Method::GET]) // Specify allowed methods
|
||||
.allow_headers(Any);
|
||||
|
||||
let router = Router::new()
|
||||
// .route("/", get(hello_world))
|
||||
.nest_service("/", ServeDir::new("frontend/dist"))
|
||||
.route("/word", get(word))
|
||||
.layer(middleware::from_fn(log_ip))
|
||||
.layer(cors);
|
||||
|
||||
Ok(router.into())
|
||||
@ -26,6 +34,18 @@ async fn word() -> 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] = &[
|
||||
"aalen", "Aales", "aalst", "aalte", "Aarau", "Aaron", "Aasee", "Aases", "Abart", "abbat",
|
||||
"Abbau", "Abbey", "abbog", "Abdul", "Abeba", "Abels", "Abend", "abgab", "Abgas", "abhob",
|
||||
|
Loading…
x
Reference in New Issue
Block a user