diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..8a20982 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,62 @@ +#![feature(ip_bits)] +pub mod embed; +pub mod error; +pub mod info; +pub mod state; +use embed::*; +use info::*; + +use markup::Render; +use rocket::{ + catch, catchers, + fairing::AdHoc, + http::{ContentType, Header, Status}, + response::{self, Responder}, + routes, Request, Response, +}; +use state::{Config, Logic}; +use std::io::Cursor; + +#[rocket::main] +async fn main() { + env_logger::init_from_env("LOG"); + + let config = std::env::args() + .nth(1) + .expect("first arg needs to be the config"); + 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 state = Logic::new(config); + + let _ = rocket::build() + .attach(AdHoc::on_response("set server header", |_req, res| { + res.set_header(Header::new("server", "meta adservices")); + Box::pin(async {}) + })) + .manage(state) + .mount("/", routes![r_index, r_embed, r_style, r_image]) + .register("/", catchers![r_catch]) + .launch() + .await + .unwrap(); +} + +pub struct Template<T>(pub T); +impl<'r, T: Render> Responder<'r, 'static> for Template<T> { + fn respond_to(self, _req: &'r Request<'_>) -> response::Result<'static> { + let mut out = String::new(); + self.0.render(&mut out).unwrap(); + Response::build() + .header(ContentType::HTML) + .streamed_body(Cursor::new(out)) + .ok() + } +} + +#[catch(default)] +pub fn r_catch<'a>(status: Status, _request: &Request) -> String { + format!("{status}; This ad is likely misconfigured. Server could also be broken...") +} |