generated from ElnuDev/rust-project
Convert to monorepo with utils library
This commit is contained in:
parent
befc0c0ee3
commit
dad460ad37
8 changed files with 38 additions and 13 deletions
9
utils/Cargo.toml
Normal file
9
utils/Cargo.toml
Normal 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
21
utils/src/lib.rs
Normal 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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue