mirror of
https://codeberg.org/icewind/rss-webhook-trigger.git
synced 2026-06-03 18:04:09 +02:00
allow loading headers from files
This commit is contained in:
parent
21e8b90d8f
commit
94c7571d28
1 changed files with 35 additions and 2 deletions
|
|
@ -1,8 +1,12 @@
|
||||||
use color_eyre::{eyre::WrapErr, Result};
|
use color_eyre::{eyre::WrapErr, Result};
|
||||||
use serde::Deserialize;
|
use serde::{Deserialize, Deserializer};
|
||||||
use std::fs::read_to_string;
|
use std::fs::read_to_string;
|
||||||
use tokio::time::Duration;
|
use tokio::time::Duration;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::convert::{TryFrom, TryInto};
|
||||||
|
use std::path::Path;
|
||||||
|
use reqwest::header::{HeaderValue, InvalidHeaderValue};
|
||||||
|
use serde::de::Error;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
|
|
@ -16,7 +20,7 @@ pub struct FeedConfig {
|
||||||
pub feed: String,
|
pub feed: String,
|
||||||
pub hook: String,
|
pub hook: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub headers: HashMap<String, String>,
|
pub headers: HashMap<String, HeaderVal>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub body: Value,
|
pub body: Value,
|
||||||
}
|
}
|
||||||
|
|
@ -32,3 +36,32 @@ impl Config {
|
||||||
Duration::from_secs(self.interval.unwrap_or(30 * 60))
|
Duration::from_secs(self.interval.unwrap_or(30 * 60))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct HeaderVal(String);
|
||||||
|
|
||||||
|
impl<'de> Deserialize<'de> for HeaderVal {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
|
||||||
|
let raw = String::deserialize(deserializer)?;
|
||||||
|
let str = load_secret(raw).map_err(|e| D::Error::custom(e))?;
|
||||||
|
Ok(HeaderVal(str))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<&HeaderVal> for HeaderValue {
|
||||||
|
type Error = InvalidHeaderValue;
|
||||||
|
|
||||||
|
fn try_from(header: &HeaderVal) -> std::result::Result<Self, Self::Error> {
|
||||||
|
header.0.as_str().try_into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_secret(raw: String) -> Result<String, std::io::Error> {
|
||||||
|
let path: &Path = raw.as_ref();
|
||||||
|
if raw.starts_with("/") && path.exists() {
|
||||||
|
let raw = read_to_string(raw)?;
|
||||||
|
Ok(raw.trim().into())
|
||||||
|
} else {
|
||||||
|
Ok(raw)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue