diff options
author | metamuffin <metamuffin@disroot.org> | 2024-05-30 00:09:11 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-05-30 00:09:11 +0200 |
commit | 532cc431d1c5ca1ffcf429a4ccb94edc7848fe7a (patch) | |
tree | c4422c4d54e01f63bae391cd95788cad74f59fbb /src/filters/file.rs | |
parent | 8b39940a58c28bc1bbe291eb5229e9ce1444e33c (diff) | |
download | gnix-532cc431d1c5ca1ffcf429a4ccb94edc7848fe7a.tar gnix-532cc431d1c5ca1ffcf429a4ccb94edc7848fe7a.tar.bz2 gnix-532cc431d1c5ca1ffcf429a4ccb94edc7848fe7a.tar.zst |
rename filters dir
Diffstat (limited to 'src/filters/file.rs')
-rw-r--r-- | src/filters/file.rs | 62 |
1 files changed, 0 insertions, 62 deletions
diff --git a/src/filters/file.rs b/src/filters/file.rs deleted file mode 100644 index 53c27f4..0000000 --- a/src/filters/file.rs +++ /dev/null @@ -1,62 +0,0 @@ -use super::{Node, NodeContext, NodeKind, NodeRequest, NodeResponse}; -use crate::error::ServiceError; -use futures::Future; -use http_body_util::{combinators::BoxBody, BodyExt}; -use hyper::{ - header::{HeaderValue, CONTENT_TYPE}, - Response, -}; -use serde::Deserialize; -use serde_yaml::Value; -use std::{fs::read_to_string, path::PathBuf, pin::Pin, sync::Arc}; - -pub struct FileKind; - -#[derive(Debug, Deserialize)] -struct FileConfig { - path: Option<PathBuf>, - content: Option<String>, - r#type: Option<String>, -} - -#[derive(Debug, Deserialize)] -struct File { - content: String, - r#type: String, -} - -impl NodeKind for FileKind { - fn name(&self) -> &'static str { - "file" - } - fn instanciate(&self, config: Value) -> anyhow::Result<Arc<dyn Node>> { - let conf = serde_yaml::from_value::<FileConfig>(config)?; - Ok(Arc::new(File { - content: conf - .content - .or(conf - .path - .map(|p| Ok::<_, ServiceError>(read_to_string(p)?)) - .transpose()?) - .unwrap_or_default(), - r#type: conf.r#type.unwrap_or("text/html".to_string()), // TODO infer mime from ext - })) - } -} - -impl Node for File { - fn handle<'a>( - &'a self, - _context: &'a mut NodeContext, - _request: NodeRequest, - ) -> Pin<Box<dyn Future<Output = Result<NodeResponse, ServiceError>> + Send + Sync + 'a>> { - Box::pin(async move { - let mut r = Response::new(BoxBody::<_, ServiceError>::new( - self.content.clone().map_err(|_| unreachable!()), - )); - r.headers_mut() - .insert(CONTENT_TYPE, HeaderValue::from_str(&self.r#type).unwrap()); - Ok(r) - }) - } -} |