config: move from utils to types

This commit is contained in:
MedzikUser 2022-04-19 21:10:36 +02:00
parent 58d99806d4
commit e3a8bf3550
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
16 changed files with 83 additions and 176 deletions

79
Cargo.lock generated
View File

@ -269,7 +269,6 @@ checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506"
dependencies = [
"block-buffer",
"crypto-common",
"subtle",
]
[[package]]
@ -496,30 +495,13 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "hkdf"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "791a029f6b9fc27657f6f188ec6e5e43f6911f6f878e0dc5501396e09809d437"
dependencies = [
"hmac",
]
[[package]]
name = "hmac"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
dependencies = [
"digest",
]
[[package]]
name = "homedisk-core"
version = "0.0.0"
dependencies = [
"better-panic",
"homedisk-server",
"homedisk-types",
"homedisk-utils",
"log",
"simplelog",
@ -544,11 +526,14 @@ dependencies = [
name = "homedisk-types"
version = "0.0.0"
dependencies = [
"anyhow",
"axum",
"chrono",
"dirs",
"jsonwebtoken",
"serde",
"thiserror",
"toml",
"uuid",
]
@ -556,8 +541,6 @@ dependencies = [
name = "homedisk-utils"
version = "0.0.0"
dependencies = [
"anyhow",
"dirs",
"hex",
"log",
"serde",
@ -565,7 +548,6 @@ dependencies = [
"sha2",
"sqlx",
"tokio",
"toml",
"uuid",
]
@ -755,15 +737,6 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73cbba799671b762df5a175adf59ce145165747bb891505c43d09aefbbf38beb"
[[package]]
name = "md-5"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "658646b21e0b72f7866c7038ab086d3d5e1cd6271f060fd37defb241949d0582"
dependencies = [
"digest",
]
[[package]]
name = "memchr"
version = "2.4.1"
@ -1005,12 +978,6 @@ version = "0.3.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
[[package]]
name = "ppv-lite86"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "proc-macro2"
version = "1.0.37"
@ -1044,18 +1011,6 @@ version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
@ -1322,13 +1277,11 @@ checksum = "e48c61941ccf5ddcada342cd59e3e5173b007c509e1e8e990dafc830294d9dc5"
dependencies = [
"ahash",
"atoi",
"base64",
"bitflags",
"byteorder",
"bytes",
"crc",
"crossbeam-queue",
"dirs",
"either",
"event-listener",
"flume",
@ -1339,23 +1292,16 @@ dependencies = [
"futures-util",
"hashlink",
"hex",
"hkdf",
"hmac",
"indexmap",
"itoa",
"libc",
"libsqlite3-sys",
"log",
"md-5",
"memchr",
"once_cell",
"paste",
"percent-encoding",
"rand",
"rustls",
"serde",
"serde_json",
"sha-1",
"sha2",
"smallvec",
"sqlformat",
@ -1366,7 +1312,6 @@ dependencies = [
"url",
"webpki",
"webpki-roots",
"whoami",
]
[[package]]
@ -1409,12 +1354,6 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "subtle"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601"
[[package]]
name = "syn"
version = "1.0.91"
@ -1862,16 +1801,6 @@ dependencies = [
"webpki",
]
[[package]]
name = "whoami"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "524b58fa5a20a2fb3014dd6358b70e6579692a56ef6fce928834e488f42f65e8"
dependencies = [
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "winapi"
version = "0.3.9"

View File

@ -31,3 +31,7 @@ features = ["full"]
[dependencies.homedisk-server]
path = "../server"
[dependencies.homedisk-types]
path = "../types"
features = ["config"]

View File

@ -1,6 +1,7 @@
mod init;
use homedisk_utils::{config::Config, database::Database};
use homedisk_types::config::types::Config;
use homedisk_utils::database::Database;
#[tokio::main]
async fn main() {

View File

@ -1,13 +1,11 @@
use axum::{Extension, Json};
use homedisk_types::{
auth::login::{Request, Response},
config::types::Config,
errors::{AuthError, ServerError},
token::{Claims, Token},
};
use homedisk_utils::{
config::Config,
database::{Database, User},
};
use homedisk_utils::database::{Database, User};
pub async fn handle(
db: Extension<Database>,

View File

@ -2,7 +2,8 @@ pub mod auth;
mod error;
use axum::{http::HeaderValue, routing::get, Extension, Router, Server};
use homedisk_utils::{config::Config, database::Database};
use homedisk_types::config::types::Config;
use homedisk_utils::database::Database;
use log::{debug, info};
use tower_http::cors::{CorsLayer, Origin};

View File

@ -5,10 +5,12 @@ edition = "2021"
[features]
token = ["chrono", "jsonwebtoken"]
config = ["toml", "dirs"]
[dependencies]
thiserror = "1.0.30"
uuid = "0.8.2"
anyhow = "1.0.56"
[dependencies.serde]
version = "1.0.136"
@ -18,6 +20,14 @@ features = ["derive"]
version = "0.5.3"
optional = true
# config
[dependencies.toml]
version = "0.5.9"
optional = true
[dependencies.dirs]
version = "4.0.0"
optional = true
# token
[dependencies.chrono]
version = "0.4.19"

4
types/src/config/mod.rs Normal file
View File

@ -0,0 +1,4 @@
pub mod types;
#[cfg(feature = "config")]
pub mod toml;

20
types/src/config/toml.rs Normal file
View File

@ -0,0 +1,20 @@
use std::fs;
use anyhow::Result;
use crate::option_return;
use super::types::Config;
impl Config {
/// parse configuration file
pub fn parse() -> Result<Config> {
// configuration file path
let config_dir = option_return!(dirs::config_dir(), "s")?;
let config_path = format!("{}/homedisk/config.toml", config_dir.to_string_lossy());
let config = fs::read_to_string(config_path)?;
Ok(toml::from_str(&config)?)
}
}

19
types/src/config/types.rs Normal file
View File

@ -0,0 +1,19 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
pub http: ConfigHTTP,
pub jwt: ConfigJWT,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigHTTP {
pub host: String,
pub port: u16,
pub cors: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigJWT {
pub secret: String,
}

View File

@ -1,5 +1,7 @@
pub mod auth;
pub mod config;
pub mod errors;
pub mod macros;
#[cfg(feature = "token")]
pub mod token;

12
types/src/macros.rs Normal file
View File

@ -0,0 +1,12 @@
#[macro_export]
macro_rules! option_return {
($a:expr,$b:expr) => {
match $a {
Some(x) => Ok(x),
None => {
let err = std::io::Error::new(std::io::ErrorKind::Other, $b);
Err(anyhow::Error::from(err))
}
}
};
}

View File

@ -4,9 +4,8 @@ version = "0.0.0"
edition = "2021"
[features]
full = ["crypto", "config", "database"]
full = ["crypto", "database"]
crypto = ["sha-1", "sha2", "hex"]
config = ["toml", "anyhow", "dirs"]
database = ["sqlx", "uuid"]
[dependencies]
@ -27,21 +26,10 @@ optional = true
version = "0.4.3"
optional = true
# config
[dependencies.toml]
version = "0.5.9"
optional = true
[dependencies.anyhow]
version = "1.0.56"
optional = true
[dependencies.dirs]
version = "4.0.0"
optional = true
# database
[dependencies.sqlx]
version = "0.5.13"
features = ["runtime-tokio-rustls", "postgres", "sqlite"]
features = ["runtime-tokio-rustls", "sqlite"]
optional = true
[dependencies.uuid]
version = "0.8.2"

View File

@ -1,32 +0,0 @@
use std::fmt;
#[derive(Debug)]
pub enum Error {
UnknowConfigDir(),
Io(std::io::Error),
Toml(toml::de::Error),
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::Io(err)
}
}
impl From<toml::de::Error> for Error {
fn from(err: toml::de::Error) -> Self {
Error::Toml(err)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::UnknowConfigDir() => {
write!(f, "failed to lock up a system configuration directory")
}
Error::Io(err) => write!(f, "{}", err),
Error::Toml(err) => write!(f, "{}", err),
}
}
}

View File

@ -1,6 +0,0 @@
mod error;
mod parser;
use parser as config;
pub use {config::*, error::*};

View File

@ -1,40 +0,0 @@
use std::fs;
use serde::{Deserialize, Serialize};
use super::Error;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
pub http: ConfigHTTP,
pub jwt: ConfigJWT,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigHTTP {
pub host: String,
pub port: u16,
pub cors: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigJWT {
pub secret: String,
}
impl Config {
/// parse configuration file
pub fn parse() -> Result<Config, Error> {
// configuration file path
let config_dir = if dirs::config_dir() == None {
return Err(Error::UnknowConfigDir());
} else {
dirs::config_dir().unwrap()
};
let config_path = format!("{}/homedisk/config.toml", config_dir.to_string_lossy());
let config = fs::read_to_string(config_path)?;
Ok(toml::from_str(&config)?)
}
}

View File

@ -1,6 +1,3 @@
#[cfg(feature = "config")]
pub mod config;
#[cfg(feature = "crypto")]
pub mod crypto;