#![allow(clippy::result_large_err)]
use figment::providers::{Env, Format, Serialized, Toml};
use figment::{Figment, Profile};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ServerConfig {
pub host: String,
pub port: u16,
}
impl Default for ServerConfig {
fn default() -> Self {
Self {
host: "127.0.0.1".to_string(),
port: 3000,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct LambdaConfig {
pub memory_size: u32,
pub timeout_seconds: u32,
}
impl Default for LambdaConfig {
fn default() -> Self {
Self {
memory_size: 128,
timeout_seconds: 30,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TelemetryConfig {
pub log_level: String,
pub format: String,
}
impl Default for TelemetryConfig {
fn default() -> Self {
Self {
log_level: "info".to_string(),
format: "json".to_string(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct OpenApiConfig {
pub title: String,
pub version: String,
pub docs_path: String,
pub redoc_path: String,
}
impl Default for OpenApiConfig {
fn default() -> Self {
Self {
title: "serverust".to_string(),
version: "0.1.0".to_string(),
docs_path: "/docs".to_string(),
redoc_path: "/redoc".to_string(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct ServerustConfig {
pub server: ServerConfig,
pub lambda: LambdaConfig,
pub telemetry: TelemetryConfig,
pub openapi: OpenApiConfig,
}
impl ServerustConfig {
pub fn load() -> Result<Self, figment::Error> {
Self::load_for_profile(Profile::Default)
}
pub fn load_for_profile(profile: impl Into<Profile>) -> Result<Self, figment::Error> {
Self::load_from_for_profile("serverust.toml", profile)
}
pub fn load_from(path: &str) -> Result<Self, figment::Error> {
Self::load_from_for_profile(path, Profile::Default)
}
pub fn load_from_for_profile(
path: &str,
profile: impl Into<Profile>,
) -> Result<Self, figment::Error> {
Figment::new()
.merge(Serialized::defaults(Self::default()))
.merge(Toml::file(path).nested())
.merge(Env::prefixed("SERVERUST_").split("__"))
.select(profile)
.extract()
}
}