feat: use librepages/libconfig for handling deployment configurations
This commit is contained in:
parent
538bc41113
commit
5c0f6fd84d
4 changed files with 369 additions and 378 deletions
598
Cargo.lock
generated
598
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -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"}
|
||||
sqlx = { version = "0.6.1", features = ["runtime-actix-rustls", "postgres", "time", "offline", "json", "uuid"] }
|
||||
clap = { version = "3.2.20", features = ["derive"]}
|
||||
libconfig = { version = "0.1.0", git = "https://git.batsense.net/librepages/libconfig" }
|
||||
|
||||
config = "0.13"
|
||||
git2 = "0.14.2"
|
||||
|
|
|
@ -61,7 +61,7 @@ impl Ctx {
|
|||
self.db.add_site(&db_site).await?;
|
||||
let page = Page::from_site(&self.settings, db_site);
|
||||
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!();
|
||||
}
|
||||
self.db
|
||||
|
@ -87,7 +87,7 @@ impl Ctx {
|
|||
.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!();
|
||||
}
|
||||
self.db
|
||||
|
|
|
@ -16,41 +16,11 @@
|
|||
*/
|
||||
use std::path::Path;
|
||||
|
||||
use libconfig::Config;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
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)]
|
||||
struct Policy<'a> {
|
||||
rel_path: &'a str,
|
||||
|
@ -70,14 +40,13 @@ enum SupportedFormat {
|
|||
Toml,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load<P: AsRef<Path>>(repo_path: &P, branch: &str) -> Option<Config> {
|
||||
const POLICIES: [Policy; 2] = [
|
||||
Policy::new("librepages.toml", SupportedFormat::Toml),
|
||||
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 contents = fs::read_to_string(path).await.unwrap();
|
||||
|
||||
|
@ -86,9 +55,9 @@ impl Config {
|
|||
.unwrap();
|
||||
if let ContentType::Text(contents) = file.content {
|
||||
let res = match policy.format {
|
||||
SupportedFormat::Json => Self::load_json(&contents),
|
||||
SupportedFormat::Yaml => Self::load_yaml(&contents),
|
||||
SupportedFormat::Toml => Self::load_toml(&contents),
|
||||
SupportedFormat::Json => load_json(&contents),
|
||||
SupportedFormat::Yaml => load_yaml(&contents),
|
||||
SupportedFormat::Toml => load_toml(&contents),
|
||||
};
|
||||
|
||||
return Some(res);
|
||||
|
@ -141,7 +110,6 @@ impl Config {
|
|||
fn load_json(c: &str) -> Config {
|
||||
serde_json::from_str(c).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
@ -149,6 +117,8 @@ mod tests {
|
|||
use crate::git::tests::write_file_util;
|
||||
use mktemp::Temp;
|
||||
|
||||
use libconfig::*;
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn page_config_test() {
|
||||
let tmp_dir = Temp::new_dir().unwrap();
|
||||
|
@ -167,7 +137,7 @@ mod tests {
|
|||
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.image_compression.as_ref().unwrap().enable);
|
||||
assert_eq!(config.source.production_branch, "librepages");
|
||||
|
|
Loading…
Add table
Reference in a new issue