diff --git a/Cargo.toml b/Cargo.toml index f7998cf..7315b72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,6 @@ rand = "0.8.5" chrono = { version = "0.4.9", features = ["serde"] } serde = { version = "1.0", features = ["derive"] } comrak = "0.24.1" -tera = "1.19.1" \ No newline at end of file +tera = "1.19.1" +isbot = "0.1.3" +lazy_static = "1.4.0" diff --git a/src/controllers.rs b/src/controllers.rs index bfc66d8..8493090 100644 --- a/src/controllers.rs +++ b/src/controllers.rs @@ -85,8 +85,8 @@ pub mod posts { let connection = establish_connection(); diesel::update(posts.filter(id.eq(post_id))) - .set(views.eq(views + 1)) - .execute(&connection) - .unwrap(); + .set(views.eq(views + 1)) + .execute(&connection) + .unwrap(); } } diff --git a/src/main.rs b/src/main.rs index 3b125b3..11cbe2f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,13 +13,39 @@ extern crate diesel; use comrak::{markdown_to_html, Options}; use controllers::posts; use dotenv::dotenv; +use isbot::Bots; +use lazy_static::lazy_static; use rocket::fs::{FileServer, NamedFile}; +use rocket::http::Status; +use rocket::request::{FromRequest, Outcome, Request}; use rocket::response::content::RawXml; -use rocket::Request; use rocket_dyn_templates::Template; use std::path::Path; use std::vec::Vec; +lazy_static! { + static ref ISBOT: Bots = Bots::default(); +} + +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); @@ -72,13 +98,15 @@ fn atom_feed() -> RawXml