add comments
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending

This commit is contained in:
Mylloon 2023-04-21 19:47:47 +02:00
parent 2ca24f020e
commit f2359be6a3
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 29 additions and 3 deletions

View file

@ -7,17 +7,25 @@ use std::{fs::File, io::Write, path::Path};
use crate::template::Template; use crate::template::Template;
/// Store the configuration of config/config.toml
#[derive(Deserialize, Clone, Default)] #[derive(Deserialize, Clone, Default)]
pub struct FileConfig { pub struct FileConfig {
/// http/https
pub scheme: Option<String>, pub scheme: Option<String>,
/// Port used
pub port: Option<u16>, pub port: Option<u16>,
/// Mail of owner
pub mail: Option<String>, pub mail: Option<String>,
/// Lang used
pub lang: Option<String>, pub lang: Option<String>,
/// Adress .onion for Tor
pub onion: Option<String>, pub onion: Option<String>,
/// App name
pub app_name: Option<String>, pub app_name: Option<String>,
} }
impl FileConfig { impl FileConfig {
/// Initialize with default values
fn new() -> Self { fn new() -> Self {
Self { Self {
scheme: Some("http".to_owned()), scheme: Some("http".to_owned()),
@ -27,6 +35,7 @@ impl FileConfig {
} }
} }
/// Complete default structure with an existing one
fn complete(a: Self) -> Self { fn complete(a: Self) -> Self {
// Default config // Default config
let d = FileConfig::new(); let d = FileConfig::new();
@ -51,13 +60,18 @@ impl FileConfig {
} }
} }
/// Configuration used internally in the app
#[derive(Clone)] #[derive(Clone)]
pub struct Config { pub struct Config {
/// Information given in the config file
pub fc: FileConfig, pub fc: FileConfig,
/// Location where the static files are stored
pub static_location: String, pub static_location: String,
/// Informations about templates
pub tmpl: Template, pub tmpl: Template,
} }
/// Load the config file
fn get_file_config(file_path: &str) -> FileConfig { fn get_file_config(file_path: &str) -> FileConfig {
match fs::read_to_string(file_path) { match fs::read_to_string(file_path) {
Ok(file) => match toml::from_str(&file) { Ok(file) => match toml::from_str(&file) {
@ -66,14 +80,14 @@ fn get_file_config(file_path: &str) -> FileConfig {
panic!("Error in config file: {file_error}"); panic!("Error in config file: {file_error}");
} }
}, },
Err(_) => Err(_) => {
// No config file // No config file
{
FileConfig::new() FileConfig::new()
} }
} }
} }
/// Build the configuration
pub fn get_config(file_path: &str) -> Config { pub fn get_config(file_path: &str) -> Config {
let internal_config = get_file_config(file_path); let internal_config = get_file_config(file_path);
@ -91,6 +105,7 @@ pub fn get_config(file_path: &str) -> Config {
} }
} }
/// Preparation before running the http server
fn init(dist_dir: String, static_dir: String, templates_dir: String) -> String { fn init(dist_dir: String, static_dir: String, templates_dir: String) -> String {
// The static folder is minimized only in release mode // The static folder is minimized only in release mode
if cfg!(debug_assertions) { if cfg!(debug_assertions) {
@ -127,6 +142,7 @@ fn init(dist_dir: String, static_dir: String, templates_dir: String) -> String {
} }
} }
/// Minify some assets for production
fn minify_and_copy(cfg: &Cfg, path: PathBuf, path_with_dist: String) { fn minify_and_copy(cfg: &Cfg, path: PathBuf, path_with_dist: String) {
// Create folders // Create folders
let new_path = Path::new(&path_with_dist); let new_path = Path::new(&path_with_dist);

View file

@ -1,11 +1,15 @@
use ramhorns::{Content, Ramhorns}; use ramhorns::{Content, Ramhorns};
/// Structure used in the config variable of the app
#[derive(Clone)] #[derive(Clone)]
pub struct Template { pub struct Template {
/// Root directory where templates are stored
pub directory: String, pub directory: String,
/// App name
pub app_name: String, pub app_name: String,
} }
/// Structure used by /routes/*.rs
#[derive(Default)] #[derive(Default)]
pub struct Infos { pub struct Infos {
/// Title /// Title
@ -16,12 +20,18 @@ pub struct Infos {
pub page_kw: Option<String>, pub page_kw: Option<String>,
} }
/// Final structure given to template
#[derive(Content)] #[derive(Content)]
struct Data<T> { struct Data<T> {
/// App name
app_name: String, app_name: String,
/// Title of the page
page_title: Option<String>, page_title: Option<String>,
/// Description of the page
page_desc: Option<String>, page_desc: Option<String>,
/// Keywords of the the page
page_kw: Option<String>, page_kw: Option<String>,
/// Data needed to render the page
data: T, data: T,
} }