1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
/*
This file is part of gnix (https://codeberg.org/metamuffin/gnix)
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2025 metamuffin <metamuffin.org>
*/
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_yml::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_yml::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)
})
}
}
|