diff options
| author | Fuwn <[email protected]> | 2022-04-19 23:41:09 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-04-19 23:41:09 -0700 |
| commit | a7c661cd6142c8bc6f5783948c173ad0514827df (patch) | |
| tree | e217f4f56a5e6e2eb52e8c6c1957f86289a59370 /src | |
| parent | refactor(modules): move remarks to modules module (diff) | |
| download | locus-a7c661cd6142c8bc6f5783948c173ad0514827df.tar.xz locus-a7c661cd6142c8bc6f5783948c173ad0514827df.zip | |
refactor(modules): move uptime to modules module
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 32 | ||||
| -rw-r--r-- | src/modules/mod.rs | 1 | ||||
| -rw-r--r-- | src/modules/uptime.rs | 46 |
3 files changed, 49 insertions, 30 deletions
diff --git a/src/main.rs b/src/main.rs index 7eb6a31..a0c3f3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -83,7 +83,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { let mut time_mount = Instant::now(); let mut router = Router::new(); - let uptime = Instant::now(); router.set_private_key_file(".locus/locus_private.pem"); router.set_certificate_file(".locus/locus_public.pem"); @@ -133,41 +132,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { ); time_mount = Instant::now(); - track_mount( - &mut router, - "/uptime", - "The uptime of Locus (A.K.A., The Locus Epoch). (\\?[s|ms|mu|ns]?)?", - Box::new(move |context| { - Response::Success(context.url.query().map_or_else( - || uptime.elapsed().as_nanos().to_string(), - |query| { - match query { - "secs" | "seconds" | "s" => uptime.elapsed().as_secs().to_string(), - "milli" | "milliseconds" | "ms" => - uptime.elapsed().as_millis().to_string(), - "micro" | "microseconds" | "mu" => - uptime.elapsed().as_micros().to_string(), - _ => uptime.elapsed().as_nanos().to_string(), - } - }, - )) - }), - ); - + router.attach_stateless(modules::uptime::module); router.attach_stateless(modules::sitemap::module); router.attach_stateless(modules::search::module); router.attach_stateless(modules::remarks::module); - - info!( - "preliminary mounts took {}ms", - time_mount.elapsed().as_nanos() as f64 / 1_000_000.0 - ); - time_mount = Instant::now(); - router.attach_stateless(modules::multi_blog::module); info!( - "blog mounts took {}ms", + "module mounts took {}ms", time_mount.elapsed().as_nanos() as f64 / 1_000_000.0 ); time_mount = Instant::now(); diff --git a/src/modules/mod.rs b/src/modules/mod.rs index a41bf2b..c5d4aaa 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -20,3 +20,4 @@ pub mod multi_blog; pub mod remarks; pub mod search; pub mod sitemap; +pub mod uptime; diff --git a/src/modules/uptime.rs b/src/modules/uptime.rs new file mode 100644 index 0000000..0725933 --- /dev/null +++ b/src/modules/uptime.rs @@ -0,0 +1,46 @@ +// 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 std::lazy::SyncLazy; + +use tokio::time::Instant; + +static UPTIME: SyncLazy<Instant> = SyncLazy::new(Instant::now); + +pub fn module(router: &mut windmark::Router) { + crate::route::track_mount( + router, + "/uptime", + "The uptime of Locus (A.K.A., The Locus Epoch). (\\?[s|ms|mu|ns]?)?", + Box::new(move |context| { + windmark::Response::Success(context.url.query().map_or_else( + || UPTIME.elapsed().as_nanos().to_string(), + |query| { + match query { + "secs" | "seconds" | "s" => UPTIME.elapsed().as_secs().to_string(), + "milli" | "milliseconds" | "ms" => + UPTIME.elapsed().as_millis().to_string(), + "micro" | "microseconds" | "mu" => + UPTIME.elapsed().as_micros().to_string(), + _ => UPTIME.elapsed().as_nanos().to_string(), + } + }, + )) + }), + ); +} |