variable for data directory path

This commit is contained in:
Mylloon 2024-01-24 11:52:20 +01:00
parent a4af983418
commit 5ce39b0453
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
5 changed files with 41 additions and 22 deletions

View file

@ -69,13 +69,20 @@ impl FileConfig {
} }
} }
// Paths where files are stored
#[derive(Clone, Debug)]
pub struct Locations {
pub static_dir: String,
pub data_dir: String,
}
/// Configuration used internally in the app /// Configuration used internally in the app
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Config { pub struct Config {
/// Information given in the config file /// Information given in the config file
pub fc: FileConfig, pub fc: FileConfig,
/// Location where the static files are stored /// Location where the static files are stored
pub static_location: String, pub locations: Locations,
/// Informations about templates /// Informations about templates
pub tmpl: Template, pub tmpl: Template,
} }
@ -110,7 +117,10 @@ pub fn get_config(file_path: &str) -> Config {
Config { Config {
fc: internal_config.to_owned(), fc: internal_config.to_owned(),
static_location: format!("{}/{}", files_root, static_dir), locations: Locations {
static_dir: format!("{}/{}", files_root, static_dir),
data_dir: String::from("data"),
},
tmpl: Template { tmpl: Template {
directory: format!("{}/{}", files_root, templates_dir), directory: format!("{}/{}", files_root, templates_dir),
app_name: internal_config.app_name.unwrap(), app_name: internal_config.app_name.unwrap(),

View file

@ -60,7 +60,7 @@ async fn main() -> Result<()> {
.service(portfolio::page) .service(portfolio::page)
.service(setup::page) .service(setup::page)
.service(web3::page) .service(web3::page)
.service(Files::new("/", config.static_location.to_owned())) .service(Files::new("/", config.locations.static_dir.to_owned()))
.default_service(web::to(not_found::page)) .default_service(web::to(not_found::page))
}) })
.bind(addr)? .bind(addr)?

View file

@ -42,7 +42,7 @@ struct BlogIndexTemplate {
#[once(time = 60)] #[once(time = 60)]
fn build_index(config: Config) -> String { fn build_index(config: Config) -> String {
let mut posts = get_posts("data/blog"); let mut posts = get_posts(format!("{}/blog", config.locations.data_dir));
// Sort from newest to oldest // Sort from newest to oldest
posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day)); posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day));
@ -81,8 +81,8 @@ struct Post {
impl Post { impl Post {
// Fetch the file content // Fetch the file content
fn fetch_content(&mut self) { fn fetch_content(&mut self, data_dir: &str) {
let blog_dir = "data/blog"; let blog_dir = format!("{}/blog", data_dir);
let ext = ".md"; let ext = ".md";
if let Some(file) = read_file( if let Some(file) = read_file(
@ -102,8 +102,8 @@ impl Hash for Post {
} }
} }
fn get_posts(location: &str) -> Vec<Post> { fn get_posts(location: String) -> Vec<Post> {
let entries = match std::fs::read_dir(location) { let entries = match std::fs::read_dir(&location) {
Ok(res) => res Ok(res) => res
.flatten() .flatten()
.filter(|f| match f.path().extension() { .filter(|f| match f.path().extension() {
@ -190,7 +190,12 @@ async fn page(path: web::Path<(String,)>, config: web::Data<Config>) -> impl Res
fn build_post(file: String, config: Config) -> String { fn build_post(file: String, config: Config) -> String {
let mut post = None; let mut post = None;
let (infos, toc) = get_post(&mut post, file, config.fc.name.unwrap_or_default()); let (infos, toc) = get_post(
&mut post,
file,
config.fc.name.unwrap_or_default(),
config.locations.data_dir,
);
config.tmpl.render( config.tmpl.render(
"blog/post.html", "blog/post.html",
@ -206,8 +211,13 @@ fn build_post(file: String, config: Config) -> String {
) )
} }
fn get_post(post: &mut Option<File>, filename: String, name: String) -> (Infos, String) { fn get_post(
let blog_dir = "data/blog"; post: &mut Option<File>,
filename: String,
name: String,
data_dir: String,
) -> (Infos, String) {
let blog_dir = format!("{}/blog", data_dir);
let ext = ".md"; let ext = ".md";
*post = read_file( *post = read_file(
@ -268,7 +278,7 @@ async fn rss(config: web::Data<Config>) -> impl Responder {
#[once(time = 10800)] // 3h #[once(time = 10800)] // 3h
fn build_rss(config: Config) -> String { fn build_rss(config: Config) -> String {
let mut posts = get_posts("data/blog"); let mut posts = get_posts(format!("{}/blog", config.locations.data_dir));
// Sort from newest to oldest // Sort from newest to oldest
posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day)); posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day));
@ -315,7 +325,7 @@ fn build_rss(config: Config) -> String {
.iter_mut() .iter_mut()
.map(|p| { .map(|p| {
// Get post data // Get post data
p.fetch_content(); p.fetch_content(&config.locations.data_dir);
// Build item // Build item
Item { Item {

View file

@ -38,13 +38,12 @@ struct ContactLink {
} }
#[once(time = 60)] #[once(time = 60)]
fn find_links() -> Vec<ContactLink> { fn find_links(directory: String) -> Vec<ContactLink> {
// TOML file location // TOML filename
let contacts_dir = "data/contacts";
let toml_file = "links.toml"; let toml_file = "links.toml";
// Read the TOML file and parse it // Read the TOML file and parse it
let toml_str = read_to_string(format!("{contacts_dir}/{toml_file}")).unwrap_or_default(); let toml_str = read_to_string(format!("{directory}/{toml_file}")).unwrap_or_default();
let mut redirections = vec![]; let mut redirections = vec![];
match toml::de::from_str::<toml::Value>(&toml_str) { match toml::de::from_str::<toml::Value>(&toml_str) {
@ -74,9 +73,9 @@ fn find_links() -> Vec<ContactLink> {
#[routes] #[routes]
#[get("/{service}")] #[get("/{service}")]
#[get("/{service}/{scope}")] #[get("/{service}/{scope}")]
async fn service_redirection(req: HttpRequest) -> impl Responder { async fn service_redirection(config: web::Data<Config>, req: HttpRequest) -> impl Responder {
let info = req.match_info(); let info = req.match_info();
let link = find_links() let link = find_links(format!("{}/contacts", config.locations.data_dir))
.iter() .iter()
// Find requested service // Find requested service
.filter(|&x| x.service == *info.query("service")) .filter(|&x| x.service == *info.query("service"))
@ -123,7 +122,7 @@ fn remove_paragraphs(list: &mut [File]) {
#[once(time = 60)] #[once(time = 60)]
fn build_page(config: Config) -> String { fn build_page(config: Config) -> String {
let contacts_dir = "data/contacts"; let contacts_dir = format!("{}/contacts", config.locations.data_dir);
let ext = ".md"; let ext = ".md";
let socials_dir = "socials"; let socials_dir = "socials";

View file

@ -29,7 +29,7 @@ struct PortfolioTemplate<'a> {
#[once(time = 60)] #[once(time = 60)]
fn build_page(config: Config) -> String { fn build_page(config: Config) -> String {
let projects_dir = "data/projects"; let projects_dir = format!("{}/projects", config.locations.data_dir);
let ext = ".md"; let ext = ".md";
// Get apps // Get apps
@ -39,7 +39,7 @@ fn build_page(config: Config) -> String {
.collect::<Vec<File>>(); .collect::<Vec<File>>();
let appdata = if apps.is_empty() { let appdata = if apps.is_empty() {
(None, Some(projects_dir)) (None, Some(projects_dir.as_str()))
} else { } else {
(Some(apps), None) (Some(apps), None)
}; };