From 7f60409ed9c193ba6cea957b82afd836d9d6e90d Mon Sep 17 00:00:00 2001 From: Fuwn Date: Mon, 25 Apr 2022 01:16:37 -0700 Subject: refactor(modules): move complex router setup to modules --- src/modules/mod.rs | 1 + src/modules/router.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/modules/router.rs (limited to 'src/modules') diff --git a/src/modules/mod.rs b/src/modules/mod.rs index fd856ff..b139159 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -19,6 +19,7 @@ pub mod multi_blog; pub mod random; pub mod remarks; +pub mod router; pub mod search; pub mod sitemap; pub mod r#static; diff --git a/src/modules/router.rs b/src/modules/router.rs new file mode 100644 index 0000000..2445365 --- /dev/null +++ b/src/modules/router.rs @@ -0,0 +1,52 @@ +// This file is part of Locus . +// Copyright (C) 2022-2022 Fuwn +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +use crate::DATABASE; + +pub fn module(router: &mut windmark::Router) { + router.set_pre_route_callback(Box::new(|stream, url, _| { + info!( + "accepted connection from {} to {}", + stream.peer_addr().unwrap().ip(), + url.to_string(), + ); + + let url_path = if url.path().is_empty() { + "/" + } else { + url.path() + }; + + let previous_database = (*DATABASE.lock().unwrap()).get::(url_path); + + match previous_database { + None => { + (*DATABASE.lock().unwrap()) + .set::(url_path, &0) + .unwrap(); + } + Some(_) => {} + } + + let new_database = (*DATABASE.lock().unwrap()).get::(url_path); + + (*DATABASE.lock().unwrap()) + .set(url_path, &(new_database.unwrap() + 1)) + .unwrap(); + })); +} -- cgit v1.2.3