rieter/server/src/cli.rs

79 lines
2.3 KiB
Rust
Raw Normal View History

2023-07-16 20:52:03 +02:00
use crate::repo::RepoGroupManager;
use crate::{Config, Global};
use axum::extract::FromRef;
use axum::Router;
use clap::Parser;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use tower_http::trace::TraceLayer;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
/// Port the server will listen on
#[arg(short, long, value_name = "PORT", default_value_t = 8000)]
pub port: u16,
/// Log levels for the tracing
#[arg(
long,
value_name = "LOG_LEVEL",
default_value = "tower_http=debug,rieterd=debug"
)]
pub log: String,
/// Directory where package archives will be stored
pub pkg_dir: PathBuf,
/// Directory where repository metadata is stored
pub repo_dir: PathBuf,
}
impl FromRef<Global> for Arc<RwLock<RepoGroupManager>> {
fn from_ref(global: &Global) -> Self {
Arc::clone(&global.repo_manager)
}
}
impl Cli {
pub fn init_tracing(&self) {
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::new(self.log.clone()))
.with(tracing_subscriber::fmt::layer())
.init();
}
pub async fn run(&self) {
self.init_tracing();
// let db = crate::db::init("sqlite://test.db").await.unwrap();
let db = crate::db::init("postgres://rieter:rieter@localhost:5432/rieter")
.await
.unwrap();
2023-07-16 20:52:03 +02:00
let config = Config {
repo_dir: self.repo_dir.clone(),
pkg_dir: self.pkg_dir.clone(),
};
let repo_manager = RepoGroupManager::new(&self.repo_dir, &self.pkg_dir);
2023-07-16 20:52:03 +02:00
let global = Global {
config,
repo_manager: Arc::new(RwLock::new(repo_manager)),
2023-07-30 18:21:07 +02:00
db,
2023-07-16 20:52:03 +02:00
};
// build our application with a single route
let app = Router::new()
.nest("/api", crate::api::router())
.merge(crate::repo::router())
2023-07-16 20:52:03 +02:00
.with_state(global)
.layer(TraceLayer::new_for_http());
// run it with hyper on localhost:3000
axum::Server::bind(&format!("0.0.0.0:{}", self.port).parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}
}