config: move from utils to types
This commit is contained in:
parent
58d99806d4
commit
e3a8bf3550
|
@ -269,7 +269,6 @@ checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"block-buffer",
|
"block-buffer",
|
||||||
"crypto-common",
|
"crypto-common",
|
||||||
"subtle",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -496,30 +495,13 @@ version = "0.4.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
|
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]]
|
[[package]]
|
||||||
name = "homedisk-core"
|
name = "homedisk-core"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"better-panic",
|
"better-panic",
|
||||||
"homedisk-server",
|
"homedisk-server",
|
||||||
|
"homedisk-types",
|
||||||
"homedisk-utils",
|
"homedisk-utils",
|
||||||
"log",
|
"log",
|
||||||
"simplelog",
|
"simplelog",
|
||||||
|
@ -544,11 +526,14 @@ dependencies = [
|
||||||
name = "homedisk-types"
|
name = "homedisk-types"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
"axum",
|
"axum",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
"dirs",
|
||||||
"jsonwebtoken",
|
"jsonwebtoken",
|
||||||
"serde",
|
"serde",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
|
"toml",
|
||||||
"uuid",
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -556,8 +541,6 @@ dependencies = [
|
||||||
name = "homedisk-utils"
|
name = "homedisk-utils"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
|
||||||
"dirs",
|
|
||||||
"hex",
|
"hex",
|
||||||
"log",
|
"log",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -565,7 +548,6 @@ dependencies = [
|
||||||
"sha2",
|
"sha2",
|
||||||
"sqlx",
|
"sqlx",
|
||||||
"tokio",
|
"tokio",
|
||||||
"toml",
|
|
||||||
"uuid",
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -755,15 +737,6 @@ version = "0.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "73cbba799671b762df5a175adf59ce145165747bb891505c43d09aefbbf38beb"
|
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]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
version = "2.4.1"
|
version = "2.4.1"
|
||||||
|
@ -1005,12 +978,6 @@ version = "0.3.25"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
|
checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "ppv-lite86"
|
|
||||||
version = "0.2.16"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.37"
|
version = "1.0.37"
|
||||||
|
@ -1044,18 +1011,6 @@ version = "0.8.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||||
dependencies = [
|
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",
|
"rand_core",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -1322,13 +1277,11 @@ checksum = "e48c61941ccf5ddcada342cd59e3e5173b007c509e1e8e990dafc830294d9dc5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ahash",
|
"ahash",
|
||||||
"atoi",
|
"atoi",
|
||||||
"base64",
|
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"bytes",
|
"bytes",
|
||||||
"crc",
|
"crc",
|
||||||
"crossbeam-queue",
|
"crossbeam-queue",
|
||||||
"dirs",
|
|
||||||
"either",
|
"either",
|
||||||
"event-listener",
|
"event-listener",
|
||||||
"flume",
|
"flume",
|
||||||
|
@ -1339,23 +1292,16 @@ dependencies = [
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"hashlink",
|
"hashlink",
|
||||||
"hex",
|
"hex",
|
||||||
"hkdf",
|
|
||||||
"hmac",
|
|
||||||
"indexmap",
|
"indexmap",
|
||||||
"itoa",
|
"itoa",
|
||||||
"libc",
|
"libc",
|
||||||
"libsqlite3-sys",
|
"libsqlite3-sys",
|
||||||
"log",
|
"log",
|
||||||
"md-5",
|
|
||||||
"memchr",
|
"memchr",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"paste",
|
"paste",
|
||||||
"percent-encoding",
|
"percent-encoding",
|
||||||
"rand",
|
|
||||||
"rustls",
|
"rustls",
|
||||||
"serde",
|
|
||||||
"serde_json",
|
|
||||||
"sha-1",
|
|
||||||
"sha2",
|
"sha2",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"sqlformat",
|
"sqlformat",
|
||||||
|
@ -1366,7 +1312,6 @@ dependencies = [
|
||||||
"url",
|
"url",
|
||||||
"webpki",
|
"webpki",
|
||||||
"webpki-roots",
|
"webpki-roots",
|
||||||
"whoami",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1409,12 +1354,6 @@ dependencies = [
|
||||||
"unicode-normalization",
|
"unicode-normalization",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "subtle"
|
|
||||||
version = "2.4.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "1.0.91"
|
version = "1.0.91"
|
||||||
|
@ -1862,16 +1801,6 @@ dependencies = [
|
||||||
"webpki",
|
"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]]
|
[[package]]
|
||||||
name = "winapi"
|
name = "winapi"
|
||||||
version = "0.3.9"
|
version = "0.3.9"
|
||||||
|
|
|
@ -31,3 +31,7 @@ features = ["full"]
|
||||||
|
|
||||||
[dependencies.homedisk-server]
|
[dependencies.homedisk-server]
|
||||||
path = "../server"
|
path = "../server"
|
||||||
|
|
||||||
|
[dependencies.homedisk-types]
|
||||||
|
path = "../types"
|
||||||
|
features = ["config"]
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
mod init;
|
mod init;
|
||||||
|
|
||||||
use homedisk_utils::{config::Config, database::Database};
|
use homedisk_types::config::types::Config;
|
||||||
|
use homedisk_utils::database::Database;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
use axum::{Extension, Json};
|
use axum::{Extension, Json};
|
||||||
use homedisk_types::{
|
use homedisk_types::{
|
||||||
auth::login::{Request, Response},
|
auth::login::{Request, Response},
|
||||||
|
config::types::Config,
|
||||||
errors::{AuthError, ServerError},
|
errors::{AuthError, ServerError},
|
||||||
token::{Claims, Token},
|
token::{Claims, Token},
|
||||||
};
|
};
|
||||||
use homedisk_utils::{
|
use homedisk_utils::database::{Database, User};
|
||||||
config::Config,
|
|
||||||
database::{Database, User},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub async fn handle(
|
pub async fn handle(
|
||||||
db: Extension<Database>,
|
db: Extension<Database>,
|
||||||
|
|
|
@ -2,7 +2,8 @@ pub mod auth;
|
||||||
mod error;
|
mod error;
|
||||||
|
|
||||||
use axum::{http::HeaderValue, routing::get, Extension, Router, Server};
|
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 log::{debug, info};
|
||||||
use tower_http::cors::{CorsLayer, Origin};
|
use tower_http::cors::{CorsLayer, Origin};
|
||||||
|
|
||||||
|
|
|
@ -5,10 +5,12 @@ edition = "2021"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
token = ["chrono", "jsonwebtoken"]
|
token = ["chrono", "jsonwebtoken"]
|
||||||
|
config = ["toml", "dirs"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
thiserror = "1.0.30"
|
thiserror = "1.0.30"
|
||||||
uuid = "0.8.2"
|
uuid = "0.8.2"
|
||||||
|
anyhow = "1.0.56"
|
||||||
|
|
||||||
[dependencies.serde]
|
[dependencies.serde]
|
||||||
version = "1.0.136"
|
version = "1.0.136"
|
||||||
|
@ -18,6 +20,14 @@ features = ["derive"]
|
||||||
version = "0.5.3"
|
version = "0.5.3"
|
||||||
optional = true
|
optional = true
|
||||||
|
|
||||||
|
# config
|
||||||
|
[dependencies.toml]
|
||||||
|
version = "0.5.9"
|
||||||
|
optional = true
|
||||||
|
[dependencies.dirs]
|
||||||
|
version = "4.0.0"
|
||||||
|
optional = true
|
||||||
|
|
||||||
# token
|
# token
|
||||||
[dependencies.chrono]
|
[dependencies.chrono]
|
||||||
version = "0.4.19"
|
version = "0.4.19"
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
pub mod types;
|
||||||
|
|
||||||
|
#[cfg(feature = "config")]
|
||||||
|
pub mod toml;
|
|
@ -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)?)
|
||||||
|
}
|
||||||
|
}
|
|
@ -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,
|
||||||
|
}
|
|
@ -1,5 +1,7 @@
|
||||||
pub mod auth;
|
pub mod auth;
|
||||||
|
pub mod config;
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
|
pub mod macros;
|
||||||
|
|
||||||
#[cfg(feature = "token")]
|
#[cfg(feature = "token")]
|
||||||
pub mod token;
|
pub mod token;
|
||||||
|
|
|
@ -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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
|
@ -4,9 +4,8 @@ version = "0.0.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
full = ["crypto", "config", "database"]
|
full = ["crypto", "database"]
|
||||||
crypto = ["sha-1", "sha2", "hex"]
|
crypto = ["sha-1", "sha2", "hex"]
|
||||||
config = ["toml", "anyhow", "dirs"]
|
|
||||||
database = ["sqlx", "uuid"]
|
database = ["sqlx", "uuid"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
@ -27,21 +26,10 @@ optional = true
|
||||||
version = "0.4.3"
|
version = "0.4.3"
|
||||||
optional = true
|
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
|
# database
|
||||||
[dependencies.sqlx]
|
[dependencies.sqlx]
|
||||||
version = "0.5.13"
|
version = "0.5.13"
|
||||||
features = ["runtime-tokio-rustls", "postgres", "sqlite"]
|
features = ["runtime-tokio-rustls", "sqlite"]
|
||||||
optional = true
|
optional = true
|
||||||
[dependencies.uuid]
|
[dependencies.uuid]
|
||||||
version = "0.8.2"
|
version = "0.8.2"
|
||||||
|
|
|
@ -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),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,6 +0,0 @@
|
||||||
mod error;
|
|
||||||
mod parser;
|
|
||||||
|
|
||||||
use parser as config;
|
|
||||||
|
|
||||||
pub use {config::*, error::*};
|
|
|
@ -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)?)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,6 +1,3 @@
|
||||||
#[cfg(feature = "config")]
|
|
||||||
pub mod config;
|
|
||||||
|
|
||||||
#[cfg(feature = "crypto")]
|
#[cfg(feature = "crypto")]
|
||||||
pub mod crypto;
|
pub mod crypto;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue