Skip to content

Commit

Permalink
Default trustd storage path to .trustify/storage/...
Browse files Browse the repository at this point in the history
Default logging to INFO unless otherwise controlled.
  • Loading branch information
Bob McWhirter committed Mar 29, 2024
1 parent ae7ce59 commit a110baf
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 30 deletions.
1 change: 1 addition & 0 deletions common/auth/src/swagger_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl SwaggerUiOidcConfig {
}
}

#[derive(Debug)]
pub struct SwaggerUiOidc {
pub client_id: String,
pub auth_url: String,
Expand Down
11 changes: 10 additions & 1 deletion common/infrastructure/src/tracing.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::fmt;
use log::LevelFilter;
use opentelemetry::{propagation::Injector, Context, KeyValue};
use opentelemetry_sdk::Resource;
use reqwest::RequestBuilder;
Expand Down Expand Up @@ -142,7 +143,15 @@ fn init_otlp(name: &str) {
}

fn init_no_tracing() {
if let Err(e) = env_logger::builder().format_timestamp_millis().try_init() {
let mut builder = env_logger::builder();
builder.format_timestamp_millis();

if std::env::var("RUST_LOG").ok().is_none() {
eprintln!("Default to INFO logging; use RUST_LOG environment variable to change");
builder.filter_level(LevelFilter::Info);
}

if let Err(e) = builder.try_init() {
eprintln!("Error initializing logging: {:?}", e);
}
}
55 changes: 37 additions & 18 deletions common/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt::{Display, Formatter};
use std::path::PathBuf;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum)]
pub enum DbStrategy {
Expand Down Expand Up @@ -35,7 +36,6 @@ pub struct Database {
long,
env = "DB_USER",
default_value = "trustify",
group = "external-db",
required = false,
required_if_eq("db-strategy", "external")
)]
Expand All @@ -45,33 +45,52 @@ pub struct Database {
long,
env = "DB_PASSWORD",
default_value = "trustify",
group = "external-db",
required = false,
required_if_eq("db-strategy", "external")
)]
pub password: String,
#[arg(
id = "db-host",
long,
env = "DB_HOST",
default_value = "localhost",
group = "external-db"
)]
#[arg(id = "db-host", long, env = "DB_HOST", default_value = "localhost")]
pub host: String,
#[arg(id = "db-port", long, env = "DB_PORT", default_value_t = 5432)]
pub port: u16,
#[arg(id = "db-name", long, env = "DB_NAME", default_value = "trustify")]
pub name: String,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum)]
pub enum StorageStrategy {
Fs,
S3,
}

impl Display for StorageStrategy {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
StorageStrategy::Fs => write!(f, "fs"),
StorageStrategy::S3 => write!(f, "s3"),
}
}
}

#[derive(clap::Args, Debug, Clone)]
#[command(next_help_heading = "Storage")]
#[group(id = "storage", multiple = false)]
pub struct StorageConfig {
#[arg(
id = "db-port",
id = "storage-strategy",
long,
env = "DB_PORT",
default_value_t = 5432,
group = "external-db"
env,
default_value_t = StorageStrategy::Fs,
)]
pub port: u16,
pub storage_strategy: StorageStrategy,

#[arg(
id = "db-name",
id = "storage-fs-path",
long,
env = "DB_NAME",
default_value = "trustify",
group = "external-db"
default_value = "./.trustify/storage",
required = false,
required_if_eq("storage-strategy", "fs")
)]
pub name: String,
pub fs_path: Option<PathBuf>,
}
4 changes: 2 additions & 2 deletions modules/storage/src/service/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{
};
use tempfile::{tempdir, tempfile, TempDir};
use tokio::{
fs::{create_dir, create_dir_all, File},
fs::{create_dir_all, File},
io::{AsyncSeekExt, AsyncWriteExt},
};
use tokio_util::io::ReaderStream;
Expand Down Expand Up @@ -51,7 +51,7 @@ impl FileSystemBackend {
let base = base.into();
let content = base.join("content");

create_dir(&content)
create_dir_all(&content)
.await
.or_else(|err| {
if err.kind() == ErrorKind::AlreadyExists {
Expand Down
19 changes: 13 additions & 6 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use trustify_auth::{
auth::AuthConfigArguments, authenticator::Authenticator, authorizer::Authorizer,
swagger_ui::SwaggerUiOidcConfig,
};
use trustify_common::config::StorageConfig;
use trustify_common::{config::Database, db};
use trustify_infrastructure::{
app::http::{HttpServerBuilder, HttpServerConfig},
Expand All @@ -36,16 +37,16 @@ pub struct Run {
#[command(flatten)]
pub database: Database,

/// Location of the file storage
#[arg(long, env)]
pub storage: Option<PathBuf>,

#[arg(long, env)]
pub bootstrap: bool,

#[arg(long, env)]
pub devmode: bool,

/// Location of the storage
#[command(flatten)]
pub storage: StorageConfig,

#[command(flatten)]
pub infra: InfrastructureConfig,

Expand Down Expand Up @@ -118,9 +119,15 @@ impl InitData {

let storage = run
.storage
.unwrap_or_else(|| PathBuf::from("./data/storage"));
.fs_path
.as_ref()
.cloned()
.unwrap_or_else(|| PathBuf::from("./.trustify/storage"));
if run.devmode {
create_dir_all(&storage).context("Failed to create storage directory")?;
create_dir_all(&storage).context(format!(
"Failed to create filesystem storage directory: {:?}",
run.storage.fs_path
))?;
}
let storage = DispatchBackend::Filesystem(FileSystemBackend::new(storage).await?);

Expand Down
9 changes: 6 additions & 3 deletions trustd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::process::{ExitCode, Termination};
use tokio::task::JoinSet;
use trustify_auth::auth::AuthConfigArguments;
use trustify_auth::swagger_ui::SwaggerUiOidcConfig;
use trustify_common::config::{Database, DbStrategy};
use trustify_common::config::{Database, DbStrategy, StorageConfig};
use trustify_infrastructure::app::http::HttpServerConfig;
use trustify_infrastructure::endpoint::Trustify;
use trustify_infrastructure::InfrastructureConfig;
Expand All @@ -20,7 +20,7 @@ pub enum Command {
#[derive(clap::Parser, Debug)]
#[command(
author,
version = env!("CARGO_PKG_VERSION"),
version = env ! ("CARGO_PKG_VERSION"),
about = "trustd",
long_about = None
)]
Expand All @@ -37,6 +37,9 @@ pub struct Trustd {
#[arg(long, env)]
pub devmode: bool,

#[command(flatten)]
pub storage: StorageConfig,

#[command(flatten)]
pub database: Database,

Expand Down Expand Up @@ -109,7 +112,7 @@ impl Trustd {
if self.with_http {
let http = trustify_server::Run {
database: self.database.clone(),
storage: None,
storage: self.storage.clone(),
bootstrap: self.bootstrap,
devmode: self.devmode,
infra: self.infra.clone(),
Expand Down

0 comments on commit a110baf

Please sign in to comment.