diff --git a/Cargo.toml b/Cargo.toml index a519135..6c4874c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,4 @@ futures = "0.3.7" itertools = "0.9.0" log = "0.4.11" webpki = "0.21.0" +lazy_static = "1.4.0" diff --git a/examples/certificates.rs b/examples/certificates.rs index 095f65f..103aa7f 100644 --- a/examples/certificates.rs +++ b/examples/certificates.rs @@ -1,7 +1,7 @@ use anyhow::*; use futures::{future::BoxFuture, FutureExt}; use tokio::sync::RwLock; -use northstar::{Server, Request, Response, GEMINI_PORT, Certificate, gemini_mime}; +use northstar::{Certificate, GEMINI_MIME, GEMINI_PORT, Request, Response, Server}; use std::collections::HashMap; use std::sync::Arc; @@ -32,7 +32,7 @@ fn handle_request(users: Arc>>, request: Reque if let Some(user) = users_read.get(cert_bytes) { // The user has already registered Ok( - Response::success(&gemini_mime()?)? + Response::success(&GEMINI_MIME)? .with_body(format!("Welcome {}!", user)) ) } else { @@ -44,7 +44,7 @@ fn handle_request(users: Arc>>, request: Reque let mut users_write = users.write().await; users_write.insert(cert_bytes.clone(), username.to_owned()); Ok( - Response::success(&gemini_mime()?)? + Response::success(&GEMINI_MIME)? .with_body(format!( "Your account has been created {}! Welcome!", username diff --git a/src/lib.rs b/src/lib.rs index b5e59f6..0cc7d6b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,7 @@ use tokio_rustls::{rustls, TlsAcceptor}; use rustls::*; use anyhow::*; use uri::URIReference; +use lazy_static::lazy_static; pub mod types; pub mod util; @@ -203,9 +204,13 @@ fn load_key() -> Result { const GEMINI_MIME_STR: &str = "text/gemini"; +lazy_static! { + pub static ref GEMINI_MIME: Mime = GEMINI_MIME_STR.parse().expect("northstar BUG"); +} + +#[deprecated(note = "Use `GEMINI_MIME` instead", since = "0.3.0")] pub fn gemini_mime() -> Result { - let mime = GEMINI_MIME_STR.parse()?; - Ok(mime) + Ok(GEMINI_MIME.clone()) } /// A client cert verifier that accepts all connections @@ -243,3 +248,13 @@ impl ClientCertVerifier for AllowAnonOrSelfsignedClient { Ok(ClientCertVerified::assertion()) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn gemini_mime_parses() { + let _: &Mime = &GEMINI_MIME; + } +} diff --git a/src/util.rs b/src/util.rs index e7a6cf0..8b388fc 100644 --- a/src/util.rs +++ b/src/util.rs @@ -6,7 +6,7 @@ use tokio::{ fs::{self, File}, io, }; -use crate::{GEMINI_MIME_STR, Response, gemini_mime}; +use crate::{GEMINI_MIME, GEMINI_MIME_STR, Response}; use itertools::Itertools; pub async fn serve_file>(path: P, mime: &Mime) -> Result { @@ -82,7 +82,7 @@ async fn serve_dir_listing, B: AsRef>(path: P, virtual_path )?; } - Ok(Response::success(&gemini_mime()?)?.with_body(listing)) + Ok(Response::success(&GEMINI_MIME)?.with_body(listing)) } pub fn guess_mime_from_path>(path: P) -> Mime {