Skip to content

Commit fd5534d

Browse files
Env file options is expanding
1 parent 2a329ce commit fd5534d

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "cynthiaweb"
33
description = "A simple web server and generator based on plain file editing. I hate WordPress."
44
documentation = "https://cynthia-docs.strawmelonjuice.com/"
55
authors = ["MLC Bloeiman <[email protected]>"]
6-
version = "2.0.2-alpha.1"
6+
version = "2.0.5-alpha.1"
77
edition = "2021"
88
license = "GPL-3.0 OR MIT"
99

src/files.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ use std::io::Write;
99
use std::io::{Error, ErrorKind};
1010
use std::path::{Path, PathBuf};
1111
use std::time::{SystemTime, UNIX_EPOCH};
12-
use std::{env, fs};
12+
use std::fs;
13+
use dotenv::dotenv;
1314

1415
fn cachefolder() -> PathBuf {
15-
let fl = env::current_dir()
16-
.unwrap()
17-
.join("/.cynthiaTemp/cache/")
16+
let fl = PathBuf::from("./.cynthiaTemp/cache/")
1817
.join(format!("{}", std::process::id()))
1918
.normalize();
19+
// logger(31, format!("Cache folder: {}", fl.display()));
2020
fs::create_dir_all(&fl).unwrap();
2121
fl
2222
}
@@ -36,6 +36,7 @@ pub(crate) fn cacheretriever(file: String, max_age: u64) -> Result<PathBuf, Erro
3636
if (now - f.timestamp) < max_age {
3737
return Ok(f.cachepath);
3838
} else if Path::new(&f.cachepath).exists() {
39+
logger(31, format!("Cache {}: {} at {}, reason: Too old!","removed".red(), file, &f.cachepath.display()));
3940
fs::remove_file(Path::new(&f.cachepath)).unwrap();
4041
};
4142
}
@@ -59,6 +60,7 @@ pub(crate) fn cacheplacer(fileid: String, contents: String) -> String {
5960

6061
let mut cachedfile = File::create(cachepath.clone()).unwrap();
6162
write!(cachedfile, "{}", contents).unwrap();
63+
logger(31, format!("Cache {}: {} in {}", "placed".green(), fileid, cachepath.display()));
6264
let new = CynthiaCacheIndexObject {
6365
fileid,
6466
cachepath,
@@ -79,7 +81,12 @@ pub(crate) fn cacheplacer(fileid: String, contents: String) -> String {
7981
}
8082

8183
pub(crate) fn import_js_minified(scriptfile: String) -> String {
82-
return match cacheretriever(scriptfile.to_string(), 1200) {
84+
dotenv().ok();
85+
let jscachelifetime: u64 = match std::env::var("JAVASCRIPT_CACHE_LIFETIME") {
86+
Ok(g) => g.parse::<u64>().unwrap(),
87+
Err(_) => 1200,
88+
};
89+
return match cacheretriever(scriptfile.to_string(), jscachelifetime) {
8390
Ok(o) => fs::read_to_string(o).expect("Couldn't find or open a JS file."),
8491
Err(_) => match jsruntime(true) {
8592
BUNJSR => {
@@ -102,7 +109,7 @@ pub(crate) fn import_js_minified(scriptfile: String) -> String {
102109
if output.status.success() {
103110
let res: String = String::from_utf8_lossy(&output.stdout).parse().unwrap();
104111
cacheplacer(scriptfile, format!(
105-
"\n\r// Minified internally by Cynthia using Terser\n\n{res}\n\n\r// Cached after minifying, so might be ~20 minutes behind.\n\r"
112+
"\n\r// Minified internally by Cynthia using Terser\n\n{res}\n\n\r// Cached after minifying, so might be somewhat behind.\n\r"
106113
))
107114
} else {
108115
logger(
@@ -145,7 +152,7 @@ pub(crate) fn import_js_minified(scriptfile: String) -> String {
145152
} else {
146153
let res: String = String::from_utf8_lossy(&output.stdout).parse().unwrap();
147154
cacheplacer(scriptfile, format!(
148-
"\n\r// Minified internally by Cynthia using Terser\n\n{res}\n\n\r// Cached after minifying, so might be ~20 minutes behind.\n\r"
155+
"\n\r// Minified internally by Cynthia using Terser\n\n{res}\n\n\r// Cached after minifying, so might be somewhat behind.\n\r"
149156
))
150157
}
151158
}
@@ -158,7 +165,11 @@ pub(crate) fn import_js_minified(scriptfile: String) -> String {
158165
}
159166

160167
pub(crate) fn import_css_minified(stylefile: String) -> String {
161-
return match cacheretriever(stylefile.to_string(), 1200) {
168+
let csscachelifetime: u64 = match std::env::var("STYLESHEET_CACHE_LIFETIME") {
169+
Ok(g) => g.parse::<u64>().unwrap(),
170+
Err(_) => 1200,
171+
};
172+
return match cacheretriever(stylefile.to_string(), csscachelifetime) {
162173
Ok(o) => fs::read_to_string(o).expect("Couldn't find or open a JS file."),
163174
Err(_) => match jsruntime(true) {
164175
BUNJSR => {
@@ -181,7 +192,7 @@ pub(crate) fn import_css_minified(stylefile: String) -> String {
181192
if output.status.success() {
182193
let res: String = String::from_utf8_lossy(&output.stdout).parse().unwrap();
183194
cacheplacer(stylefile, format!(
184-
"\n\r/* Minified internally by Cynthia using clean-css */\n\n{res}\n\n\r/* Cached after minifying, so might be ~20 minutes behind. */\n\r"
195+
"\n\r/* Minified internally by Cynthia using clean-css */\n\n{res}\n\n\r/* Cached after minifying, so might be somewhat behind. */\n\r"
185196
))
186197
} else {
187198
logger(
@@ -224,7 +235,7 @@ pub(crate) fn import_css_minified(stylefile: String) -> String {
224235
} else {
225236
let res: String = String::from_utf8_lossy(&output.stdout).parse().unwrap();
226237
cacheplacer(stylefile, format!(
227-
"\n\r/* Minified internally by Cynthia using clean-css */\n\n{res}\n\n\r/* Cached after minifying, so might be ~20 minutes behind. */\n\r"
238+
"\n\r/* Minified internally by Cynthia using clean-css */\n\n{res}\n\n\r/* Cached after minifying, so might be somewhat behind. */\n\r"
228239
))
229240
}
230241
}

src/logger.rs

+11
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ pub(crate) fn logger(act: i32, msg: String) {
9595
let preq = format!("{0}{2}{1}", title, " ".repeat(spaceleft), tabs);
9696
println!("{0}❕ {1}", preq, msg.bright_green());
9797
}
98+
31 => {
99+
let name = format!("[{} - [CACHE]", times);
100+
let spaceleft = if name.chars().count() < spaces {
101+
spaces - name.chars().count()
102+
} else {
103+
0
104+
};
105+
let title = format!("{}", name.white());
106+
let preq = format!("{0}{2}{1}", title, " ".repeat(spaceleft), tabs);
107+
println!("{0}♻️ {1}", preq, msg.bright_white().italic());
108+
}
98109
_ => {
99110
let name = format!("[{} - [LOG]", times).blue();
100111
let spaceleft = if name.chars().count() < spaces {

0 commit comments

Comments
 (0)