feat: use librepages/libconfig for handling deployment configurations

This commit is contained in:
Aravinth Manivannan 2022-12-09 14:24:02 +05:30
parent 538bc41113
commit 5c0f6fd84d
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
4 changed files with 369 additions and 378 deletions

598
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -21,6 +21,7 @@ actix-web-codegen-const-routes = { version = "0.1.0", tag = "0.1.0", git = "http
argon2-creds = { branch = "master", git = "https://github.com/realaravinth/argon2-creds"} argon2-creds = { branch = "master", git = "https://github.com/realaravinth/argon2-creds"}
sqlx = { version = "0.6.1", features = ["runtime-actix-rustls", "postgres", "time", "offline", "json", "uuid"] } sqlx = { version = "0.6.1", features = ["runtime-actix-rustls", "postgres", "time", "offline", "json", "uuid"] }
clap = { version = "3.2.20", features = ["derive"]} clap = { version = "3.2.20", features = ["derive"]}
libconfig = { version = "0.1.0", git = "https://git.batsense.net/librepages/libconfig" }
config = "0.13" config = "0.13"
git2 = "0.14.2" git2 = "0.14.2"

View file

@ -61,7 +61,7 @@ impl Ctx {
self.db.add_site(&db_site).await?; self.db.add_site(&db_site).await?;
let page = Page::from_site(&self.settings, db_site); let page = Page::from_site(&self.settings, db_site);
page.update(&page.branch)?; page.update(&page.branch)?;
if let Some(_config) = page_config::Config::load(&page.path, &page.branch) { if let Some(_config) = page_config::load(&page.path, &page.branch) {
unimplemented!(); unimplemented!();
} }
self.db self.db
@ -87,7 +87,7 @@ impl Ctx {
.unwrap(); .unwrap();
} }
rx.await.unwrap()?; rx.await.unwrap()?;
if let Some(_config) = page_config::Config::load(&page.path, &page.branch) { if let Some(_config) = page_config::load(&page.path, &page.branch) {
unimplemented!(); unimplemented!();
} }
self.db self.db

View file

@ -16,41 +16,11 @@
*/ */
use std::path::Path; use std::path::Path;
use libconfig::Config;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::git::{ContentType, GitFileMode}; use crate::git::{ContentType, GitFileMode};
#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone)]
pub struct Config {
pub source: Source,
pub domains: Option<Vec<String>>,
pub forms: Option<Forms>,
pub image_compression: Option<ImageCompression>,
pub redirects: Option<Vec<Redirects>>,
}
#[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone)]
pub struct Source {
production_branch: String,
staging: Option<String>,
}
#[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone)]
pub struct Forms {
pub enable: bool,
}
#[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone)]
pub struct ImageCompression {
pub enable: bool,
}
#[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone)]
pub struct Redirects {
pub from: String,
pub to: String,
}
#[derive(Deserialize, Debug, Serialize, PartialEq, Eq)] #[derive(Deserialize, Debug, Serialize, PartialEq, Eq)]
struct Policy<'a> { struct Policy<'a> {
rel_path: &'a str, rel_path: &'a str,
@ -70,14 +40,13 @@ enum SupportedFormat {
Toml, Toml,
} }
impl Config {
pub fn load<P: AsRef<Path>>(repo_path: &P, branch: &str) -> Option<Config> { pub fn load<P: AsRef<Path>>(repo_path: &P, branch: &str) -> Option<Config> {
const POLICIES: [Policy; 2] = [ const POLICIES: [Policy; 2] = [
Policy::new("librepages.toml", SupportedFormat::Toml), Policy::new("librepages.toml", SupportedFormat::Toml),
Policy::new("librepages.json", SupportedFormat::Json), Policy::new("librepages.json", SupportedFormat::Json),
]; ];
if let Some(policy) = Self::discover(repo_path, branch, &POLICIES) { if let Some(policy) = discover(repo_path, branch, &POLICIES) {
// let path = p.repo.as_ref().join(policy.rel_path); // let path = p.repo.as_ref().join(policy.rel_path);
//let contents = fs::read_to_string(path).await.unwrap(); //let contents = fs::read_to_string(path).await.unwrap();
@ -86,9 +55,9 @@ impl Config {
.unwrap(); .unwrap();
if let ContentType::Text(contents) = file.content { if let ContentType::Text(contents) = file.content {
let res = match policy.format { let res = match policy.format {
SupportedFormat::Json => Self::load_json(&contents), SupportedFormat::Json => load_json(&contents),
SupportedFormat::Yaml => Self::load_yaml(&contents), SupportedFormat::Yaml => load_yaml(&contents),
SupportedFormat::Toml => Self::load_toml(&contents), SupportedFormat::Toml => load_toml(&contents),
}; };
return Some(res); return Some(res);
@ -141,7 +110,6 @@ impl Config {
fn load_json(c: &str) -> Config { fn load_json(c: &str) -> Config {
serde_json::from_str(c).unwrap() serde_json::from_str(c).unwrap()
} }
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -149,6 +117,8 @@ mod tests {
use crate::git::tests::write_file_util; use crate::git::tests::write_file_util;
use mktemp::Temp; use mktemp::Temp;
use libconfig::*;
#[actix_rt::test] #[actix_rt::test]
async fn page_config_test() { async fn page_config_test() {
let tmp_dir = Temp::new_dir().unwrap(); let tmp_dir = Temp::new_dir().unwrap();
@ -167,7 +137,7 @@ mod tests {
Some(&content), Some(&content),
); );
let config = Config::load(&repo_path, "master").unwrap(); let config = load(&repo_path, "master").unwrap();
assert!(config.forms.as_ref().unwrap().enable); assert!(config.forms.as_ref().unwrap().enable);
assert!(config.image_compression.as_ref().unwrap().enable); assert!(config.image_compression.as_ref().unwrap().enable);
assert_eq!(config.source.production_branch, "librepages"); assert_eq!(config.source.production_branch, "librepages");