Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit 99f17aa8 authored by peterrabbit's avatar peterrabbit
Browse files

reorganized file structure

parent 07751f86
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,8 @@ ...@@ -6,7 +6,8 @@
"description": "A website for Pijar", "description": "A website for Pijar",
"image": "https://pijar.com/static/images/pijar_pic.png", "image": "https://pijar.com/static/images/pijar_pic.png",
"css": [], "css": [],
"js": [] "js": [],
"url_slug": ""
}, },
"body": [ "body": [
{ {
...@@ -30,7 +31,8 @@ ...@@ -30,7 +31,8 @@
} }
] ]
} }
] ],
"sub_pages": []
}, },
"assets_index": { "assets_index": {
"images": [ "images": [
......
No preview for this file type
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(Clone, StructOpt)]
pub struct AppArgs {
#[structopt(short = "c", long = "ctx", default_value = "debug")]
pub context: String,
#[structopt(short = "d", long = "dir")]
pub app_storage_root: Option<PathBuf>,
#[structopt(long)]
pub load: Option<PathBuf>,
#[structopt(short, long, default_value = "localhost")]
pub host: String,
#[structopt(short, long, default_value = "8080")]
pub port: u16,
#[structopt(long = "ptls", default_value = "8443")]
pub port_tls: u16,
#[structopt(long = "certs_dir", default_value = "/etc/letsencrypt/live")]
pub ssl_certs_dir: PathBuf,
}
use crate::app::AppArgs;
use std::path::PathBuf; use std::path::PathBuf;
use structopt::StructOpt; use structopt::StructOpt;
...@@ -7,30 +8,6 @@ pub enum AppContext { ...@@ -7,30 +8,6 @@ pub enum AppContext {
Production, Production,
} }
#[derive(Clone, StructOpt)]
pub struct AppArgs {
#[structopt(short = "c", long = "ctx", default_value = "debug")]
pub context: String,
#[structopt(short = "d", long = "dir")]
pub app_storage_root: Option<PathBuf>,
#[structopt(long)]
pub load: Option<PathBuf>,
#[structopt(short, long, default_value = "localhost")]
pub host: String,
#[structopt(short, long, default_value = "8080")]
pub port: u16,
#[structopt(long = "ptls", default_value = "8443")]
pub port_tls: u16,
#[structopt(long = "certs_dir", default_value = "/etc/letsencrypt/live")]
pub ssl_certs_dir: PathBuf,
}
#[derive(Clone)] #[derive(Clone)]
pub struct AppConfig { pub struct AppConfig {
pub exec_path: PathBuf, pub exec_path: PathBuf,
...@@ -91,23 +68,3 @@ impl AppConfig { ...@@ -91,23 +68,3 @@ impl AppConfig {
} }
} }
} }
#[derive(Clone)]
pub struct AppState {
pub config: AppConfig,
pub preferences: AppPreference,
// authentication
// ...
}
#[derive(Clone)]
pub struct AppPreference {}
impl AppState {
pub fn new() -> Self {
AppState {
config: AppConfig::new(),
preferences: AppPreference {},
}
}
}
mod args;
mod config;
mod preferences;
mod state;
pub use args::*;
pub use config::*;
pub use preferences::*;
pub use state::*;
#[derive(Clone)]
pub struct AppPreferences {}
use crate::app::{AppConfig, AppPreferences};
#[derive(Clone)]
pub struct AppState {
pub config: AppConfig,
pub preferences: AppPreferences,
// authentication
// ...
}
impl AppState {
pub fn new() -> Self {
AppState {
config: AppConfig::new(),
preferences: AppPreferences {},
}
}
}
mod app_state; mod app;
mod service; mod service;
mod static_files; mod static_files;
mod testing; mod testing;
...@@ -6,7 +6,7 @@ mod tls_config; ...@@ -6,7 +6,7 @@ mod tls_config;
mod website; mod website;
use actix_web::{App, HttpServer}; use actix_web::{App, HttpServer};
use actix_web_lab::middleware::RedirectHttps; use actix_web_lab::middleware::RedirectHttps;
use app_state::AppState; use app::AppState;
use static_files::StaticFilesManager; use static_files::StaticFilesManager;
use tls_config::tls_config; use tls_config::tls_config;
use website::WebSite; use website::WebSite;
......
File moved
use crate::app_state::AppState; use crate::app::AppState;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
pub struct StaticFilesManager { pub struct StaticFilesManager {
......
use crate::app_state::AppConfig; use crate::app::AppConfig;
pub fn tls_config(app_config: &AppConfig) -> rustls::ServerConfig { pub fn tls_config(app_config: &AppConfig) -> rustls::ServerConfig {
let certs_dir = app_config.ssl_certs_dir.clone(); let certs_dir = app_config.ssl_certs_dir.clone();
......
use crate::website::page::PageData;
use regex::{Captures, Regex};
use serde::{Deserialize, Serialize};
pub const HTML_DOC_TEMPLATE: &'static str = "
<html lang='{lang}'>
<head>
<meta charset='UTF-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<meta name='description' content='{description}'>
<title>{title}</title>
<link rel='stylesheet' href='{css}'>
</head>
<body>
{body}
</body>
<script src='{js}'></script>
</html>
";
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct HtmlDoc(String);
impl HtmlDoc {
pub fn from_page_data(page_data: &PageData) -> Self {
let re = Regex::new(r#"\{[a-z]+\}"#).unwrap();
let html = re
.replace_all(HTML_DOC_TEMPLATE, |captures: &Captures| {
let placeholder = captures.iter().next().unwrap().unwrap().as_str();
let placeholder = placeholder[1..placeholder.len() - 1].to_owned();
page_data.field_from_str_key(placeholder)
})
.to_string();
HtmlDoc(html)
}
pub fn to_string(&self) -> String {
self.0.clone()
}
}
mod html;
mod page;
mod website;
pub use website::*;
use crate::website::html::HtmlDoc;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PageData {
pub title: String,
pub lang: String,
pub description: String,
pub slug: String,
pub html_body: String,
pub css_src: Option<String>,
pub js_src: Option<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct WebPage {
pub page_data: PageData,
pub html_doc: HtmlDoc,
}
impl PageData {
pub fn to_web_page(&self) -> WebPage {
WebPage {
page_data: self.clone(),
html_doc: HtmlDoc::from_page_data(&self),
}
}
pub fn field_from_str_key(&self, key: String) -> String {
match &key[..] {
"title" => self.title.to_owned(),
"lang" => self.lang.to_owned(),
"description" => self.description.to_owned(),
"slug" => self.slug.to_owned(),
"body" => self.html_body.to_owned(),
"css" => self.css_src.as_ref().unwrap_or(&String::new()).to_owned(),
"js" => self.js_src.as_ref().unwrap_or(&String::new()).to_owned(),
_ => String::new(),
}
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PagesTree {
pub page_data: PageData,
pub sub_pages: Option<Vec<PagesTree>>,
}
use crate::app_state::AppConfig; use crate::app::AppConfig;
use regex::{Captures, Regex}; use crate::website::page::{PagesTree, WebPage};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use std::path::PathBuf; use std::path::PathBuf;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PageData {
pub title: String,
pub lang: String,
pub description: String,
pub slug: String,
pub html_body: String,
pub css_src: Option<String>,
pub js_src: Option<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct HtmlDoc(String);
impl HtmlDoc {
fn load_template() -> String {
std::fs::read_to_string(
std::env::current_dir()
.unwrap()
.join("templates")
.join("html_doc.html"),
)
.expect("Missing html_doc template")
}
pub fn from_page_data(page_data: &PageData) -> Self {
let re = Regex::new(r#"\{[a-z]+\}"#).unwrap();
let html = re
.replace_all(&HtmlDoc::load_template(), |captures: &Captures| {
let placeholder = captures.iter().next().unwrap().unwrap().as_str();
let placeholder = placeholder[1..placeholder.len() - 1].to_owned();
page_data.field_from_str_key(placeholder)
})
.to_string();
HtmlDoc(html)
}
pub fn to_string(&self) -> String {
self.0.clone()
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct WebPage {
pub page_data: PageData,
pub html_doc: HtmlDoc,
}
impl PageData {
pub fn to_web_page(&self) -> WebPage {
WebPage {
page_data: self.clone(),
html_doc: HtmlDoc::from_page_data(&self),
}
}
pub fn field_from_str_key(&self, key: String) -> String {
match &key[..] {
"title" => self.title.to_owned(),
"lang" => self.lang.to_owned(),
"description" => self.description.to_owned(),
"slug" => self.slug.to_owned(),
"body" => self.html_body.to_owned(),
"css" => self.css_src.as_ref().unwrap_or(&String::new()).to_owned(),
"js" => self.js_src.as_ref().unwrap_or(&String::new()).to_owned(),
_ => String::new(),
}
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PagesTree {
pub page_data: PageData,
pub sub_pages: Option<Vec<PagesTree>>,
}
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct WebSite { pub struct WebSite {
pages_tree: PagesTree, pages_tree: PagesTree,
......
<html lang="{lang}">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="{description}">
<title>{title}</title>
<link rel="stylesheet" href="{css}">
</head>
<body>
{body}
</body>
<script src="{js}"></script>
</html>
\ No newline at end of file
...@@ -6,35 +6,5 @@ ...@@ -6,35 +6,5 @@
"description": "A new website", "description": "A new website",
"html_body": "<h1>New Website</h1>" "html_body": "<h1>New Website</h1>"
}, },
"sub_pages": [ "sub_pages": []
{
"page_data": {
"title": "A sub page",
"lang": "en",
"slug": "subpage",
"description": "A sub page of the new web site",
"html_body": "<h1>A sub page</h1>"
}
},
{
"page_data": {
"title": "Some other page",
"lang": "en",
"slug": "otherpage",
"description": "Some other page of the new web site",
"html_body": "<h1>Another page</h1>"
},
"sub_pages": [
{
"page_data": {
"title": "A sub page of the other page",
"lang": "en",
"slug": "othersubpage",
"description": "A sub page of the other page of the new web site",
"html_body": "<h1>A subpage</h1>"
}
}
]
}
]
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment