Remove the bot views from the visitors counter
parent
bbba1ead64
commit
ad519d80f7
|
@ -14,3 +14,5 @@ chrono = { version = "0.4.9", features = ["serde"] }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
comrak = "0.24.1"
|
comrak = "0.24.1"
|
||||||
tera = "1.19.1"
|
tera = "1.19.1"
|
||||||
|
isbot = "0.1.3"
|
||||||
|
lazy_static = "1.4.0"
|
||||||
|
|
32
src/main.rs
32
src/main.rs
|
@ -13,13 +13,39 @@ extern crate diesel;
|
||||||
use comrak::{markdown_to_html, Options};
|
use comrak::{markdown_to_html, Options};
|
||||||
use controllers::posts;
|
use controllers::posts;
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
|
use isbot::Bots;
|
||||||
|
use lazy_static::lazy_static;
|
||||||
use rocket::fs::{FileServer, NamedFile};
|
use rocket::fs::{FileServer, NamedFile};
|
||||||
|
use rocket::http::Status;
|
||||||
|
use rocket::request::{FromRequest, Outcome, Request};
|
||||||
use rocket::response::content::RawXml;
|
use rocket::response::content::RawXml;
|
||||||
use rocket::Request;
|
|
||||||
use rocket_dyn_templates::Template;
|
use rocket_dyn_templates::Template;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::vec::Vec;
|
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<Self, Self::Error> {
|
||||||
|
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("/?<page>")]
|
#[get("/?<page>")]
|
||||||
fn index(page: Option<u64>) -> Template {
|
fn index(page: Option<u64>) -> Template {
|
||||||
let page: u64 = page.unwrap_or(1);
|
let page: u64 = page.unwrap_or(1);
|
||||||
|
@ -72,13 +98,15 @@ fn atom_feed() -> RawXml<Template> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/post/<title>")]
|
#[get("/post/<title>")]
|
||||||
fn show_post(title: String) -> Template {
|
fn show_post(title: String, headers: Headers) -> Template {
|
||||||
let title_splited: Vec<&str> = title.split('-').collect();
|
let title_splited: Vec<&str> = title.split('-').collect();
|
||||||
let id = title_splited.last().unwrap().parse().unwrap_or(-1);
|
let id = title_splited.last().unwrap().parse().unwrap_or(-1);
|
||||||
|
|
||||||
match posts::get_post(id) {
|
match posts::get_post(id) {
|
||||||
Ok(mut post) => {
|
Ok(mut post) => {
|
||||||
|
if !ISBOT.is_bot(&headers.user_agent) {
|
||||||
posts::add_visit(id);
|
posts::add_visit(id);
|
||||||
|
}
|
||||||
|
|
||||||
let mut comrak_options = Options::default();
|
let mut comrak_options = Options::default();
|
||||||
comrak_options.extension.table = true;
|
comrak_options.extension.table = true;
|
||||||
|
|
Loading…
Reference in New Issue