54 lines
1.2 KiB
Rust
54 lines
1.2 KiB
Rust
|
|
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)
|
||
|
|
}
|
||
|
|
}
|