Take config path as input
This commit is contained in:
parent
958af89116
commit
baba8f82bf
6 changed files with 56 additions and 9 deletions
1
backend/sync_server/src/cli.rs
Normal file
1
backend/sync_server/src/cli.rs
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
pub mod args;
|
||||||
38
backend/sync_server/src/cli/args.rs
Normal file
38
backend/sync_server/src/cli/args.rs
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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_DATABASES_DIRECTORY_PATH: &str = "databases";
|
||||||
pub const DEFAULT_HOST: &str = "127.0.0.1";
|
pub const DEFAULT_HOST: &str = "127.0.0.1";
|
||||||
pub const DEFAULT_PORT: u16 = 3000;
|
pub const DEFAULT_PORT: u16 = 3000;
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
mod cli;
|
||||||
mod config;
|
mod config;
|
||||||
mod consts;
|
mod consts;
|
||||||
mod database;
|
mod database;
|
||||||
|
|
@ -6,6 +7,8 @@ mod server;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
use anyhow::{Context as _, Result};
|
use anyhow::{Context as _, Result};
|
||||||
|
use clap::Parser;
|
||||||
|
use cli::args::Args;
|
||||||
use errors::{SyncServerError, init_error};
|
use errors::{SyncServerError, init_error};
|
||||||
use log::info;
|
use log::info;
|
||||||
use server::create_server;
|
use server::create_server;
|
||||||
|
|
@ -13,6 +16,8 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), SyncServerError> {
|
async fn main() -> Result<(), SyncServerError> {
|
||||||
|
let args = Args::parse();
|
||||||
|
|
||||||
tracing_subscriber::registry()
|
tracing_subscriber::registry()
|
||||||
.with(
|
.with(
|
||||||
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
|
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
|
||||||
|
|
@ -33,7 +38,7 @@ async fn main() -> Result<(), SyncServerError> {
|
||||||
env!("CARGO_PKG_VERSION")
|
env!("CARGO_PKG_VERSION")
|
||||||
);
|
);
|
||||||
|
|
||||||
create_server()
|
create_server(args.config_path)
|
||||||
.await
|
.await
|
||||||
.context("Failed to start server")
|
.context("Failed to start server")
|
||||||
.map_err(init_error)
|
.map_err(init_error)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use std::sync::Arc;
|
use std::{ffi::OsString, sync::Arc};
|
||||||
|
|
||||||
use aide::{
|
use aide::{
|
||||||
axum::{
|
axum::{
|
||||||
|
|
@ -48,11 +48,11 @@ mod requests;
|
||||||
mod responses;
|
mod responses;
|
||||||
mod update_document;
|
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::on_error(|err| error!("{err}"));
|
||||||
aide::r#gen::extract_schemas(true);
|
aide::r#gen::extract_schemas(true);
|
||||||
|
|
||||||
let app_state = AppState::try_new()
|
let app_state = AppState::try_new(config_path)
|
||||||
.await
|
.await
|
||||||
.context("Failed to initialise app state")?;
|
.context("Failed to initialise app state")?;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
|
use std::ffi::OsString;
|
||||||
|
|
||||||
use anyhow::Result;
|
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)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
|
|
@ -9,10 +11,11 @@ pub struct AppState {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
pub async fn try_new() -> Result<Self> {
|
pub async fn try_new(config_path: Option<OsString>) -> Result<Self> {
|
||||||
let path = std::path::Path::new(CONFIG_PATH);
|
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?;
|
let database = Database::try_new(&config.database).await?;
|
||||||
|
|
||||||
Ok(Self { config, database })
|
Ok(Self { config, database })
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue