feat: add archive base_path in settings

This commit is contained in:
Aravinth Manivannan 2023-02-02 21:54:43 +05:30
parent ca0c847f04
commit a44f6f1748
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
2 changed files with 29 additions and 2 deletions

View file

@ -34,6 +34,9 @@ password = "password"
name = "postgres"
pool = 4
[archive]
base_path = "/tmp/mcaptcha-survey"
[footer]
about = "https://mcapthca.org/about"
donate = "https://mcapthca.org/donate"

View file

@ -15,6 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::env;
use std::fs;
use std::path::Path;
use config::{Config, ConfigError, Environment, File};
@ -84,6 +85,25 @@ pub struct Footer {
pub thanks: Url,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Archive {
pub base_path: String,
}
impl Archive {
fn create_archive_base_path(&self) {
let base_path = Path::new(&self.base_path);
if base_path.exists() {
if !base_path.is_dir() {
fs::remove_file(&base_path).unwrap();
fs::create_dir_all(&base_path).unwrap();
}
} else {
fs::create_dir_all(&base_path).unwrap();
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Settings {
pub debug: bool,
@ -94,6 +114,7 @@ pub struct Settings {
pub support_email: String,
pub default_campaign: String,
pub footer: Footer,
pub archive: Archive,
}
#[cfg(not(tarpaulin_include))]
@ -143,8 +164,11 @@ impl Settings {
set_database_url(&mut s);
match s.try_into() {
Ok(val) => Ok(val),
match s.try_into::<Settings>() {
Ok(val) => {
val.archive.create_archive_base_path();
Ok(val)
},
Err(e) => Err(ConfigError::Message(format!("\n\nError: {}. If it says missing fields, then please refer to https://github.com/mCaptcha/mcaptcha#configuration to learn more about how mcaptcha reads configuration\n\n", e))),
}
}