style: format

This commit is contained in:
Tien Do Nam
2025-01-09 21:56:45 +01:00
parent a16ebb7214
commit 8f209119fe
7 changed files with 57 additions and 34 deletions

View File

@@ -4,7 +4,7 @@ use axum::response::{IntoResponse, Response};
// https://github.com/tokio-rs/axum/blob/main/examples/oauth/src/main.rs
#[derive(Debug)]
pub struct AppError{
pub struct AppError {
status: StatusCode,
message: Option<String>,
error: Option<anyhow::Error>,
@@ -23,13 +23,17 @@ impl AppError {
impl IntoResponse for AppError {
fn into_response(self) -> Response {
if self.status >= StatusCode::INTERNAL_SERVER_ERROR {
tracing::error!("{:?}; {:?}", match &self.message {
Some(message) => message,
None => "(no message)",
}, match &self.error {
Some(error) => error.to_string(),
None => "(no error)".to_string(),
});
tracing::error!(
"{:?}; {:?}",
match &self.message {
Some(message) => message,
None => "(no message)",
},
match &self.error {
Some(error) => error.to_string(),
None => "(no error)".to_string(),
}
);
}
// return status code with message

View File

@@ -1,4 +1,4 @@
pub(crate) mod state;
pub(crate) mod init;
pub(crate) mod error;
pub(crate) mod init;
mod scheduler;
pub(crate) mod state;

View File

@@ -1,19 +1,22 @@
use tokio_cron_scheduler::{Job, JobScheduler};
use crate::config::state::{ IpRequestCountMap};
use crate::config::state::IpRequestCountMap;
pub async fn configure_scheduling(ip_request_count_map: IpRequestCountMap) -> Result<(), Box<dyn std::error::Error>>{
pub async fn configure_scheduling(
ip_request_count_map: IpRequestCountMap,
) -> Result<(), Box<dyn std::error::Error>> {
let scheduler = JobScheduler::new().await?;
scheduler.add(Job::new_async("0 0 0 * * *", move |_uuid, _l| {
Box::pin({
let value = ip_request_count_map.clone();
async move {
value.lock().await.clear();
}
})
})?).await?;
scheduler
.add(Job::new_async("0 0 0 * * *", move |_uuid, _l| {
Box::pin({
let value = ip_request_count_map.clone();
async move {
value.lock().await.clear();
}
})
})?)
.await?;
scheduler.start().await?;

View File

@@ -1,2 +1,2 @@
pub(crate) mod ws_controller;
pub(crate) mod rest_controller;
pub(crate) mod ws_controller;

View File

@@ -3,11 +3,11 @@ use crate::config::state::{AppState, IpRequestCountMap, TxMap};
use crate::controller::ws_controller::{PeerInfo, WsMessageType, WsServerMessage};
use crate::util;
use axum::extract::{ConnectInfo, State};
use axum::http::StatusCode;
use axum::Json;
use serde::Deserialize;
use std::net::SocketAddr;
use std::sync::LazyLock;
use axum::http::StatusCode;
use tokio::sync::mpsc;
use uuid::Uuid;
@@ -52,7 +52,8 @@ pub async fn send_offer(
sdp: Some(payload.sdp),
},
&state.tx_map,
).await;
)
.await;
Ok(())
}
@@ -77,7 +78,8 @@ pub async fn send_answer(
sdp: Some(payload.sdp),
},
&state.tx_map,
).await;
)
.await;
Ok(())
}

View File

@@ -1,7 +1,7 @@
use std::net::SocketAddr;
use axum::Router;
use axum::routing::{get, post};
use crate::controller::{rest_controller, ws_controller};
use axum::routing::{get, post};
use axum::Router;
use std::net::SocketAddr;
mod config;
mod controller;
@@ -21,7 +21,9 @@ async fn main() {
let server_port = std::env::var("SERVER_PORT").unwrap_or_else(|_| "3000".to_string());
let bind_address = format!("{server_ip}:{server_port}");
let listener = tokio::net::TcpListener::bind(bind_address.clone()).await.unwrap();
let listener = tokio::net::TcpListener::bind(bind_address.clone())
.await
.unwrap();
tracing::info!("Listening on http://{bind_address}");
axum::serve(listener, app).await.unwrap();
}

View File

@@ -5,11 +5,14 @@ pub(crate) fn get_ip_group(ip: IpAddr) -> String {
IpAddr::V4(ip) => {
// IPv4: Each IP address is already a group.
ip.to_string()
},
}
IpAddr::V6(ip) => {
// IPv6: We treat /64 as a group.
let segments = ip.segments();
format!("{:x}:{:x}:{:x}:{:x}", segments[0], segments[1], segments[2], segments[3])
format!(
"{:x}:{:x}:{:x}:{:x}",
segments[0], segments[1], segments[2], segments[3]
)
}
}
}
@@ -21,8 +24,17 @@ mod tests {
#[test]
fn test_get_ip_group() {
assert_eq!(get_ip_group(IpAddr::from_str("1.2.3.4").unwrap()), "1.2.3.4");
assert_eq!(get_ip_group(IpAddr::from_str("1:2:3:4:5:6:7:8").unwrap()), "1:2:3:4");
assert_eq!(get_ip_group(IpAddr::from_str("a:b:c:d:e:f:0:1").unwrap()), "a:b:c:d");
assert_eq!(
get_ip_group(IpAddr::from_str("1.2.3.4").unwrap()),
"1.2.3.4"
);
assert_eq!(
get_ip_group(IpAddr::from_str("1:2:3:4:5:6:7:8").unwrap()),
"1:2:3:4"
);
assert_eq!(
get_ip_group(IpAddr::from_str("a:b:c:d:e:f:0:1").unwrap()),
"a:b:c:d"
);
}
}