-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
98 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# The following is a sample configuration file. | ||
# This will be used to provision a | ||
# managed postgres instance and managed redis instance. | ||
|
||
[database] | ||
name = "" | ||
user = "" | ||
enable_high_availability = false | ||
plan = "free" | ||
version = "11" | ||
|
||
[redis] | ||
plan = "free" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#![allow(unused)] | ||
use rand::distributions::Alphanumeric; | ||
use rand::{thread_rng, Rng}; | ||
use serde::Deserialize; | ||
use std::fs; | ||
use toml; | ||
|
||
use super::db::DatabaseConf; | ||
use super::redis::RedisConf; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct Conf { | ||
pub database: DatabaseConf, | ||
pub redis: RedisConf, | ||
} | ||
|
||
impl Conf { | ||
pub fn generate_random_string(&self, length: usize) -> String { | ||
thread_rng() | ||
.sample_iter(&Alphanumeric) | ||
.take(length) | ||
.map(char::from) | ||
.collect() | ||
} | ||
|
||
pub fn populate_blank_values(config: &mut Conf) { | ||
if config.database.name.as_deref() == Some("") { | ||
config.database.name = Some(format!("db_{}", config.generate_random_string(10))); | ||
} | ||
|
||
if config.database.user.as_deref() == Some("") { | ||
config.database.user = Some(format!("user_{}", config.generate_random_string(10))); | ||
} | ||
} | ||
|
||
pub fn read_configuration_file() -> Self { | ||
let conf_path = "./samples/sample.conf"; | ||
let contents = fs::read_to_string(conf_path) | ||
.expect(format!("Unable to read <{conf_path:?}>").as_str()); | ||
|
||
// Parse config. file. | ||
let mut config: Conf = toml::from_str(&contents).expect("Unable to parse config. file!"); | ||
|
||
// Populate any <black>/"" fields. | ||
Self::populate_blank_values(&mut config); | ||
|
||
//////////////////////// | ||
// Debug logs. | ||
/////////////////////// | ||
// println!("[DEBUG] -> {:?}", config); | ||
|
||
Self { | ||
database: config.database, | ||
redis: config.redis, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#![allow(unused)] | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct DatabaseConf { | ||
pub name: Option<String>, | ||
pub user: Option<String>, | ||
pub enable_high_availability: bool, | ||
pub plan: String, | ||
pub version: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
pub mod prelude; | ||
pub mod config; | ||
pub mod db; | ||
pub mod prelude; | ||
pub mod redis; | ||
|
||
pub use config::*; | ||
pub use db::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#![allow(unused)] | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct RedisConf { | ||
pub plan: String, | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters