From 12827ca37e4b298214ca665c0ce1d0cc07fdccc5 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Sat, 17 Jun 2023 00:55:12 -0400 Subject: [PATCH] add log level to config, create /data dir in docker image --- Dockerfile | 4 +++- config-example.toml | 3 +++ src/config.rs | 34 ++++++++++++++++++++++++++++++++-- src/main.rs | 12 ++++++------ 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4f02515..228d541 100644 --- a/Dockerfile +++ b/Dockerfile @@ -34,7 +34,9 @@ ARG GID=1000 RUN adduser --disabled-password --gecos "" $UNAME -s -G $GID -u $UID COPY --from=builder --chown=$UID:$GID /app/src/target/release/orca-registry /app/orca-registry -RUN chown -R $UID:$GID /app +RUN mkdir /data && \ + chown -R $UID:$GID /data && \ + chown -R $UID:$GID /app USER $UNAME diff --git a/config-example.toml b/config-example.toml index 0981690..0924fc3 100644 --- a/config-example.toml +++ b/config-example.toml @@ -2,6 +2,9 @@ listen_address = "127.0.0.1" listen_port = "3000" url = "http://localhost:3000/" +# error, warn, info, debug, trace +log_level = "debug" + [storage] driver = "filesystem" path = "/app/blobs" diff --git a/src/config.rs b/src/config.rs index fb40188..f03c880 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,8 @@ +use anyhow::anyhow; use figment::{Figment, providers::{Env, Toml, Format}}; use figment_cliarg_provider::FigmentCliArgsProvider; -use serde::Deserialize; +use serde::{Deserialize, Deserializer}; +use tracing::Level; use std::env; @@ -56,6 +58,8 @@ pub struct Config { pub listen_address: String, pub listen_port: String, url: Option, + #[serde(deserialize_with = "serialize_log_level", default = "default_log_level")] + pub log_level: Level, pub ldap: Option, pub database: DatabaseConfig, pub storage: StorageConfig, @@ -102,4 +106,30 @@ impl Config { None => format!("http://{}:{}", self.listen_address, self.listen_port) } } -} \ No newline at end of file +} + +fn default_log_level() -> Level { + Level::INFO +} + +fn serialize_log_level<'de, D>(deserializer: D) -> Result +where D: Deserializer<'de> { + let s = String::deserialize(deserializer)?.to_lowercase(); + let s = s.as_str(); + + match s { + "error" => Ok(Level::ERROR), + "warn" => Ok(Level::WARN), + "info" => Ok(Level::INFO), + "debug" => Ok(Level::DEBUG), + "trace" => Ok(Level::TRACE), + _ => Err(serde::de::Error::custom(format!("Unknown log level: '{}'", s))), + } +} + +/* fn<'de, D> serialize_log_level(D) -> Result +where D: Deserializer<'de> +{ + +} */ +//fn serialize_log_level() -> Level \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index fa39dc7..203c797 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ use tower_layer::Layer; use sqlx::sqlite::{SqlitePoolOptions, SqliteConnectOptions, SqliteJournalMode}; use tokio::sync::Mutex; use tower_http::normalize_path::NormalizePathLayer; -use tracing::{debug, Level}; +use tracing::{debug, Level, info}; use app_state::AppState; use database::Database; @@ -62,13 +62,13 @@ async fn change_request_paths(mut request: Request, next: Next) -> Resu #[tokio::main] async fn main() -> anyhow::Result<()> { - tracing_subscriber::fmt() - .with_max_level(Level::DEBUG) - .init(); - let config = Config::new() .expect("Failure to parse config!"); + tracing_subscriber::fmt() + .with_max_level(config.log_level) + .init(); + let sqlite_config = match &config.database { DatabaseConfig::Sqlite(sqlite) => sqlite, }; @@ -136,7 +136,7 @@ async fn main() -> anyhow::Result<()> { let layered_app = NormalizePathLayer::trim_trailing_slash().layer(path_middleware.layer(app)); - debug!("Starting http server, listening on {}", app_addr); + info!("Starting http server, listening on {}", app_addr); axum::Server::bind(&app_addr) .serve(layered_app.into_make_service()) .await?;