Skip to content

Commit

Permalink
feat: mise en place config et basique home
Browse files Browse the repository at this point in the history
  • Loading branch information
batleforc committed Aug 4, 2024
1 parent 6209556 commit 3306566
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 2 deletions.
2 changes: 2 additions & 0 deletions apps/back/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ serde = "1.0"
serde_json = "1.0"
serde_yaml = "0.9"
tokio = "1.39"
dotenvy = "0.15.7"
tracing = "0.1.40"

# Code that handle the CLI
[[bin]]
Expand Down
84 changes: 84 additions & 0 deletions apps/back/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::path::PathBuf;

use dotenvy::dotenv;
use serde::Deserialize;
use tool_tracing::tracing_kind::Tracing;
use tracing::info;

const API_PORT: &str = "API_PORT";
const CONTENT_PATH: &str = "CONTENT_PATH";

#[derive(Deserialize, Debug, Clone)]
pub struct Config {
pub port: u16,
pub content_path: String,
pub tracing: Vec<Tracing>,
}

fn override_config_with_env(config: Config) -> Config {
let mut config = config;
if let Ok(port) = std::env::var(API_PORT) {
if let Ok(port) = port.parse::<u16>() {
config.port = port;
}
}
if let Ok(content_path) = std::env::var(CONTENT_PATH) {
config.content_path = content_path;
}
config
}

fn parse_config_from_file(path_buf: PathBuf) -> Config {
let file = std::fs::File::open(path_buf).expect("file should open read only");
let reader = std::io::BufReader::new(file);
serde_yaml::from_reader(reader).expect("file should be proper YAML")
}

pub fn parse_config(path_buf: PathBuf) -> Config {
let config = parse_config_from_file(path_buf);
override_config_with_env(config)
}

pub fn parse_local_config() -> Config {
let mut path_buf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path_buf.push("../../content/config.yaml");
match dotenv() {
Ok(_) => info!("Loaded .env file"),
Err(err) => println!("No .env file found: {:?}", err),
}
parse_config(path_buf)
}

#[allow(dead_code)]
pub fn parse_test_config() -> Config {
let mut path_buf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path_buf.push("../../test_dataset/content/config.yaml");
println!("{:?}", path_buf);
parse_config(path_buf)
}

#[cfg(test)]
mod tests {
use std::env;

use super::*;

#[test]
fn test_parse_config() {
let config = parse_test_config();
assert_eq!(config.port, 5437);
assert_eq!(config.content_path, "./content");
let config = Config {
port: 8080,
content_path: "content".to_string(),
tracing: vec![],
};
env::set_var(API_PORT, "8081");
env::set_var(CONTENT_PATH, "content2");
let config = override_config_with_env(config);
assert_eq!(config.port, 8081);
assert_eq!(config.content_path, "content2");
let _local = parse_local_config();
assert!(true);
}
}
1 change: 1 addition & 0 deletions apps/back/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod config;
pub mod homeprofil;
6 changes: 6 additions & 0 deletions content/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
port: 5437
content_path: "./content"
tracing:
- name: "console"
level: 2
kind: "Console"
3 changes: 1 addition & 2 deletions libs/markdown_struct/src/folder_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ mod tests {
ressources_dir.push(TEST_FOLDER);
let folder = process_folder_struct(ressources_dir.to_str().unwrap().to_string()).unwrap();
assert_eq!(folder.name, ressources_dir.to_str().unwrap().to_string());
assert_eq!(folder.files.len(), 1);
assert_eq!(folder.files.len(), 3);
assert_eq!(folder.folders.len(), 3);
assert_eq!(folder.files[0].name, "index.md");
}
#[test]
fn test_process_folder_path_invalid() {
Expand Down
1 change: 1 addition & 0 deletions libs/tool_tracing/src/tracing_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct Tracing {
pub kind: TracingKind,
pub name: String,
pub level: VerboseLevel,
#[serde(default)]
pub additional: HashMap<String, String>,
}

Expand Down
6 changes: 6 additions & 0 deletions test_dataset/content/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
port: 5437
content_path: "./content"
tracing:
- name: "console"
level: 2
kind: "Console"
52 changes: 52 additions & 0 deletions test_dataset/content/home.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Maxime Leriche
presentation: |
Hello, Moi c'est Maxime, je suis un développeur passionné autant par les nouvelles technologies qu'un nouveau projet.
J'aime apprendre en permanence et construire ce que j'estime étre ma base de connaissance.
Et vous êtes ici sur mon site qui est un savant mélange de mes passions et de mes projets.
Alors, bienvenue et bonne visite !
coverTitle:
- Développeur
- Passionné
- Ops
- Back
- Front
- Kube and Co
- Architecture
- Too much coffee
- And lot's more
cvUrl: /media/cv.pdf
url:
- name: LinkedIn
url: https://www.linkedin.com/in/maxime-leriche/
primaire: true
imgUrl: /media/linkedin.png
- name: GitHub
url: https://github.com/batleforc
primaire: true
imgUrl: /media/github.png
- name: Git Weebo
url: https://git.weebo.fr/batleforc
primaire: true
imgUrl: /media/gitea.png
history:
- title: Bac STI2D
lieux: Lycée Marie Curie, Nogent sur Oise
date: 2015 - 2018
weight: 1
imgUrl: /media/lycee_marie_curie.jpg
description: |
Bac STI2D option ITEC (Innovation Technologique et Eco-Conception)
Obtenu avec mention Bien
Découverte de la la conception de piéce en 3D et de l'annalyse des contraintes mécaniques
Base importante pour ma passion pour l'impression 3D et la conception de piéce
- title: DUT INFO
lieux: IUT Amiens, Amiens
date: 2018 - 2020
weight: 2
imgUrl: /media/iut_amiens.jpg
url:
- name: Objectif CIAP
url: https://www.youtube.com/watch?v=fZ8DYxclVvQ
description: |
DUT Informatique
Découverte de la programmation et des bases nécessaires pour devenir développeur (Java, C, SQL, C#, JS, Python, PHP, etc)

0 comments on commit 3306566

Please sign in to comment.