diff options
author | metamuffin <metamuffin@disroot.org> | 2024-05-29 23:44:14 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-05-29 23:44:14 +0200 |
commit | 29c48afafb4a6a0a0636774f9b56423881fb1703 (patch) | |
tree | fa610555a33c25a1aaeb98242099c2010ac243b0 /src/filters/file.rs | |
parent | 886a18e0c67624d0882f04c7f6659bcfee6b4d8d (diff) | |
download | gnix-29c48afafb4a6a0a0636774f9b56423881fb1703.tar gnix-29c48afafb4a6a0a0636774f9b56423881fb1703.tar.bz2 gnix-29c48afafb4a6a0a0636774f9b56423881fb1703.tar.zst |
implement cookie base auth.
Diffstat (limited to 'src/filters/file.rs')
-rw-r--r-- | src/filters/file.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/filters/file.rs b/src/filters/file.rs new file mode 100644 index 0000000..53c27f4 --- /dev/null +++ b/src/filters/file.rs @@ -0,0 +1,62 @@ +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) + }) + } +} |