diff options
| author | Fuwn <[email protected]> | 2022-04-25 01:16:37 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-04-25 01:16:37 -0700 |
| commit | 7f60409ed9c193ba6cea957b82afd836d9d6e90d (patch) | |
| tree | 6ddd3c2aed18bb2fc991e0e3c964772cf7593167 /src/modules | |
| parent | refactor(router): move error handler to constant (diff) | |
| download | locus-7f60409ed9c193ba6cea957b82afd836d9d6e90d.tar.xz locus-7f60409ed9c193ba6cea957b82afd836d9d6e90d.zip | |
refactor(modules): move complex router setup to modules
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/mod.rs | 1 | ||||
| -rw-r--r-- | src/modules/router.rs | 52 |
2 files changed, 53 insertions, 0 deletions
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 <https://github.com/gemrest/locus>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// 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 <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// 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::<i32>(url_path); + + match previous_database { + None => { + (*DATABASE.lock().unwrap()) + .set::<i32>(url_path, &0) + .unwrap(); + } + Some(_) => {} + } + + let new_database = (*DATABASE.lock().unwrap()).get::<i32>(url_path); + + (*DATABASE.lock().unwrap()) + .set(url_path, &(new_database.unwrap() + 1)) + .unwrap(); + })); +} |