moved core functions to web-base crate
This commit is contained in:
parent
9b989745e0
commit
873b91f5fd
7 changed files with 29 additions and 127 deletions
|
@ -1,62 +1,5 @@
|
|||
use actix_files::NamedFile;
|
||||
use actix_web::*;
|
||||
use std::io::Write;
|
||||
|
||||
// Bootstrap
|
||||
|
||||
async fn download_file(url: &str, file: &str) {
|
||||
let content = reqwest::get(url).await.expect("couldn't download file");
|
||||
std::fs::File::create(file)
|
||||
.unwrap()
|
||||
.write_all(&content.bytes().await.unwrap())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub(crate) async fn cache_bootstrap() {
|
||||
std::fs::create_dir_all("./cache/fonts").expect("couldn't create cache dir");
|
||||
download_file(
|
||||
"https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css",
|
||||
"./cache/bootstrap.min.css",
|
||||
)
|
||||
.await;
|
||||
download_file(
|
||||
"https://cdn.jsdelivr.net/npm/bootstrap-icons@1.9.1/font/bootstrap-icons.css",
|
||||
"./cache/bootstrap-icons.css",
|
||||
)
|
||||
.await;
|
||||
download_file(
|
||||
"https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js",
|
||||
"./cache/bootstrap.bundle.min.js",
|
||||
)
|
||||
.await;
|
||||
download_file("https://cdn.jsdelivr.net/npm/bootstrap-icons@1.9.1/font/fonts/bootstrap-icons.woff2?8d200481aa7f02a2d63a331fc782cfaf", "./cache/fonts/bootstrap-icons.woff2").await;
|
||||
download_file("https://cdn.jsdelivr.net/npm/bootstrap-icons@1.9.1/font/fonts/bootstrap-icons.woff?8d200481aa7f02a2d63a331fc782cfaf", "./cache/fonts/bootstrap-icons.woff").await;
|
||||
}
|
||||
|
||||
#[get("/bootstrap.min.css")]
|
||||
pub(crate) async fn bootstrap_css() -> Result<NamedFile> {
|
||||
Ok(NamedFile::open("./cache/bootstrap.min.css")?)
|
||||
}
|
||||
|
||||
#[get("/bootstrap-icons.css")]
|
||||
pub(crate) async fn bootstrap_icons() -> Result<NamedFile> {
|
||||
Ok(NamedFile::open("./cache/bootstrap-icons.css")?)
|
||||
}
|
||||
|
||||
#[get("/bootstrap.bundle.min.js")]
|
||||
pub(crate) async fn bootstrap_js() -> Result<NamedFile> {
|
||||
Ok(NamedFile::open("./cache/bootstrap.bundle.min.js")?)
|
||||
}
|
||||
|
||||
#[get("/fonts/bootstrap-icons.woff2")]
|
||||
pub(crate) async fn bootstrap_font1(_: HttpRequest) -> Result<NamedFile> {
|
||||
Ok(NamedFile::open("./cache/fonts/bootstrap-icons.woff2")?)
|
||||
}
|
||||
|
||||
#[get("/fonts/bootstrap-icons.woff")]
|
||||
pub(crate) async fn bootstrap_font2(_: HttpRequest) -> Result<NamedFile> {
|
||||
Ok(NamedFile::open("./cache/fonts/bootstrap-icons.woff")?)
|
||||
}
|
||||
|
||||
// Assets
|
||||
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
pub fn is_browser(req: &actix_web::HttpRequest) -> bool {
|
||||
let ua = req
|
||||
.headers()
|
||||
.get("user-agent")
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.to_lowercase();
|
||||
if ua.contains("chrome") || ua.contains("safari") || ua.contains("firefox") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
pub fn get_host(r: &actix_web::HttpRequest) -> String {
|
||||
let res = r.headers().get("HOST").unwrap().to_str().unwrap();
|
||||
return res.to_string();
|
||||
}
|
||||
|
||||
pub fn get_host_address(r: &actix_web::HttpRequest) -> String {
|
||||
let res = r.headers().get("HOST").unwrap().to_str().unwrap();
|
||||
let res: Vec<&str> = res.split(":").collect();
|
||||
let res = res.first().unwrap();
|
||||
return res.to_string();
|
||||
}
|
||||
|
||||
pub fn is_onion(r: &actix_web::HttpRequest) -> bool {
|
||||
return get_host_address(r).ends_with("onion");
|
||||
}
|
||||
|
||||
pub fn is_i2p(r: &actix_web::HttpRequest) -> bool {
|
||||
return get_host_address(r).ends_with("i2p");
|
||||
}
|
||||
|
||||
pub fn dynamic_link(
|
||||
inner: &str,
|
||||
normal_link: &str,
|
||||
onion: Option<&str>,
|
||||
i2p: Option<&str>,
|
||||
req: &actix_web::HttpRequest,
|
||||
) -> String {
|
||||
if is_onion(req) {
|
||||
let href = onion.unwrap_or(normal_link);
|
||||
return format!("<a href={href}> {inner} </a>");
|
||||
}
|
||||
if is_i2p(req) {
|
||||
let href = i2p.unwrap_or(normal_link);
|
||||
return format!("<a href={href}> {inner} </a>");
|
||||
}
|
||||
return format!("<a href={normal_link}> {inner} </a>");
|
||||
}
|
|
@ -28,7 +28,7 @@ pub async fn message_post(r: HttpRequest, f: Form<MessageForm>) -> impl Responde
|
|||
#[get("/message")]
|
||||
pub async fn message_page(r: HttpRequest) -> impl Responder {
|
||||
let config: &web::Data<config::Config> = r.app_data().unwrap();
|
||||
let host = pages::func::get_host(&r);
|
||||
let host = web_base::func::get_host(&r);
|
||||
let resp = format!(
|
||||
r#"
|
||||
<div class="container" style="margin-top: 25px"><h1>Message</h1>
|
||||
|
@ -50,7 +50,7 @@ pub async fn mirrors(r: HttpRequest) -> impl Responder {
|
|||
let config: &web::Data<config::Config> = r.app_data().unwrap();
|
||||
if let Ok(mirror_file) = std::fs::File::open("/config/mirrors.txt") {
|
||||
let content = std::io::read_to_string(mirror_file).unwrap();
|
||||
if pages::func::is_browser(&r) {
|
||||
if web_base::func::is_browser(&r) {
|
||||
let resp = format!(
|
||||
r#"
|
||||
<div style="margin: 25px;">
|
||||
|
@ -71,9 +71,9 @@ pub async fn mirrors(r: HttpRequest) -> impl Responder {
|
|||
|
||||
#[get("/public_key")]
|
||||
pub async fn public_key(r: HttpRequest) -> impl Responder {
|
||||
if pages::func::is_browser(&r) {
|
||||
if web_base::func::is_browser(&r) {
|
||||
let config: &web::Data<config::Config> = r.app_data().unwrap();
|
||||
let host = format!("http://{}", pages::func::get_host(&r));
|
||||
let host = format!("http://{}", web_base::func::get_host(&r));
|
||||
let key = std::io::read_to_string(std::fs::File::open("/config/pub.key").unwrap()).unwrap();
|
||||
|
||||
let pgp = gnupg::GnuPG::new().unwrap();
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
pub mod assets;
|
||||
pub mod func;
|
||||
pub mod html_fn;
|
||||
pub mod index;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue