mirror of
https://github.com/DefectingCat/candy
synced 2025-07-15 16:51:34 +00:00
refactor mime type with cow str
This commit is contained in:
@ -4,7 +4,7 @@ use crate::{
|
||||
},
|
||||
error::Result,
|
||||
};
|
||||
use std::{collections::BTreeMap, fs};
|
||||
use std::{borrow::Cow, collections::BTreeMap, fs};
|
||||
|
||||
use anyhow::Context;
|
||||
use serde::Deserialize;
|
||||
@ -25,7 +25,7 @@ pub struct SettingHost {
|
||||
pub port: u32,
|
||||
route: Vec<Option<SettingRoute>>,
|
||||
#[serde(skip_deserializing, skip_serializing)]
|
||||
pub route_map: BTreeMap<String, SettingRoute>,
|
||||
pub route_map: HostRouteMap,
|
||||
/// Index files format
|
||||
#[serde(default = "host_index")]
|
||||
pub index: Vec<String>,
|
||||
@ -34,12 +34,14 @@ pub struct SettingHost {
|
||||
pub keep_alive: u16,
|
||||
}
|
||||
|
||||
pub type MIMEType = BTreeMap<Cow<'static, str>, Cow<'static, str>>;
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
pub struct Settings {
|
||||
#[serde(default = "mime_default")]
|
||||
pub default_type: String,
|
||||
pub default_type: Cow<'static, str>,
|
||||
#[serde(default = "types_default")]
|
||||
pub types: BTreeMap<String, String>,
|
||||
pub types: MIMEType,
|
||||
pub host: Vec<SettingHost>,
|
||||
}
|
||||
|
||||
@ -64,3 +66,11 @@ pub fn init_config() -> Result<Settings> {
|
||||
|
||||
Ok(settings)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// #[test]
|
||||
// fn
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
use std::{collections::BTreeMap, env, process::exit, sync::OnceLock};
|
||||
use std::{borrow::Cow, collections::BTreeMap, env, process::exit, sync::OnceLock};
|
||||
|
||||
use tracing::error;
|
||||
|
||||
use crate::config::{init_config, Settings};
|
||||
use crate::config::{init_config, MIMEType, Settings};
|
||||
|
||||
// global settings
|
||||
static SETTINGS: OnceLock<Settings> = OnceLock::new();
|
||||
@ -37,20 +37,20 @@ pub fn keep_alive_timeout_default() -> u16 {
|
||||
|
||||
// default mime type for unknow file
|
||||
pub const MIME_DEFAULT: &str = "application/octet-stream";
|
||||
pub fn mime_default() -> String {
|
||||
MIME_DEFAULT.to_string()
|
||||
pub fn mime_default() -> Cow<'static, str> {
|
||||
MIME_DEFAULT.into()
|
||||
}
|
||||
|
||||
// default mime types
|
||||
pub fn types_default() -> BTreeMap<String, String> {
|
||||
pub fn types_default() -> MIMEType {
|
||||
BTreeMap::new()
|
||||
}
|
||||
macro_rules! insert_mime {
|
||||
($name:literal, $mime:ident, $map:ident) => {
|
||||
$map.entry($name.to_string()).or_insert($mime.to_string());
|
||||
$map.entry($name.into()).or_insert($mime.into());
|
||||
};
|
||||
}
|
||||
pub fn insert_default_mimes(map: &mut BTreeMap<String, String>) {
|
||||
pub fn insert_default_mimes(map: &mut MIMEType) {
|
||||
use crate::http::mime::*;
|
||||
|
||||
insert_mime!("html", TEXT_HTML, map);
|
||||
|
Reference in New Issue
Block a user