Skip to content

Commit c1c71d4

Browse files
committed
feat: betterconfig
- load from .env - load from configurable path Signed-off-by: Rachel Powers <[email protected]>
1 parent 0b8db17 commit c1c71d4

File tree

4 files changed

+41
-24
lines changed

4 files changed

+41
-24
lines changed

mcmeta/.env.example

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
MCMETA__BIND_ADDRESS=127.0.0.1:9988
2+
23
MCMETA__STORAGE_FORMAT__TYPE=json
3-
MCMETA__STORAGE_FORMAT__META_DIRECTORY=../meta
4+
MCMETA__STORAGE_FORMAT__META_DIRECTORY=../meta
5+
6+
MCMETA__METADATA__MAX_PARALLEL_FETCH_CONNECTIONS=8
7+
8+
MCMETA__DEBUG_LOG__ENABLE=true
9+
MCMETA__DEBUG_LOG__PATH=./logs
10+
MCMETA__DEBUG_LOG__PREFIX=mcmeta.log
11+
MCMETA__DEBUG_LOG__LEVEL=DEBUG

mcmeta/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
argparse = "0.2.2"
910
axum = "0.6.15"
1011
clap = "4.2.1"
1112
config = "0.13.3"
1213
custom_error = "1.9.2"
14+
dotenv = "0.15.0"
1315
futures = "0.3.28"
1416
git2 = "0.17.0"
1517
hyper = "0.14.25"
@@ -18,6 +20,7 @@ reqwest = { version = "0.11.16", features = ["json"] }
1820
serde = { version = "1.0.160", features = ["derive"] }
1921
serde_json = "1.0.95"
2022
serde_valid = "0.15.0"
23+
serde_with = "2.3.2"
2124
tokio = { version = "1.27.0", features = ["full"] }
2225
tracing = "0.1.37"
2326
tracing-appender = "0.2.2"

mcmeta/src/app_config.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,6 @@ pub struct DebugLogConfig {
2222
pub level: String,
2323
}
2424

25-
impl DebugLogConfig {
26-
pub fn tracing_level(&self) -> tracing::Level {
27-
match self.level.to_uppercase().trim() {
28-
"TRACE" => tracing::Level::TRACE,
29-
"DEBUG" => tracing::Level::DEBUG,
30-
"INFO" => tracing::Level::INFO,
31-
"WARN" => tracing::Level::WARN,
32-
"ERROR" => tracing::Level::ERROR,
33-
_ => tracing::Level::ERROR,
34-
}
35-
}
36-
}
37-
3825
#[derive(Deserialize, Debug)]
3926
pub struct ServerConfig {
4027
pub bind_address: String,
@@ -44,7 +31,7 @@ pub struct ServerConfig {
4431
}
4532

4633
impl ServerConfig {
47-
pub fn from_config() -> Result<Self, MetaMCError> {
34+
pub fn from_config(path: &str) -> Result<Self, MetaMCError> {
4835
let config = config::Config::builder()
4936
.set_default("bind_address", "127.0.0.1:8080")?
5037
.set_default("storage_format.type", "json")?
@@ -54,10 +41,9 @@ impl ServerConfig {
5441
.set_default("debug_log.path", "./logs")?
5542
.set_default("debug_log.prefix", "mcmeta.log")?
5643
.set_default("debug_log.level", "debug")?
57-
.add_source(config::File::new(
58-
"config/settings",
59-
config::FileFormat::Json,
60-
))
44+
// optionaly oad config from a file. this is optional though
45+
.add_source(config::File::from(std::path::Path::new(path)).required(false))
46+
// envierment overrides file
6147
.add_source(config::Environment::with_prefix("mcmeta").separator("__"))
6248
.build()?;
6349

mcmeta/src/main.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{path::Path as StdPath, sync::Arc};
1+
use std::{path::Path as StdPath, str::FromStr, sync::Arc};
22

33
use app_config::{ServerConfig, StorageFormat};
44
use axum::{
@@ -25,7 +25,7 @@ mod storage;
2525
mod utils;
2626

2727
custom_error! {pub MetaMCError
28-
MojangMetadata { source: download::mojang::MojangMetadataError } = "Error while downloading Mojang metadata: {source}",
28+
MojangMetadata { source: download::errors::MetadataError } = "Error while downloading metadata: {source}",
2929
Config { source: config::ConfigError } = "Error while reading config from environment",
3030
Parse { source: std::net::AddrParseError } = "Error while parsing address: {source}",
3131
Hyper { source: hyper::Error } = "Error while running Hyper: {source}",
@@ -250,13 +250,31 @@ async fn raw_forge_version_installer(
250250
}
251251
}
252252

253+
use argparse::{ArgumentParser, Store};
254+
use dotenv::dotenv;
253255
use tracing_subscriber::{filter, prelude::*};
254256

255257
#[tokio::main]
256258
async fn main() -> Result<(), MetaMCError> {
257-
let config = Arc::new(ServerConfig::from_config()?);
259+
dotenv().ok(); // This line loads the environment variables from the ".env" file.
258260

259-
let file_appender = tracing_appender::rolling::hourly(&config.debug_log.path, &config.debug_log.prefix);
261+
let mut config_path = "".to_string();
262+
{
263+
// limit scope of argparse borrows
264+
let mut ap = ArgumentParser::new();
265+
ap.set_description("A Minecraft metadata api server for Mojang and Modloader metadata.");
266+
ap.refer(&mut config_path).add_option(
267+
&["-c", "--config"],
268+
Store,
269+
"Path to a json config file.",
270+
);
271+
ap.parse_args_or_exit();
272+
}
273+
274+
let config = Arc::new(ServerConfig::from_config(&config_path)?);
275+
276+
let file_appender =
277+
tracing_appender::rolling::hourly(&config.debug_log.path, &config.debug_log.prefix);
260278
let (non_blocking_file, _guard) = tracing_appender::non_blocking(file_appender);
261279
let stdout_log = tracing_subscriber::fmt::layer()
262280
.with_file(true)
@@ -267,7 +285,9 @@ async fn main() -> Result<(), MetaMCError> {
267285
let debug_log = tracing_subscriber::fmt::layer()
268286
.with_ansi(false)
269287
.with_writer(non_blocking_file)
270-
.with_filter(filter::LevelFilter::from_level(config.debug_log.tracing_level()));
288+
.with_filter(filter::LevelFilter::from_level(
289+
tracing::Level::from_str(&config.debug_log.level).unwrap_or(tracing::Level::DEBUG),
290+
));
271291

272292
if config.debug_log.enable {
273293
tracing_subscriber::registry()

0 commit comments

Comments
 (0)