fix ip matching

This commit is contained in:
lelgenio 2024-10-05 14:06:56 -03:00
parent 55e9cf1f9c
commit acd8778864

View file

@ -1,4 +1,10 @@
use std::{collections::HashSet, future::Future, net::SocketAddr, pin::Pin, sync::Arc};
use std::{
collections::HashSet,
future::Future,
net::{IpAddr, SocketAddr},
pin::Pin,
sync::Arc,
};
use anyhow::{Context, Result};
use askama::Template;
@ -18,7 +24,7 @@ async fn routes() -> Router {
#[derive(Clone)]
pub struct AppState {
view_count: Arc<RwLock<u32>>,
seen_ips: Arc<RwLock<HashSet<SocketAddr>>>,
seen_ips: Arc<RwLock<HashSet<IpAddr>>>,
}
impl AppState {
@ -40,7 +46,7 @@ impl AppState {
}
}
async fn increment(&self, ip: SocketAddr) -> u32 {
async fn increment(&self, ip: IpAddr) -> u32 {
if self.seen_ips.read().await.contains(&ip) {
let view_count = self.view_count.read().await;
return *view_count;
@ -99,8 +105,9 @@ async fn write_count(new_count: u32) -> anyhow::Result<()> {
#[axum::debug_handler]
pub async fn home(
State(state): State<AppState>,
ConnectInfo(ip): ConnectInfo<SocketAddr>,
ConnectInfo(addr): ConnectInfo<SocketAddr>,
) -> HomeTemplate {
let ip = addr.ip();
tracing::info!(ip = ip.to_string());
let view_count = state.increment(ip).await;