Implemented configs, made functions use it instead

This commit is contained in:
2024-03-23 23:44:50 +02:00
parent a4a1e93ddd
commit b18e193173
9 changed files with 158 additions and 9 deletions

View File

@@ -5,14 +5,17 @@ use clap::Parser;
#[command(version, about, long_about = None)]
pub struct CliArgs {
/// Port to bind to
#[arg(short, long, default_value_t=8080)]
pub port: u16,
#[arg(short, long)]
pub port: Option<u16>,
/// Host ip to bind to, usually not required to change
#[arg(long, default_value="0.0.0.0")]
pub host: String,
#[arg(long)]
pub host: Option<String>,
/// Extra debugging output
#[arg(long, short)]
pub debug: bool
pub debug: bool,
#[arg(long, short, default_value="./config.toml")]
pub config: camino::Utf8PathBuf,
}

13
src/config/definition.rs Normal file
View File

@@ -0,0 +1,13 @@
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct Config {
pub debug: bool,
pub webserver: ConfigWebserver
}
#[derive(Debug, Deserialize)]
pub struct ConfigWebserver {
pub host: String,
pub port: u16,
}

54
src/config/mod.rs Normal file
View File

@@ -0,0 +1,54 @@
use std::borrow::{Borrow, BorrowMut};
use crate::cli::CliArgs;
pub mod definition;
const DEFAULT_CONFIG: &'static str = include_str!("../../config.default.toml");
pub struct ConfigManager {
inner: definition::Config,
}
impl ConfigManager {
pub fn parse_and_join(cli: &CliArgs) -> anyhow::Result<Self> {
let data = if !cli.config.exists() {
log::warn!("Config doesnt exist, making it.");
std::fs::write(&cli.config, DEFAULT_CONFIG)?;
DEFAULT_CONFIG.to_string()
} else {
std::fs::read_to_string(&cli.config)?
};
let mut inner = toml::from_str::<definition::Config>(&data)?;
if let Some(host) = &cli.host {
inner.webserver.host = host.clone();
}
if let Some(port) = cli.port {
inner.webserver.port = port;
}
if cli.debug {
inner.debug = true;
}
Ok(Self {
inner
})
}
pub fn get_ref(&self) -> &definition::Config {
self.inner.borrow()
}
#[allow(dead_code)]
pub fn get_mut(&mut self) -> &mut definition::Config {
self.inner.borrow_mut()
}
}

View File

@@ -3,14 +3,23 @@ use clap::Parser;
mod public;
mod logger;
mod cli;
mod config;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let cli = cli::CliArgs::parse();
logger::init_logger(&cli);
let config = match config::ConfigManager::parse_and_join(&cli) {
Ok(r) => r,
Err(e) => {
log::error!("Failed to parse configs: {e}");
return Ok(());
}
};
if let Err(e) = public::start_actix(&cli).await {
if let Err(e) = public::start_actix(config.get_ref()).await {
log::error!("Actix had an error: {e}");
}
Ok(())

View File

@@ -5,10 +5,10 @@ mod templates;
use actix_web::{web, App, HttpServer};
use actix_files as actix_fs;
use crate::cli::CliArgs;
use crate::config::definition::Config;
pub(crate) async fn start_actix(cli: &CliArgs) -> anyhow::Result<()> {
let bindip = format!("{}:{}", cli.host, cli.port);
pub(crate) async fn start_actix(config: &Config) -> anyhow::Result<()> {
let bindip = format!("{}:{}", config.webserver.host, config.webserver.port);
log::info!("Serving an http server at http://{bindip}");
HttpServer::new(|| {