Replace lazy_Static with Tokio OnceCell
parent
ad519d80f7
commit
42b4285ae9
|
@ -15,4 +15,4 @@ serde = { version = "1.0", features = ["derive"] }
|
|||
comrak = "0.24.1"
|
||||
tera = "1.19.1"
|
||||
isbot = "0.1.3"
|
||||
lazy_static = "1.4.0"
|
||||
once_cell = "1.19.0"
|
||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -14,17 +14,20 @@ 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::tokio::sync::OnceCell;
|
||||
use rocket_dyn_templates::Template;
|
||||
use std::path::Path;
|
||||
use std::vec::Vec;
|
||||
|
||||
lazy_static! {
|
||||
static ref ISBOT: Bots = Bots::default();
|
||||
static BOTS: OnceCell<Bots> = 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 {
|
||||
|
@ -98,13 +101,13 @@ fn atom_feed() -> RawXml<Template> {
|
|||
}
|
||||
|
||||
#[get("/post/<title>")]
|
||||
fn show_post(title: String, headers: Headers) -> Template {
|
||||
async fn show_post(title: &str, headers: Headers) -> Template {
|
||||
let title_splited: Vec<&str> = title.split('-').collect();
|
||||
let id = title_splited.last().unwrap().parse().unwrap_or(-1);
|
||||
|
||||
match posts::get_post(id) {
|
||||
Ok(mut post) => {
|
||||
if !ISBOT.is_bot(&headers.user_agent) {
|
||||
if !is_bot(&headers.user_agent).await {
|
||||
posts::add_visit(id);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue