Start server via subcommand

This commit is contained in:
Joscha 2023-08-07 14:49:09 +02:00
parent 9bdfc79c8b
commit 6815c36f5b
3 changed files with 40 additions and 29 deletions

View file

@ -4,16 +4,27 @@ pub const NAME: &str = env!("CARGO_PKG_NAME");
pub const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", env!("VERGEN_GIT_SHA"), ")"); pub const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", env!("VERGEN_GIT_SHA"), ")");
#[derive(Debug, clap::Parser)] #[derive(Debug, clap::Parser)]
#[command(name = NAME, version = VERSION)] pub struct ServerCommand {
pub struct Args {
/// Path to the repo's tablejohn database. /// Path to the repo's tablejohn database.
pub db: PathBuf, pub db: PathBuf,
/// Path to the git repo. /// Path to the git repo.
pub repo: PathBuf, pub repo: PathBuf,
}
#[derive(Debug, clap::Parser)]
pub enum Command {
Server(ServerCommand),
}
#[derive(Debug, clap::Parser)]
#[command(name = NAME, version = VERSION)]
pub struct Args {
/// Path to the config file. /// Path to the config file.
#[arg(long, short)] #[arg(long, short)]
pub config: Option<PathBuf>, pub config: Option<PathBuf>,
/// Enable increasingly more verbose output /// Enable increasingly more verbose output
#[arg(long, short, action = clap::ArgAction::Count)] #[arg(long, short, action = clap::ArgAction::Count)]
pub verbose: u8, pub verbose: u8,
#[command(subcommand)]
pub command: Command,
} }

View file

@ -15,7 +15,7 @@ use tracing_subscriber::{
}; };
use crate::{ use crate::{
args::{Args, NAME, VERSION}, args::{Args, Command, NAME, VERSION},
config::Config, config::Config,
server::Server, server::Server,
}; };
@ -98,9 +98,11 @@ async fn run() -> somehow::Result<()> {
info!("You are running {NAME} {VERSION}"); info!("You are running {NAME} {VERSION}");
let config = load_config(args.config)?; let config = load_config(args.config)?;
let server = Server::new(config, &args.db, &args.repo).await?;
info!("Startup complete, running"); match args.command {
Command::Server(command) => {
let server = Server::new(config, command).await?;
select! { select! {
_ = wait_for_signal() => {} _ = wait_for_signal() => {}
_ = server.run() => {} _ = server.run() => {}
@ -119,6 +121,8 @@ async fn run() -> somehow::Result<()> {
// nothing else? // nothing else?
_ = server.shut_down() => {} _ = server.shut_down() => {}
} }
}
}
Ok(()) Ok(())
} }

View file

@ -12,7 +12,7 @@ use sqlx::{
use tokio::select; use tokio::select;
use tracing::{debug, info}; use tracing::{debug, info};
use crate::{config::Config, somehow}; use crate::{args::ServerCommand, config::Config, somehow};
async fn open_db(db_path: &Path) -> sqlx::Result<SqlitePool> { async fn open_db(db_path: &Path) -> sqlx::Result<SqlitePool> {
let options = SqliteConnectOptions::new() let options = SqliteConnectOptions::new()
@ -61,15 +61,11 @@ pub struct Server {
} }
impl Server { impl Server {
pub async fn new( pub async fn new(config: &'static Config, command: ServerCommand) -> somehow::Result<Self> {
config: &'static Config,
db_path: &Path,
repo_path: &Path,
) -> somehow::Result<Self> {
Ok(Self { Ok(Self {
config, config,
db: open_db(db_path).await?, db: open_db(&command.db).await?,
repo: Arc::new(open_repo(repo_path)?), repo: Arc::new(open_repo(&command.repo)?),
}) })
} }