Convert to monorepo with utils library

This commit is contained in:
Elnu 2023-08-08 13:43:50 -07:00
parent befc0c0ee3
commit dad460ad37
8 changed files with 38 additions and 13 deletions

9
utils/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "utils"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "4.3.1"

21
utils/src/lib.rs Normal file
View file

@ -0,0 +1,21 @@
use actix_web::{http::header, HttpRequest};
pub fn is_human(request: &HttpRequest) -> bool {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
const HUMANS: &[&str] = &[
"Firefox", "Chrome", // Chrome and Chromium browsers
"Opera", // Old Presto-based Opera
"Mobile", // Safari
"Trident", // Internet Explorer
];
request
.headers()
.get(header::USER_AGENT)
.and_then(|header| header.to_str().ok())
.map(|ua| {
HUMANS
.iter()
.any(|&human| ua.contains(human))
})
.unwrap_or(false)
}