Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow offering dynamic script output for download #251

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ qrcodegen = "=1.6.0"
rust-embed = "6.2.0"
rustls-acme = "0.1.7"
serde_yaml = "0.8.17"
tempfile = "3.2.0"
termcolor = "1.1.2"
tokio-rustls = "0.22.0"
tonic = "0.5.2"
cradle = "0.2.0"

[dependencies.agora-lnd-client]
path = "agora-lnd-client"
Expand All @@ -60,7 +62,7 @@ features = ["wrap_help"]

[dependencies.tokio]
version = "1.5.0"
features = ["rt", "rt-multi-thread", "macros", "fs"]
features = ["rt", "rt-multi-thread", "macros", "fs", "process"]

[dependencies.tokio-stream]
version = "0.1.7"
Expand All @@ -83,7 +85,6 @@ pretty_assertions = "1.0.0"
regex = "1.5.4"
resvg = "0.15.0"
scraper = "0.12.0"
tempfile = "3.2.0"
tiny-skia = "0.5.1"
unindent = "0.1.7"
usvg = "0.15.0"
Expand Down
6 changes: 6 additions & 0 deletions example-files/free/.agora.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
paid: false
files:
test-script:
type: script
source: |
#!/usr/bin/env bash
date
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub(crate) use crate::{
arguments::Arguments,
config::Config,
config::{Config, VirtualFile},
environment::Environment,
error::{self, Error, Result},
error_page, html,
Expand Down
129 changes: 112 additions & 17 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,64 @@
use crate::common::*;
use std::collections::BTreeMap;

#[derive(PartialEq, Debug, Default, Deserialize)]
#[derive(PartialEq, Debug, Deserialize, Clone)]
#[serde(tag = "type", deny_unknown_fields, rename_all = "snake_case")]
pub(crate) enum VirtualFile {
Script { source: String },
}

#[derive(PartialEq, Debug, Default, Deserialize, Clone)]
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
pub(crate) struct Config {
paid: Option<bool>,
pub(crate) base_price: Option<Millisatoshi>,
pub(crate) files: BTreeMap<Filename, VirtualFile>,
}

use serde::{
de::{self, Unexpected, Visitor},
Deserializer,
};

#[derive(Clone, PartialEq, Debug, Eq, Ord, PartialOrd)]
pub(crate) struct Filename(pub(crate) String);

impl<'de> Deserialize<'de> for Filename {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_string(FilenameVisitor)
}
}

struct FilenameVisitor;

use std::{
fmt::{self, Formatter},
path::Component,
};

impl<'de> Visitor<'de> for FilenameVisitor {
type Value = Filename;

fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
formatter.write_str("a valid filename")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
match Path::new(v)
.components()
.collect::<Vec<Component>>()
.as_slice()
{
[Component::Normal(filename)] if *filename == v => Ok(Filename(v.to_owned())),
_ => Err(E::invalid_value(Unexpected::Str(v), &"a valid filename")),
}
}
}

impl Config {
Expand All @@ -22,16 +76,18 @@ impl Config {
}
path.read_dir().context(error::FilesystemIo { path })?;
let mut config = Self::default();
for path in path.ancestors() {
let mut ancestors = path.ancestors().collect::<Vec<_>>();
ancestors.reverse();
for path in ancestors {
if !path.starts_with(base_directory) {
break;
continue;
}
let file_path = path.join(".agora.yaml");
match fs::read_to_string(&file_path) {
Ok(yaml) => {
let parent =
let child: Config =
serde_yaml::from_str(&yaml).context(error::ConfigDeserialize { path: file_path })?;
config.merge_parent(parent);
config.merge_child(child);
}
Err(error) if error.kind() == io::ErrorKind::NotFound => {}
Err(source) => return Err(error::FilesystemIo { path: file_path }.into_error(source)),
Expand All @@ -40,10 +96,11 @@ impl Config {
Ok(config)
}

fn merge_parent(&mut self, parent: Self) {
fn merge_child(&mut self, child: Self) {
*self = Self {
paid: self.paid.or(parent.paid),
base_price: self.base_price.or(parent.base_price),
paid: child.paid.or(self.paid),
base_price: child.base_price.or(self.base_price),
files: child.files.clone(),
};
}
}
Expand All @@ -59,7 +116,8 @@ mod tests {
assert_eq!(
Config {
paid: None,
base_price: None
base_price: None,
files: BTreeMap::new()
},
Config::default()
);
Expand All @@ -81,7 +139,8 @@ mod tests {
config,
Config {
paid: Some(true),
base_price: None
base_price: None,
files: BTreeMap::new()
}
);
}
Expand Down Expand Up @@ -169,7 +228,8 @@ mod tests {
config,
Config {
paid: Some(true),
base_price: Some(Millisatoshi::new(42_000))
base_price: Some(Millisatoshi::new(42_000)),
files: BTreeMap::new()
}
);
}
Expand All @@ -189,7 +249,8 @@ mod tests {
config,
Config {
paid: Some(false),
base_price: Some(Millisatoshi::new(42_000))
base_price: Some(Millisatoshi::new(42_000)),
files: BTreeMap::new()
}
);
}
Expand All @@ -213,7 +274,8 @@ mod tests {
config,
Config {
paid: Some(true),
base_price: Some(Millisatoshi::new(23_000))
base_price: Some(Millisatoshi::new(23_000)),
files: BTreeMap::new()
}
);
}
Expand All @@ -237,7 +299,8 @@ mod tests {
config,
Config {
paid: Some(true),
base_price: Some(Millisatoshi::new(42_000))
base_price: Some(Millisatoshi::new(42_000)),
files: BTreeMap::new()
}
);
}
Expand All @@ -262,7 +325,8 @@ mod tests {
config,
Config {
paid: Some(true),
base_price: Some(Millisatoshi::new(42_000))
base_price: Some(Millisatoshi::new(42_000)),
files: BTreeMap::new()
}
);
}
Expand All @@ -283,7 +347,8 @@ mod tests {
config,
Config {
paid: None,
base_price: None
base_price: None,
files: BTreeMap::new()
}
);
let config = Config::for_dir(
Expand All @@ -295,7 +360,37 @@ mod tests {
config,
Config {
paid: None,
base_price: None
base_price: None,
files: BTreeMap::new()
}
);
}

#[test]
fn loads_virtual_file_configuration() {
let temp_dir = TempDir::new().unwrap();
let yaml = "
files:
foo:
type: script
source: test source
"
.unindent();
fs::write(temp_dir.path().join(".agora.yaml"), yaml).unwrap();
let config = Config::for_dir(temp_dir.path(), temp_dir.path()).unwrap();
let mut expected_files = BTreeMap::new();
expected_files.insert(
Filename("foo".to_string()),
VirtualFile::Script {
source: "test source".to_string(),
},
);
assert_eq!(
config,
Config {
paid: None,
base_price: None,
files: expected_files
}
);
}
Expand Down
Loading