mylloon.fr/src/config.rs
2023-04-11 00:57:30 +02:00

159 lines
4.3 KiB
Rust

use serde::Deserialize;
use std::{fs, path::PathBuf};
use glob::glob;
use minify_html::{minify, Cfg};
use std::{fs::File, io::Write, path::Path};
use crate::template::Template;
#[derive(Deserialize, Clone, Default)]
pub struct FileConfig {
pub scheme: Option<String>,
pub port: Option<u16>,
pub mail: Option<String>,
pub lang: Option<String>,
pub onion: Option<String>,
pub app_name: Option<String>,
}
impl FileConfig {
fn new() -> Self {
Self {
scheme: Some("http".to_string()),
port: Some(8080),
app_name: Some("EWP".to_string()),
..FileConfig::default()
}
}
fn complete(a: Self) -> Self {
// Default config
let d = FileConfig::new();
/// Return the default value if nothing is value is none
fn test<T>(val: Option<T>, default: Option<T>) -> Option<T> {
if val.is_some() {
val
} else {
default
}
}
Self {
scheme: test(a.scheme, d.scheme),
port: test(a.port, d.port),
mail: test(a.mail, d.mail),
lang: test(a.lang, d.lang),
onion: test(a.onion, d.onion),
app_name: test(a.app_name, d.app_name),
}
}
}
#[derive(Clone)]
pub struct Config {
pub fc: FileConfig,
pub static_location: String,
pub tmpl: Template,
}
fn get_file_config(file_path: &str) -> FileConfig {
match fs::read_to_string(file_path) {
Ok(file) => match toml::from_str(&file) {
Ok(stored_config) => FileConfig::complete(stored_config),
Err(file_error) => {
panic!("Error in config file: {file_error}");
}
},
Err(_) =>
// No config file
{
FileConfig::new()
}
}
}
pub fn get_config(file_path: &str) -> Config {
let internal_config = get_file_config(file_path);
let static_dir = "static".to_string();
let templates_dir = "templates".to_string();
let files_root = init(
"dist".to_string(),
static_dir.clone(),
templates_dir.clone(),
);
Config {
fc: internal_config,
static_location: format!("{}/{}", files_root, static_dir),
tmpl: Template {
directory: format!("{}/{}", files_root, templates_dir),
},
}
}
fn init(dist_dir: String, static_dir: String, templates_dir: String) -> String {
// The static folder is minimized only in release mode
if cfg!(debug_assertions) {
".".to_string()
} else {
let cfg = Cfg::spec_compliant();
// Static files
for entry in glob(&format!("{static_dir}/**/*.*")).unwrap() {
let path = entry.unwrap();
let path_with_dist = path
.to_string_lossy()
.replace(&static_dir, &format!("{dist_dir}/{static_dir}"));
minify_and_copy(&cfg, path, path_with_dist);
}
// Template files
for entry in glob(&format!("{templates_dir}/**/*.*")).unwrap() {
let path = entry.unwrap();
let path_with_dist = path
.to_string_lossy()
.replace(&templates_dir, &format!("{dist_dir}/{templates_dir}"));
minify_and_copy(&cfg, path, path_with_dist);
}
dist_dir
}
}
fn minify_and_copy(cfg: &Cfg, path: PathBuf, path_with_dist: String) {
// Create folders
let new_path = Path::new(&path_with_dist);
fs::create_dir_all(new_path.parent().unwrap()).unwrap();
let mut copy = true;
if let Some(ext) = path.extension() {
// List of files who should be minified
if ["html", "css", "js", "svg", "webmanifest", "xml"]
.iter()
.any(|item| ext.to_string_lossy().to_lowercase().contains(item))
{
// We won't copy, we'll minify
copy = false;
// Minify
let data = fs::read(&path).unwrap();
let minified = minify(&data, cfg);
// Write files
let file = File::create(&path_with_dist);
file.expect("Error when minify the file")
.write_all(&minified)
.unwrap();
}
}
if copy {
// If no minification is needed
fs::copy(path, path_with_dist).unwrap();
}
}