basic config

This commit is contained in:
blek 2023-10-21 01:12:16 +10:00
parent 05a9de1605
commit 26f40621c9
Signed by: blek
GPG Key ID: 14546221E3595D0C
6 changed files with 180 additions and 4 deletions

79
filed/Cargo.lock generated
View File

@ -276,7 +276,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "874c6e2d19f8d4a285083b11a3241bfbe01ac3ed85f26e1e6b34888d960552bd" checksum = "874c6e2d19f8d4a285083b11a3241bfbe01ac3ed85f26e1e6b34888d960552bd"
dependencies = [ dependencies = [
"derive_more", "derive_more",
"indexmap", "indexmap 1.9.3",
"nom", "nom",
] ]
@ -325,6 +325,12 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]] [[package]]
name = "erased-serde" name = "erased-serde"
version = "0.3.31" version = "0.3.31"
@ -374,6 +380,7 @@ dependencies = [
"sha2", "sha2",
"static_dir", "static_dir",
"tokio", "tokio",
"toml",
"urlencoding 2.1.3", "urlencoding 2.1.3",
"warp", "warp",
] ]
@ -486,7 +493,7 @@ dependencies = [
"futures-sink", "futures-sink",
"futures-util", "futures-util",
"http", "http",
"indexmap", "indexmap 1.9.3",
"slab", "slab",
"tokio", "tokio",
"tokio-util", "tokio-util",
@ -509,6 +516,12 @@ dependencies = [
"bumpalo", "bumpalo",
] ]
[[package]]
name = "hashbrown"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f93e7192158dbcda357bdec5fb5788eebf8bbac027f3f33e719d29135ae84156"
[[package]] [[package]]
name = "headers" name = "headers"
version = "0.3.9" version = "0.3.9"
@ -678,6 +691,16 @@ dependencies = [
"hashbrown 0.12.3", "hashbrown 0.12.3",
] ]
[[package]]
name = "indexmap"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8adf3ddd720272c6ea8bf59463c04e0f93d0bbf7c5439b691bca2987e0270897"
dependencies = [
"equivalent",
"hashbrown 0.14.2",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.9" version = "1.0.9"
@ -1136,6 +1159,15 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_spanned"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96426c9936fd7a0124915f9185ea1d20aa9445cc9821142f0a73bc9207a2e186"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "serde_urlencoded" name = "serde_urlencoded"
version = "0.7.1" version = "0.7.1"
@ -1424,6 +1456,40 @@ dependencies = [
"tracing", "tracing",
] ]
[[package]]
name = "toml"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "185d8ab0dfbb35cf1399a6344d8484209c088f75f8f68230da55d48d95d43e3d"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "396e4d48bbb2b7554c944bde63101b5ae446cff6ec4a24227428f15eb72ef338"
dependencies = [
"indexmap 2.0.2",
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
]
[[package]] [[package]]
name = "tower-service" name = "tower-service"
version = "0.3.2" version = "0.3.2"
@ -1791,3 +1857,12 @@ name = "windows_x86_64_msvc"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "winnow"
version = "0.5.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3b801d0e0a6726477cc207f60162da452f3a95adb368399bef20a946e06f65c"
dependencies = [
"memchr",
]

View File

@ -24,6 +24,7 @@ serde_json = "1.0.107"
sha2 = "0.10.8" sha2 = "0.10.8"
static_dir = "0.2.0" static_dir = "0.2.0"
tokio = { version = "1.32.0", features = ["rt", "macros", "rt-multi-thread"] } tokio = { version = "1.32.0", features = ["rt", "macros", "rt-multi-thread"] }
toml = "0.8.2"
urlencoding = "2.1.3" urlencoding = "2.1.3"
warp = "0.3.6" warp = "0.3.6"

1
filed/src/config.rs Normal file
View File

@ -0,0 +1 @@
pub mod types;

88
filed/src/config/types.rs Normal file
View File

@ -0,0 +1,88 @@
use serde::{Serialize, Deserialize};
use std::{error::Error, fs};
use crate::env::Env;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FilesPolicy {
/// Whether the uploads are enabled
#[serde(default)]
allow_uploads: bool,
/// Allow custom names
#[serde(default)]
allow_custom_names: bool,
/// Allow password protection
#[serde(default)]
allow_pass_protection: bool,
/// Max uploads for IP (doesn't include deleted uploads)
#[serde(default)]
max_per_ip: usize,
/// Default time for file to be deleted
#[serde(default)]
file_del_timeout: usize,
/// Whitelisted file types
#[serde(default)]
type_whitelist: Option<Vec<String>>,
/// Backlisted file types
#[serde(default)]
type_blacklist: Option<Vec<String>>,
/// Instance name
#[serde(default)]
instance_name: String,
/// Instance URL (not the bind URL). Must be Some(...)
#[serde(default)]
instance_url: Option<String>
}
impl Default for FilesPolicy {
fn default() -> Self {
FilesPolicy {
allow_uploads: true,
allow_custom_names: true,
allow_pass_protection: true,
max_per_ip: 8,
file_del_timeout: 1800,
type_whitelist: None,
type_blacklist: None,
instance_name: "blek! File".into(),
instance_url: None
}
}
}
impl FilesPolicy {
pub fn validate(self: &Self) -> Result<(), String> {
if self.instance_url.is_none() {
return Err("Instance url must not be empty!".into());
}
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
files: FilesPolicy
}
impl Config {
pub fn validate(self: &Self) -> Result<(), String> {
self.files.validate()?;
Ok(())
}
pub fn load(env: Env) -> Result<Config, Box<dyn Error>> {
let raw = fs::read_to_string(env.confpath.clone())?;
let conf: Config = toml::from_str(&raw)?;
conf.validate()?;
Ok(conf)
}
}

View File

@ -20,7 +20,8 @@ pub struct Env {
pub redis: Redis, pub redis: Redis,
pub filedir: String, pub filedir: String,
pub instanceurl: String, pub instanceurl: String,
pub uploadspath: String pub uploadspath: String,
pub confpath: String
} }
fn get_var<T: Into<String>, O: From<String>>(name: T) -> Result<O, String> { fn get_var<T: Into<String>, O: From<String>>(name: T) -> Result<O, String> {
@ -55,7 +56,15 @@ pub fn loadenv() -> Result<Env, Box<dyn std::error::Error>> {
spath spath
}, },
instanceurl: get_var("INSTANCE_URL")?, instanceurl: get_var("INSTANCE_URL")?,
uploadspath: get_var("UPLOADS_PATH")? uploadspath: get_var("UPLOADS_PATH")?,
confpath: {
let spath: String = get_var("CONF_FILE").unwrap_or("/etc/filed".into());
let path = Path::new(&spath);
if ! path.is_file() {
return Err(format!("CONF_FILE is {}, which is not a file!", spath).into())
}
spath
}
} }
) )
} }

View File

@ -6,6 +6,7 @@ mod files;
mod env; mod env;
mod web; mod web;
mod db; mod db;
mod config;
pub mod security; pub mod security;
@ -13,6 +14,7 @@ pub mod security;
async fn main() { async fn main() {
dotenvy::dotenv().unwrap(); dotenvy::dotenv().unwrap();
let envy = env::loadenv().map_err(|err| format!("Could not load env: {err}")).unwrap(); let envy = env::loadenv().map_err(|err| format!("Could not load env: {err}")).unwrap();
let conf = config::types::Config::load(envy.clone()).unwrap();
// set up logging // set up logging
if envy.logging { if envy.logging {