mirror of
https://github.com/JonasunderscoreJones/jonas_jones-api.git
synced 2025-10-23 00:59:18 +02:00
added request logging
This commit is contained in:
parent
41fa898a67
commit
cdcf27b0fa
5 changed files with 223 additions and 13 deletions
14
src/iplookup.rs
Normal file
14
src/iplookup.rs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
use ip2location::{DB, Record};
|
||||
|
||||
pub fn ip_lookup(ip: &str) -> String {
|
||||
let database_path = "resources/IP2LOCATION-LITE-DB5.IPV6.BIN/IP2LOCATION-LITE-DB5.IPV6.BIN";
|
||||
|
||||
let mut db = DB::from_file(database_path).unwrap();
|
||||
|
||||
let geo_info = db.ip_lookup(ip.parse().unwrap()).unwrap();
|
||||
|
||||
let record = if let Record::LocationDb(rec) = geo_info {
|
||||
Some(rec)
|
||||
} else { None };
|
||||
return record.unwrap().country.unwrap().short_name.to_string();
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ pub mod logger;
|
|||
pub mod tools;
|
||||
pub mod server;
|
||||
pub mod error_responses;
|
||||
pub mod iplookup;
|
||||
|
||||
pub use logger::Logger;
|
||||
pub use tools::parse_ip;
|
||||
|
|
|
|||
|
|
@ -1,13 +1,16 @@
|
|||
use std::convert::Infallible;
|
||||
use std::net::SocketAddr;
|
||||
use std::env;
|
||||
|
||||
use reqwest::StatusCode;
|
||||
use lastfm::reqwest::StatusCode;
|
||||
use warp::filters::path::FullPath;
|
||||
use warp::Filter;
|
||||
use warp::reply::Reply;
|
||||
|
||||
use crate::error_responses::{ErrorMessage, InternalServerError, BadRequestError, NotFoundError, NotImplementedError};
|
||||
use crate::v1::get_v1_routes;
|
||||
use crate::{Logger, parse_ip};
|
||||
use crate::iplookup::ip_lookup;
|
||||
|
||||
|
||||
pub async fn serve() {
|
||||
|
|
@ -24,9 +27,23 @@ pub async fn serve() {
|
|||
let status = warp::path("status")
|
||||
.map(|| warp::reply());
|
||||
|
||||
// Middleware filter to log request details
|
||||
let log_request = warp::any()
|
||||
.and(warp::method())
|
||||
.and(warp::path::full())
|
||||
.and(warp::addr::remote())
|
||||
.and(warp::header::optional::<String>("x-forwarded-for"))
|
||||
.map(|method, path: FullPath, addr: Option<SocketAddr>, fwd_for: Option<String>| {
|
||||
let client_ip = fwd_for.unwrap_or_else(|| addr.map(|a| a.ip().to_string()).unwrap_or_else(|| String::from("unknown")));
|
||||
let path_str = path.as_str();
|
||||
|
||||
Logger::info(&format!(" {} {} from {}", method, path_str, ip_lookup(&client_ip)));
|
||||
});
|
||||
|
||||
// GET (any) => reply with return from handle_path
|
||||
let routes = favicon.or(status.or(get_v1_routes())
|
||||
.recover(handle_rejection));
|
||||
let routes = log_request
|
||||
.clone().untuple_one().and(favicon.or(status.or(get_v1_routes())
|
||||
.recover(handle_rejection)));
|
||||
|
||||
|
||||
async fn handle_rejection(err: warp::Rejection) -> Result<impl Reply, Infallible> {
|
||||
|
|
@ -47,7 +64,7 @@ pub async fn serve() {
|
|||
message: message.into(),
|
||||
});
|
||||
|
||||
Ok(warp::reply::with_status(json, code))
|
||||
Ok(warp::reply::with_status(json, StatusCode::from_u16(code.as_u16()).unwrap()))
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue