mirror of
https://codeberg.org/demostf/frontend.git
synced 2026-06-03 10:14:13 +02:00
config from env
This commit is contained in:
parent
e6d7f24e21
commit
652fd43f36
4 changed files with 56 additions and 3 deletions
12
Cargo.lock
generated
12
Cargo.lock
generated
|
|
@ -1074,6 +1074,7 @@ dependencies = [
|
|||
"sea-query",
|
||||
"sea-query-binder",
|
||||
"serde",
|
||||
"serde-env",
|
||||
"sqlx",
|
||||
"steam-openid",
|
||||
"steamid-ng",
|
||||
|
|
@ -3028,6 +3029,17 @@ dependencies = [
|
|||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde-env"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c68119a0846249fd6f4b38561b4b4727dbc4fd9fea074f1253bca7d50440ce58"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"log",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde-wasm-bindgen"
|
||||
version = "0.3.1"
|
||||
|
|
|
|||
|
|
@ -32,3 +32,4 @@ reqwest = "0.11.16"
|
|||
rand = "0.8.5"
|
||||
demostf-build = { path = "./build", version = "*" }
|
||||
include_dir = "0.7.3"
|
||||
serde-env = "0.1.1"
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
use crate::Result;
|
||||
use config::{Environment, File};
|
||||
use serde::Deserialize;
|
||||
use serde_env::from_env;
|
||||
use sqlx::postgres::PgConnectOptions;
|
||||
use sqlx::PgPool;
|
||||
use std::net::IpAddr;
|
||||
|
|
@ -22,6 +23,10 @@ impl Config {
|
|||
|
||||
Ok(s.try_deserialize()?)
|
||||
}
|
||||
|
||||
pub fn env() -> Option<Self> {
|
||||
from_env().ok()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
|
@ -42,7 +47,26 @@ impl DbConfig {
|
|||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub struct RawListen {
|
||||
path: Option<PathBuf>,
|
||||
address: Option<IpAddr>,
|
||||
port: Option<u16>,
|
||||
}
|
||||
|
||||
impl TryFrom<RawListen> for Listen {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(value: RawListen) -> std::result::Result<Self, Self::Error> {
|
||||
match (value.path, value.address, value.port) {
|
||||
(Some(path), None, None) => Ok(Listen::Socket { path }),
|
||||
(None, Some(address), Some(port)) => Ok(Listen::Tcp { address, port }),
|
||||
_ => Err("invalid listen section"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(try_from = "RawListen")]
|
||||
pub enum Listen {
|
||||
Socket { path: PathBuf },
|
||||
Tcp { address: IpAddr, port: u16 },
|
||||
|
|
@ -51,6 +75,16 @@ pub enum Listen {
|
|||
#[derive(Debug, Deserialize)]
|
||||
pub struct SiteConfig {
|
||||
pub url: String,
|
||||
#[serde(default = "default_api")]
|
||||
pub api: String,
|
||||
#[serde(default = "default_maps")]
|
||||
pub maps: String,
|
||||
}
|
||||
|
||||
fn default_api() -> String {
|
||||
"https://api.demos.tf/".into()
|
||||
}
|
||||
|
||||
fn default_maps() -> String {
|
||||
"https://maps.demos.tf/".into()
|
||||
}
|
||||
|
|
|
|||
10
src/main.rs
10
src/main.rs
|
|
@ -79,8 +79,14 @@ async fn main() -> Result<()> {
|
|||
.try_init()
|
||||
.expect("Failed to init tracing");
|
||||
|
||||
let config = args().skip(1).next().expect("no config file provided");
|
||||
let config = Config::load(&config)?;
|
||||
let config = args()
|
||||
.skip(1)
|
||||
.next()
|
||||
.as_deref()
|
||||
.map(Config::load)
|
||||
.transpose()?
|
||||
.or_else(Config::env)
|
||||
.expect("no config file or env provided");
|
||||
let connection = config.database.connect().await?;
|
||||
|
||||
let session_store = MemoryStore::new();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue