wip: cool config stuff

This commit is contained in:
Jef Roosens 2024-06-13 09:21:56 +02:00
parent 5073855696
commit 6dff65f30d
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
8 changed files with 209 additions and 9 deletions

View file

@ -10,6 +10,7 @@ authors = ["Jef Roosens"]
axum = { version = "0.7.5", features = ["http2", "macros"] }
chrono = { version = "0.4.26", features = ["serde"] }
clap = { version = "4.3.12", features = ["env", "derive"] }
figment = { version = "0.10.19", features = ["env", "toml"] }
futures = "0.3.28"
http-body-util = "0.1.1"
libarchive = { path = "../libarchive" }

3
server/rieterd.toml Normal file
View file

@ -0,0 +1,3 @@
api_key = "test"
port = 8000
log_level = "tower_http=debug,rieterd=debug"

View file

@ -1,9 +1,10 @@
use crate::{distro::MetaDistroMgr, Config, Global};
use std::{io, path::PathBuf};
use axum::Router;
use clap::Parser;
use sea_orm_migration::MigratorTrait;
use std::{io, path::PathBuf};
use tower_http::trace::TraceLayer;
use tracing::debug;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
@ -18,6 +19,14 @@ pub struct Cli {
#[arg(env = "RIETER_API_KEY")]
pub api_key: String,
#[arg(
short,
long,
env = "RIETER_CONFIG_FILE",
default_value = "./rieterd.toml"
)]
pub config_file: PathBuf,
/// Database connection URL; either sqlite:// or postgres://. Defaults to rieter.sqlite in the
/// data directory
#[arg(short, long, env = "RIETER_DATABASE_URL")]
@ -52,6 +61,10 @@ impl Cli {
pub async fn run(&self) -> crate::Result<()> {
self.init_tracing();
tracing::debug!("{:?}", &self.config_file);
let new_config = crate::config::Config::figment(&self.config_file).extract()?;
tracing::debug!("{:?}", new_config);
let db_url = if let Some(url) = &self.database_url {
url.clone()
} else {

45
server/src/config.rs Normal file
View file

@ -0,0 +1,45 @@
use std::path::{Path, PathBuf};
use figment::{
providers::{Env, Format, Toml},
Figment,
};
use serde::Deserialize;
#[derive(Deserialize)]
#[serde(rename_all = "lowercase")]
#[serde(tag = "type")]
pub enum FsConfig {
Local { data_dir: PathBuf },
}
#[derive(Deserialize)]
#[serde(rename_all = "lowercase")]
#[serde(tag = "type")]
pub enum DbConfig {
Sqlite {
path: PathBuf,
},
Postgres {
host: String,
user: String,
password: String,
},
}
#[derive(Deserialize)]
pub struct Config {
api_key: String,
port: u16,
log_level: String,
fs: FsConfig,
db: DbConfig,
}
impl Config {
pub fn figment(config_file: impl AsRef<Path>) -> Figment {
Figment::new()
.merge(Toml::file(config_file))
.merge(Env::prefixed("RIETER_"))
}
}

View file

@ -14,6 +14,7 @@ pub enum ServerError {
Db(sea_orm::DbErr),
Status(StatusCode),
Archive(libarchive::error::ArchiveError),
Figment(figment::Error),
}
impl fmt::Display for ServerError {
@ -24,6 +25,7 @@ impl fmt::Display for ServerError {
ServerError::Status(status) => write!(fmt, "{}", status),
ServerError::Db(err) => write!(fmt, "{}", err),
ServerError::Archive(err) => write!(fmt, "{}", err),
ServerError::Figment(err) => write!(fmt, "{}", err),
}
}
}
@ -41,7 +43,7 @@ impl IntoResponse for ServerError {
ServerError::Db(sea_orm::DbErr::RecordNotFound(_)) => {
StatusCode::NOT_FOUND.into_response()
}
ServerError::Db(_) | ServerError::Archive(_) => {
ServerError::Db(_) | ServerError::Archive(_) | ServerError::Figment(_) => {
StatusCode::INTERNAL_SERVER_ERROR.into_response()
}
}
@ -83,3 +85,9 @@ impl From<libarchive::error::ArchiveError> for ServerError {
ServerError::Archive(err)
}
}
impl From<figment::Error> for ServerError {
fn from(err: figment::Error) -> Self {
ServerError::Figment(err)
}
}

View file

@ -1,5 +1,6 @@
mod api;
mod cli;
mod config;
pub mod db;
mod distro;
mod error;

View file

@ -116,11 +116,7 @@ impl RepoMgr {
Ok(repo_id)
}
async fn add_pkg_from_path<P: AsRef<Path>>(
&self,
path: P,
repo: i32,
) -> crate::Result<()> {
async fn add_pkg_from_path<P: AsRef<Path>>(&self, path: P, repo: i32) -> crate::Result<()> {
let path_clone = path.as_ref().to_path_buf();
let pkg = tokio::task::spawn_blocking(move || package::Package::open(path_clone))
.await