Skip to content

Commit

Permalink
Further implement caching
Browse files Browse the repository at this point in the history
  • Loading branch information
strawmelonjuice committed Jan 1, 2024
1 parent fd5534d commit b42a94b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 35 deletions.
71 changes: 42 additions & 29 deletions src/contentservers.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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::<u64>().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()
}
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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::<u16>()
.unwrap();
let portnum: u16 = match std::env::var("PORT") {
Ok(g) => g.parse::<u16>().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: <https://github.com/strawmelonjuice/CynthiaWebsiteEngine/blob/rust/docs/jsr.md>")),
g => {
Expand Down

0 comments on commit b42a94b

Please sign in to comment.