Initial commit
This commit is contained in:
commit
980a85d41b
15 changed files with 3969 additions and 0 deletions
112
src/config.rs
Normal file
112
src/config.rs
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
net::{IpAddr, Ipv4Addr, SocketAddr},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
const CONFIG_FILE: &str = "config.toml";
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Config {
|
||||
//#[serde(default = "Config::default_admin_emails")]
|
||||
//pub admin_emails: Vec<String>,
|
||||
#[serde(default = "Config::default_admin_passwords")]
|
||||
pub admin_passwords: Vec<String>,
|
||||
/// New or edited comments need admin's approval before being public
|
||||
#[serde(default = "Config::default_comment_approve")]
|
||||
pub comment_approve: bool,
|
||||
/// Duration for which a comment can be edited
|
||||
#[serde(default = "Config::default_comment_edit_timeout")]
|
||||
pub comment_edit_timeout: u64,
|
||||
#[serde(default = "Config::default_comment_author_max_len")]
|
||||
pub comment_author_max_len: usize,
|
||||
#[serde(default = "Config::default_comment_email_max_len")]
|
||||
pub comment_email_max_len: usize,
|
||||
#[serde(default = "Config::default_comment_text_max_len")]
|
||||
pub comment_text_max_len: usize,
|
||||
#[serde(default = "Config::default_cookies_https_only")]
|
||||
pub cookies_https_only: bool,
|
||||
#[serde(default = "Config::default_cookies_domain")]
|
||||
pub cookies_domain: Option<String>,
|
||||
#[serde(default = "Config::default_lang")]
|
||||
pub lang: String,
|
||||
#[serde(default = "Config::default_listen")]
|
||||
pub listen: SocketAddr,
|
||||
#[serde(default = "Config::default_root_url")]
|
||||
pub root_url: String,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/*fn default_admin_emails() -> Vec<String> {
|
||||
vec![]
|
||||
}*/
|
||||
fn default_admin_passwords() -> Vec<String> {
|
||||
vec![]
|
||||
}
|
||||
fn default_comment_approve() -> bool {
|
||||
true
|
||||
}
|
||||
fn default_comment_edit_timeout() -> u64 {
|
||||
7 * 86400
|
||||
}
|
||||
fn default_comment_author_max_len() -> usize {
|
||||
64
|
||||
}
|
||||
fn default_comment_email_max_len() -> usize {
|
||||
128
|
||||
}
|
||||
fn default_comment_text_max_len() -> usize {
|
||||
128 * 1024
|
||||
}
|
||||
fn default_cookies_https_only() -> bool {
|
||||
false
|
||||
}
|
||||
fn default_cookies_domain() -> Option<String> {
|
||||
None
|
||||
}
|
||||
fn default_lang() -> String {
|
||||
"en_GB".into()
|
||||
}
|
||||
fn default_listen() -> SocketAddr {
|
||||
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 31720)
|
||||
}
|
||||
fn default_root_url() -> String {
|
||||
"/".into()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
//admin_emails: Self::default_admin_emails(),
|
||||
admin_passwords: Self::default_admin_passwords(),
|
||||
comment_approve: Self::default_comment_approve(),
|
||||
comment_edit_timeout: Self::default_comment_edit_timeout(),
|
||||
comment_author_max_len: Self::default_comment_author_max_len(),
|
||||
comment_email_max_len: Self::default_comment_email_max_len(),
|
||||
comment_text_max_len: Self::default_comment_text_max_len(),
|
||||
cookies_https_only: Self::default_cookies_https_only(),
|
||||
cookies_domain: Self::default_cookies_domain(),
|
||||
lang: Self::default_lang(),
|
||||
listen: Self::default_listen(),
|
||||
root_url: Self::default_root_url(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_config(dir: &Path) -> Config {
|
||||
let path = dir.join(CONFIG_FILE);
|
||||
|
||||
if !path.is_file() {
|
||||
let config = Config::default();
|
||||
std::fs::write(path, toml_edit::easy::to_string_pretty(&config).unwrap())
|
||||
.expect("Cannot write config file");
|
||||
config
|
||||
} else {
|
||||
toml_edit::easy::from_str(
|
||||
std::str::from_utf8(&std::fs::read(path).expect("Cannot read config file"))
|
||||
.expect("Bad encoding in config file"),
|
||||
)
|
||||
.expect("Bad JSON in config file")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue