Skip to main content

ordinary_utils/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3#![warn(clippy::all, clippy::pedantic)]
4#![allow(clippy::missing_errors_doc, clippy::cast_precision_loss)]
5
6// Copyright (C) 2026 Ordinary Labs, LLC.
7//
8// SPDX-License-Identifier: AGPL-3.0-only
9
10pub mod compression;
11pub mod dns;
12pub mod headers;
13pub mod json;
14pub mod middleware;
15pub mod response;
16mod server;
17pub mod tcp;
18pub mod wasm;
19
20pub use server::*;
21
22use time::format_description::BorrowedFormatItem;
23use time::macros::format_description;
24use tokio::signal;
25
26#[cfg(target_os = "linux")]
27pub static SERVER: &str = concat!("ordinaryd/", env!("CARGO_PKG_VERSION"), " (Linux)");
28#[cfg(target_os = "macos")]
29pub static SERVER: &str = concat!("ordinaryd/", env!("CARGO_PKG_VERSION"), " (macOS)");
30#[cfg(all(unix, not(target_os = "macos"), not(target_os = "linux")))]
31pub static SERVER: &str = concat!("ordinaryd/", env!("CARGO_PKG_VERSION"), " (Unix)");
32
33#[cfg(all(not(unix), not(target_os = "macos"), not(target_os = "linux")))]
34pub static SERVER: &str = concat!("ordinaryd/", env!("CARGO_PKG_VERSION"));
35
36pub static GMT_FORMAT: &[BorrowedFormatItem<'static>] = format_description!(
37    "[weekday repr:short], [day] [month repr:short] [year] [hour]:[minute]:[second] GMT"
38);
39
40#[allow(clippy::missing_panics_doc)]
41pub async fn shutdown_signal() {
42    let ctrl_c = async {
43        signal::ctrl_c()
44            .await
45            .expect("failed to install Ctrl+C handler");
46    };
47
48    #[cfg(unix)]
49    let terminate = async {
50        signal::unix::signal(signal::unix::SignalKind::terminate())
51            .expect("failed to install signal handler")
52            .recv()
53            .await;
54    };
55
56    #[cfg(not(unix))]
57    let terminate = std::future::pending::<()>();
58
59    tokio::select! {
60        () = ctrl_c => {},
61        () = terminate => {},
62    }
63}