diff options
author | metamuffin <metamuffin@disroot.org> | 2024-07-04 13:20:06 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-07-04 13:20:06 +0200 |
commit | 847a61d3ddf0c4a7656ed330f344ccf27681e240 (patch) | |
tree | d94775f4854c7487af80ccde6cd3967acdf8fc15 /src | |
parent | 270283eb61716fa51c0c9b96ef3e00d23eb94dc9 (diff) | |
download | meta-adservices-847a61d3ddf0c4a7656ed330f344ccf27681e240.tar meta-adservices-847a61d3ddf0c4a7656ed330f344ccf27681e240.tar.bz2 meta-adservices-847a61d3ddf0c4a7656ed330f344ccf27681e240.tar.zst |
change ad index to use sidecar files
Diffstat (limited to 'src')
-rw-r--r-- | src/embed.rs | 2 | ||||
-rw-r--r-- | src/error.rs | 10 | ||||
-rw-r--r-- | src/main.rs | 35 | ||||
-rw-r--r-- | src/state.rs | 8 |
4 files changed, 41 insertions, 14 deletions
diff --git a/src/embed.rs b/src/embed.rs index 9452ac4..b7d44aa 100644 --- a/src/embed.rs +++ b/src/embed.rs @@ -75,7 +75,7 @@ pub async fn r_image(state: &State<Arc<Logic>>, k: &str) -> MyResult<CachedFile> .get(&k.to_owned()) .ok_or(anyhow!("ad does not exist"))?; Ok(CachedFile( - File::open(state.config.image_base.join(&info.image)).await?, + File::open(state.config.ad_dir.join(&info.image)).await?, )) } diff --git a/src/error.rs b/src/error.rs index 04d7e38..c3235f0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,3 @@ -use std::fmt::Display; - use rocket::{ response::{self, Responder}, Request, @@ -10,7 +8,9 @@ pub type MyResult<T> = Result<T, MyError>; #[derive(Debug, Error)] pub enum MyError { + #[error("{0}")] Anyhow(#[from] anyhow::Error), + #[error("{0}")] Io(#[from] std::io::Error), } @@ -19,9 +19,3 @@ impl<'r> Responder<'r, 'static> for MyError { format!("{self}").respond_to(req) } } - -impl Display for MyError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{self:?}") - } -} diff --git a/src/main.rs b/src/main.rs index 79c3842..891cde2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ use rocket::{ shield::{self, Shield}, Request, Response, }; -use state::{Config, Logic}; +use state::{AdInfo, Config, Logic}; use std::{io::Cursor, net::IpAddr}; #[rocket::main] @@ -30,7 +30,38 @@ async fn main() { let config = rocket::tokio::fs::read_to_string(config) .await .expect("could not read config"); - let config: Config = serde_yaml::from_str(config.as_str()).expect("config invalid"); + let mut config: Config = toml::from_str(config.as_str()).expect("config invalid"); + + for entry in config.ad_dir.read_dir().expect("cannot read ad directory") { + if let Ok(entry) = entry { + if entry + .path() + .file_name() + .unwrap() + .to_str() + .unwrap() + .ends_with(".toml") + { + let path = entry.path(); + let imname = path.file_stem().unwrap().to_str().unwrap(); + let basename = imname.split_once(".").unwrap().0; + let info: AdInfo = toml::from_str( + &rocket::tokio::fs::read_to_string(entry.path()) + .await + .unwrap(), + ) + .unwrap(); + config.ads.insert( + basename.to_string(), + AdInfo { + image: imname.into(), + ..info + }, + ); + } + } + } + eprintln!("{config:?}"); let state = Logic::new(config); diff --git a/src/state.rs b/src/state.rs index 266199b..4c91c6a 100644 --- a/src/state.rs +++ b/src/state.rs @@ -11,20 +11,22 @@ use std::time::Duration; use std::{collections::HashMap, net::IpAddr, path::PathBuf}; use std::{process::exit, sync::Arc}; -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] pub struct AdInfo { + #[serde(default)] pub image: PathBuf, pub target: String, } -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] pub struct Config { bloom_filter_size: usize, impression_weight_falloff: f64, leaderboard_weight_threshold: f64, - pub image_base: PathBuf, + pub ad_dir: PathBuf, database_path: PathBuf, pub port: u16, + #[serde(default)] pub ads: HashMap<String, AdInfo>, } |