test with kerkour architecture (https://kerkour.com/rust-web-application-clean-architecture)
This commit is contained in:
53
src/config.rs
Normal file
53
src/config.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use anyhow::Error;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
pub http: Http,
|
||||
pub database: Database,
|
||||
pub graphql: Grapthql,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Http {
|
||||
pub host: String,
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Database {
|
||||
pub url: String,
|
||||
pub pool_size: u32,
|
||||
pub max_lifetime: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Grapthql {
|
||||
pub schema_location: String,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load() -> Result<Self, Error> {
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
let http = envy::prefixed("HTTP_")
|
||||
.from_env::<Http>()
|
||||
.expect("Failed to load http config");
|
||||
|
||||
let database = envy::prefixed("DATABASE_")
|
||||
.from_env::<Database>()
|
||||
.expect("Failed to load database config");
|
||||
|
||||
let graphql = envy::prefixed("GRAPHQL_")
|
||||
.from_env::<Grapthql>()
|
||||
.expect("Failed to load graphql config");
|
||||
|
||||
let config = Self {
|
||||
http,
|
||||
database,
|
||||
graphql,
|
||||
};
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user