feat: add archive base_path in settings
This commit is contained in:
parent
ca0c847f04
commit
a44f6f1748
2 changed files with 29 additions and 2 deletions
|
@ -34,6 +34,9 @@ password = "password"
|
||||||
name = "postgres"
|
name = "postgres"
|
||||||
pool = 4
|
pool = 4
|
||||||
|
|
||||||
|
[archive]
|
||||||
|
base_path = "/tmp/mcaptcha-survey"
|
||||||
|
|
||||||
[footer]
|
[footer]
|
||||||
about = "https://mcapthca.org/about"
|
about = "https://mcapthca.org/about"
|
||||||
donate = "https://mcapthca.org/donate"
|
donate = "https://mcapthca.org/donate"
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::fs;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use config::{Config, ConfigError, Environment, File};
|
use config::{Config, ConfigError, Environment, File};
|
||||||
|
@ -84,6 +85,25 @@ pub struct Footer {
|
||||||
pub thanks: Url,
|
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)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
pub debug: bool,
|
pub debug: bool,
|
||||||
|
@ -94,6 +114,7 @@ pub struct Settings {
|
||||||
pub support_email: String,
|
pub support_email: String,
|
||||||
pub default_campaign: String,
|
pub default_campaign: String,
|
||||||
pub footer: Footer,
|
pub footer: Footer,
|
||||||
|
pub archive: Archive,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(tarpaulin_include))]
|
#[cfg(not(tarpaulin_include))]
|
||||||
|
@ -143,8 +164,11 @@ impl Settings {
|
||||||
|
|
||||||
set_database_url(&mut s);
|
set_database_url(&mut s);
|
||||||
|
|
||||||
match s.try_into() {
|
match s.try_into::<Settings>() {
|
||||||
Ok(val) => Ok(val),
|
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))),
|
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))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue