diff options
| author | Fuwn <[email protected]> | 2022-04-14 23:13:20 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-04-14 23:13:20 -0700 |
| commit | 30792860e954ac24c07102f879ad98a2d034954c (patch) | |
| tree | 911e290cfb0883c13bc466f196f024b0a856efc9 /src | |
| parent | fix: add remarks file (diff) | |
| download | locus-30792860e954ac24c07102f879ad98a2d034954c.tar.xz locus-30792860e954ac24c07102f879ad98a2d034954c.zip | |
refactor: move route related functions
Diffstat (limited to 'src')
| -rw-r--r-- | src/macros.rs | 2 | ||||
| -rw-r--r-- | src/main.rs | 22 | ||||
| -rw-r--r-- | src/route.rs | 38 |
3 files changed, 41 insertions, 21 deletions
diff --git a/src/macros.rs b/src/macros.rs index ae0fb4e..f65a901 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -22,7 +22,7 @@ macro_rules! success { Response::Success( Main { body: &$body, - hits: &crate::hits_from_route($context.url.path()), + hits: &crate::route::hits_from($context.url.path()), quote: QUOTES.choose(&mut rand::thread_rng()).unwrap(), commit: &format!("/tree/{}", env!("VERGEN_GIT_SHA")), mini_commit: env!("VERGEN_GIT_SHA").get(0..5).unwrap_or("UNKNOWN"), diff --git a/src/main.rs b/src/main.rs index 95ad96a..15da848 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,6 +32,7 @@ mod constants; mod macros; mod modules; +mod route; #[macro_use] extern crate log; @@ -41,6 +42,7 @@ use std::{collections::HashMap, lazy::SyncLazy, sync::Mutex}; use constants::QUOTES; use pickledb::PickleDb; use rand::seq::SliceRandom; +use route::track_mount; use tokio::time::Instant; use windmark::{Response, Router}; use yarte::Template; @@ -74,26 +76,6 @@ struct Main<'a> { mini_commit: &'a str, } -fn hits_from_route(route: &str) -> i32 { - if let Ok(database) = DATABASE.lock() { - (*database) - .get::<i32>(if route.is_empty() { "/" } else { route }) - .unwrap() - } else { - 0 - } -} - -fn track_mount( - router: &mut Router, - route: &str, - description: &str, - handler: windmark::handler::RouteResponse, -) { - (*ROUTES.lock().unwrap()).insert(route.to_string(), description.to_string()); - router.mount(route, handler); -} - #[windmark::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { std::env::set_var("RUST_LOG", "windmark,locus=trace"); diff --git a/src/route.rs b/src/route.rs new file mode 100644 index 0000000..66d09cc --- /dev/null +++ b/src/route.rs @@ -0,0 +1,38 @@ +// 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 + +pub fn hits_from(route: &str) -> i32 { + if let Ok(database) = crate::DATABASE.lock() { + (*database) + .get::<i32>(if route.is_empty() { "/" } else { route }) + .unwrap() + } else { + 0 + } +} + +pub fn track_mount( + router: &mut windmark::Router, + route: &str, + description: &str, + handler: windmark::handler::RouteResponse, +) { + (*crate::ROUTES.lock().unwrap()) + .insert(route.to_string(), description.to_string()); + router.mount(route, handler); +} |