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;
/// Store the configuration of config/config.toml
#[derive(Deserialize, Clone, Default)]
pub struct FileConfig {
/// http/https
pub scheme: Option<String>,
/// Port used
pub port: Option<u16>,
/// Mail of owner
pub mail: Option<String>,
/// Lang used
pub lang: Option<String>,
/// Adress .onion for Tor
pub onion: Option<String>,
/// App name
pub app_name: Option<String>,
}
impl FileConfig {
/// Initialize with default values
fn new() -> Self {
Self {
scheme: Some("http".to_owned()),
@ -27,6 +35,7 @@ impl FileConfig {
}
}
/// Complete default structure with an existing one
fn complete(a: Self) -> Self {
// Default config
let d = FileConfig::new();
@ -51,13 +60,18 @@ impl FileConfig {
}
}
/// Configuration used internally in the app
#[derive(Clone)]
pub struct Config {
/// Information given in the config file
pub fc: FileConfig,
/// Location where the static files are stored
pub static_location: String,
/// Informations about templates
pub tmpl: Template,
}
/// Load the config file
fn get_file_config(file_path: &str) -> FileConfig {
match fs::read_to_string(file_path) {
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}");
}
},
Err(_) =>
Err(_) => {
// No config file
{
FileConfig::new()
}
}
}
/// Build the configuration
pub fn get_config(file_path: &str) -> Config {
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 {
// The static folder is minimized only in release mode
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) {
// Create folders
let new_path = Path::new(&path_with_dist);

View file

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