Take config path as input

This commit is contained in:
Andras Schmelczer 2025-03-24 21:57:56 +00:00
parent 958af89116
commit baba8f82bf
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
6 changed files with 56 additions and 9 deletions

View file

@ -0,0 +1 @@
pub mod args;

View file

@ -0,0 +1,38 @@
use std::ffi::OsString;
use clap::{Parser, ValueEnum};
/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
#[arg(
long,
require_equals = true,
value_name = "WHEN",
num_args = 0..=1,
default_value_t = ColorWhen::Auto,
default_missing_value = "always",
value_enum
)]
pub color: ColorWhen,
#[arg(last = true)]
pub config_path: Option<OsString>,
}
#[derive(ValueEnum, Copy, Clone, Debug, PartialEq, Eq)]
pub enum ColorWhen {
Always,
Auto,
Never,
}
impl std::fmt::Display for ColorWhen {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.to_possible_value()
.expect("no values are skipped")
.get_name()
.fmt(f)
}
}

View file

@ -1,4 +1,4 @@
pub const CONFIG_PATH: &str = "config.yml";
pub const DEFAULT_CONFIG_PATH: &str = "config.yml";
pub const DEFAULT_DATABASES_DIRECTORY_PATH: &str = "databases";
pub const DEFAULT_HOST: &str = "127.0.0.1";
pub const DEFAULT_PORT: u16 = 3000;

View file

@ -1,3 +1,4 @@
mod cli;
mod config;
mod consts;
mod database;
@ -6,6 +7,8 @@ mod server;
mod utils;
use anyhow::{Context as _, Result};
use clap::Parser;
use cli::args::Args;
use errors::{SyncServerError, init_error};
use log::info;
use server::create_server;
@ -13,6 +16,8 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
#[tokio::main]
async fn main() -> Result<(), SyncServerError> {
let args = Args::parse();
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
@ -33,7 +38,7 @@ async fn main() -> Result<(), SyncServerError> {
env!("CARGO_PKG_VERSION")
);
create_server()
create_server(args.config_path)
.await
.context("Failed to start server")
.map_err(init_error)

View file

@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{ffi::OsString, sync::Arc};
use aide::{
axum::{
@ -48,11 +48,11 @@ mod requests;
mod responses;
mod update_document;
pub async fn create_server() -> Result<()> {
pub async fn create_server(config_path: Option<OsString>) -> Result<()> {
aide::r#gen::on_error(|err| error!("{err}"));
aide::r#gen::extract_schemas(true);
let app_state = AppState::try_new()
let app_state = AppState::try_new(config_path)
.await
.context("Failed to initialise app state")?;

View file

@ -1,6 +1,8 @@
use std::ffi::OsString;
use anyhow::Result;
use crate::{config::Config, consts::CONFIG_PATH, database::Database};
use crate::{config::Config, consts::DEFAULT_CONFIG_PATH, database::Database};
#[derive(Clone, Debug)]
pub struct AppState {
@ -9,10 +11,11 @@ pub struct AppState {
}
impl AppState {
pub async fn try_new() -> Result<Self> {
let path = std::path::Path::new(CONFIG_PATH);
pub async fn try_new(config_path: Option<OsString>) -> Result<Self> {
let config_path = config_path.unwrap_or_else(|| OsString::from(DEFAULT_CONFIG_PATH));
let path = std::path::PathBuf::from(config_path);
let config = Config::read_or_create(path).await?;
let config = Config::read_or_create(&path).await?;
let database = Database::try_new(&config.database).await?;
Ok(Self { config, database })