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 | |
| 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')
| -rw-r--r-- | src/main.rs | 31 | ||||
| -rw-r--r-- | src/modules/mod.rs | 1 | ||||
| -rw-r--r-- | src/modules/router.rs | 52 |
3 files changed, 54 insertions, 30 deletions
diff --git a/src/main.rs b/src/main.rs index a1beda9..ef66cf5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,36 +103,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { router.set_private_key_file(".locus/locus_private.pem"); router.set_certificate_file(".locus/locus_public.pem"); - 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(); - })); router.set_error_handler(Box::new(|_| { Response::NotFound(ERROR_HANDLER_RESPONSE.into()) })); @@ -152,6 +122,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { router.attach_stateless(modules::multi_blog::module); router.attach_stateless(modules::random::module); router.attach_stateless(modules::r#static::module); + router.attach_stateless(modules::router::module); }); std::thread::spawn(search::index); 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(); + })); +} |