feat(server): log errors; configurable database
ci/woodpecker/push/lint Pipeline was successful Details
ci/woodpecker/push/clippy Pipeline failed Details
ci/woodpecker/push/build Pipeline was successful Details

Jef Roosens 2023-08-01 15:30:38 +02:00
parent b097a5ea87
commit de3de6ee15
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 43 additions and 18 deletions

View File

@ -4,14 +4,27 @@ use crate::{Config, Global};
use axum::extract::FromRef; use axum::extract::FromRef;
use axum::Router; use axum::Router;
use clap::Parser; use clap::Parser;
use std::io;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use tower_http::trace::TraceLayer; use tower_http::trace::TraceLayer;
use tracing::debug;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
#[derive(Parser)] #[derive(Parser)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
pub struct Cli { pub struct Cli {
/// Directory where package archives will be stored
pub pkg_dir: PathBuf,
/// Directory where repository metadata & SQLite database is stored
pub data_dir: PathBuf,
/// Default architecture to add packages with arch "any" to
pub default_arch: String,
/// Database connection URL; either sqlite:// or postgres://. Defaults to rieter.sqlite in the
/// data directory
#[arg(short, long)]
pub database_url: Option<String>,
/// Port the server will listen on /// Port the server will listen on
#[arg(short, long, value_name = "PORT", default_value_t = 8000)] #[arg(short, long, value_name = "PORT", default_value_t = 8000)]
pub port: u16, pub port: u16,
@ -22,12 +35,6 @@ pub struct Cli {
default_value = "tower_http=debug,rieterd=debug" default_value = "tower_http=debug,rieterd=debug"
)] )]
pub log: String, pub log: String,
/// Directory where package archives will be stored
pub pkg_dir: PathBuf,
/// Directory where repository metadata is stored
pub repo_dir: PathBuf,
/// Default architecture to add packages with arch "any" to
pub default_arch: String,
} }
impl FromRef<Global> for Arc<RwLock<RepoGroupManager>> { impl FromRef<Global> for Arc<RwLock<RepoGroupManager>> {
@ -44,19 +51,32 @@ impl Cli {
.init(); .init();
} }
pub async fn run(&self) { pub async fn run(&self) -> crate::Result<()> {
self.init_tracing(); self.init_tracing();
let db = crate::db::init("sqlite://test.db").await.unwrap(); let db_url = if let Some(url) = &self.database_url {
url.clone()
} else {
format!(
"sqlite://{}",
self.data_dir.join("rieter.sqlite").to_string_lossy()
)
};
debug!("Connecting to database with URL {}", db_url);
let db = crate::db::init(db_url).await?;
// let db = crate::db::init("postgres://rieter:rieter@localhost:5432/rieter") // let db = crate::db::init("postgres://rieter:rieter@localhost:5432/rieter")
// .await // .await
// .unwrap(); // .unwrap();
let config = Config { let config = Config {
repo_dir: self.repo_dir.clone(), data_dir: self.data_dir.clone(),
repo_dir: self.data_dir.join("repos"),
pkg_dir: self.pkg_dir.clone(), pkg_dir: self.pkg_dir.clone(),
}; };
let repo_manager = RepoGroupManager::new(&self.repo_dir, &self.pkg_dir, &self.default_arch); let repo_manager =
RepoGroupManager::new(&config.repo_dir, &self.pkg_dir, &self.default_arch);
let global = Global { let global = Global {
config, config,
@ -72,9 +92,11 @@ impl Cli {
.layer(TraceLayer::new_for_http()); .layer(TraceLayer::new_for_http());
// run it with hyper on localhost:3000 // run it with hyper on localhost:3000
axum::Server::bind(&format!("0.0.0.0:{}", self.port).parse().unwrap()) Ok(
.serve(app.into_make_service()) axum::Server::bind(&format!("0.0.0.0:{}", self.port).parse().unwrap())
.await .serve(app.into_make_service())
.unwrap(); .await
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?,
)
} }
} }

View File

@ -29,6 +29,8 @@ impl Error for ServerError {}
impl IntoResponse for ServerError { impl IntoResponse for ServerError {
fn into_response(self) -> Response { fn into_response(self) -> Response {
tracing::error!("{:?}", self);
match self { match self {
ServerError::IO(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(), ServerError::IO(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
ServerError::Axum(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(), ServerError::Axum(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),

View File

@ -13,6 +13,7 @@ use std::sync::{Arc, RwLock};
#[derive(Clone)] #[derive(Clone)]
pub struct Config { pub struct Config {
data_dir: PathBuf,
repo_dir: PathBuf, repo_dir: PathBuf,
pkg_dir: PathBuf, pkg_dir: PathBuf,
} }
@ -25,7 +26,7 @@ pub struct Global {
} }
#[tokio::main] #[tokio::main]
async fn main() { async fn main() -> crate::Result<()> {
let cli = cli::Cli::parse(); let cli = cli::Cli::parse();
cli.run().await; cli.run().await
} }