diff --git a/src/contentservers.rs b/src/contentservers.rs index a0e8cba..8f37c83 100644 --- a/src/contentservers.rs +++ b/src/contentservers.rs @@ -1,9 +1,12 @@ +use std::fs; use actix_web::HttpResponse; use colored::Colorize; use curl::easy::Easy; +use dotenv::dotenv; use markdown::{to_html_with_options, CompileOptions, Options}; use crate::{logger::logger, structs::*}; +use crate::files::{cacheplacer, cacheretriever}; use self::postlists::postlist_table_gen; @@ -321,38 +324,48 @@ pub(crate) fn generate_menus(pgid: String, probableurl: &String) -> Menulist { } pub(crate) fn fetcher(uri: String) -> String { - let mut data = Vec::new(); - let mut c = Easy::new(); - c.url(&uri).unwrap(); - { - let mut transfer = c.transfer(); - match transfer.write_function(|new_data| { - data.extend_from_slice(new_data); - Ok(new_data.len()) - }) { - Ok(v) => v, - Err(_e) => { - logger(5, String::from("Could not fetch external content!")); + dotenv().ok(); + let cachelifetime: u64 = match std::env::var("EXTERNAL_CACHE_LIFETIME") { + Ok(g) => g.parse::().unwrap(), + Err(_) => 1200, + }; + return match cacheretriever(uri.clone(), cachelifetime) { + Ok(o) => fs::read_to_string(o).expect("Couldn't find or open a JS file."), + Err(_) => { + let mut data = Vec::new(); + let mut c = Easy::new(); + c.url(&uri).unwrap(); + { + let mut transfer = c.transfer(); + match transfer.write_function(|new_data| { + data.extend_from_slice(new_data); + Ok(new_data.len()) + }) { + Ok(v) => v, + Err(_e) => { + logger(5, String::from("Could not fetch external content!")); - return "contentlocationerror".to_owned(); - } - }; - match transfer.perform() { - Ok(v) => v, - Err(_e) => { - logger(5, String::from("Could not fetch external content!")); + return "contentlocationerror".to_owned(); + } + }; + match transfer.perform() { + Ok(v) => v, + Err(_e) => { + logger(5, String::from("Could not fetch external content!")); - return "contentlocationerror".to_owned(); + return "contentlocationerror".to_owned(); + } + }; } - }; - } - let resp = match std::str::from_utf8(&data) { - Ok(v) => v, - Err(_e) => { - logger(5, String::from("Could not fetch external content!")); + let resp = match std::str::from_utf8(&data) { + Ok(v) => v, + Err(_e) => { + logger(5, String::from("Could not fetch external content!")); - return "contentlocationerror".to_owned(); - } + return "contentlocationerror".to_owned(); + } + }; + cacheplacer(uri, resp.to_owned()) + }, }; - resp.to_owned() } diff --git a/src/main.rs b/src/main.rs index ed4eacf..32f29b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -360,12 +360,12 @@ As of now, Cynthia has only 4 commands: ); process::exit(1); } - if !Path::new("./.env").exists() { + if !(Path::new("./.env").exists()) || !(Path::new("./cynthiaFiles").exists()) { logger(5, String::from("No CynthiaConfig found.")); logger( 10, format!( - "To set up a clean Cynthia config, run {} {}.", + "To set up a clean Cynthia config, run '{} {}'.", std::env::args() .next() .unwrap_or(String::from("cynthiaweb")) @@ -404,10 +404,10 @@ As of now, Cynthia has only 4 commands: process::exit(1); } } - let portnum: u16 = std::env::var("PORT") - .expect("PORT must be set in the '.env' file.") - .parse::() - .unwrap(); + let portnum: u16 = match std::env::var("PORT") { + Ok(g) => g.parse::().unwrap(), + Err(_) => 3000, + }; match jsr::jsruntime(true) { "" => logger(5, String::from("No JS runtime found! Cynthia doesn't need one, but most of it's plugins do!\n\nSee: ")), g => {