pub mod admin; pub mod contexts; pub mod controllers; pub mod misc; pub mod models; mod schema; #[macro_use] extern crate rocket; #[macro_use] extern crate diesel; use comrak::{markdown_to_html, Options}; use controllers::posts; use dotenv::dotenv; use isbot::Bots; use rocket::fs::{FileServer, NamedFile}; use rocket::http::Status; use rocket::request::{FromRequest, Outcome, Request}; use rocket::response::content::RawXml; use rocket::tokio::sync::OnceCell; use rocket_dyn_templates::Template; use std::path::Path; use std::vec::Vec; static BOTS: OnceCell = OnceCell::const_new(); async fn is_bot(ua: &String) -> bool { let bots = BOTS.get_or_init(|| async { Bots::default() }).await; bots.is_bot(ua) } struct Headers { user_agent: String, } #[rocket::async_trait] impl<'r> FromRequest<'r> for Headers { type Error = (); async fn from_request(request: &'r Request<'_>) -> Outcome { let token = request.headers().get_one("User-Agent"); match token { Some(token) => Outcome::Success(Headers { user_agent: token.to_string(), }), None => Outcome::Error((Status::Unauthorized, ())), } } } #[get("/?")] fn index(page: Option) -> Template { let page: u64 = page.unwrap_or(1); let (posts, n_posts) = posts::get_posts(Some(page)); let context = contexts::PageOfPosts::new(posts, page, n_posts); Template::render("index", context) } #[get("/feed.xml")] fn rss_feed() -> RawXml