From 9a5419fb35022bb267f0928bfb1d2a89cc70c411 Mon Sep 17 00:00:00 2001 From: Troy Benson Date: Tue, 4 Jul 2023 21:53:46 +0000 Subject: [PATCH] feat: clean up config --- Cargo.lock | 1 + backend/api/src/config.rs | 106 +---------------- backend/api/src/main.rs | 2 +- backend/api/src/tests/global/mod.rs | 7 +- backend/api/src/tests/grpc/tls.rs | 3 +- common/Cargo.toml | 5 +- common/src/config.rs | 108 ++++++++++++++++++ common/src/logging.rs | 44 ++++++- common/src/tests/logging.rs | 4 +- video/edge/src/config.rs | 91 +-------------- video/edge/src/main.rs | 2 +- video/edge/src/tests/global.rs | 2 +- video/edge/src/tests/grpc/tls.rs | 3 +- video/ingest/src/config.rs | 52 +-------- video/ingest/src/main.rs | 2 +- video/ingest/src/tests/global.rs | 2 +- video/ingest/src/tests/grpc/tls.rs | 3 +- video/ingest/src/tests/ingest.rs | 3 +- video/transcoder/src/config.rs | 114 +------------------ video/transcoder/src/global.rs | 2 +- video/transcoder/src/main.rs | 2 +- video/transcoder/src/tests/global.rs | 2 +- video/transcoder/src/tests/grpc/tls.rs | 3 +- video/transcoder/src/tests/transcoder/mod.rs | 13 ++- video/transcoder/src/transcoder/mod.rs | 2 +- 25 files changed, 189 insertions(+), 389 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a5b9ab52..56369f17 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -840,6 +840,7 @@ dependencies = [ "prost", "serde", "tempfile", + "thiserror", "tokio", "tokio-util", "tonic", diff --git a/backend/api/src/config.rs b/backend/api/src/config.rs index 3bc15019..7a9928c3 100644 --- a/backend/api/src/config.rs +++ b/backend/api/src/config.rs @@ -1,111 +1,7 @@ use std::net::SocketAddr; use anyhow::Result; - -#[derive(Debug, Clone, Default, PartialEq, config::Config)] -pub struct TlsConfig { - /// Domain name to use for TLS - /// Only used for gRPC TLS connections - #[config(default)] - pub domain: Option, - - /// The path to the TLS certificate - pub cert: String, - - /// The path to the TLS private key - pub key: String, - - /// The path to the TLS CA certificate - pub ca_cert: String, -} - -#[derive(Debug, Clone, PartialEq, config::Config)] -#[config(default)] -pub struct LoggingConfig { - /// The log level to use, this is a tracing env filter - pub level: String, - - /// If we should use JSON logging - pub json: bool, -} - -impl Default for LoggingConfig { - fn default() -> Self { - Self { - level: "info".to_string(), - json: false, - } - } -} - -#[derive(Debug, Clone, PartialEq, config::Config)] -#[config(default)] -pub struct RmqConfig { - /// The URI to use for connecting to RabbitMQ - pub uri: String, -} - -impl Default for RmqConfig { - fn default() -> Self { - Self { - uri: "amqp://rabbitmq:rabbitmq@localhost:5672/scuffle".to_string(), - } - } -} - -#[derive(Debug, Clone, PartialEq, config::Config)] -#[config(default)] -pub struct RedisConfig { - /// The address of the Redis server - pub addresses: Vec, - - /// Number of connections to keep in the pool - pub pool_size: usize, - - /// The username to use for authentication - pub username: Option, - - /// The password to use for authentication - pub password: Option, - - /// The database to use - pub database: u8, - - /// The TLS configuration - pub tls: Option, - - /// To use Redis Sentinel - pub sentinel: Option, -} - -impl Default for RedisConfig { - fn default() -> Self { - Self { - addresses: vec!["localhost:6379".to_string()], - pool_size: 10, - username: None, - password: None, - database: 0, - tls: None, - sentinel: None, - } - } -} - -#[derive(Debug, Clone, PartialEq, config::Config)] -// #[config(default)] -pub struct RedisSentinelConfig { - /// The master group name - pub service_name: String, -} - -impl Default for RedisSentinelConfig { - fn default() -> Self { - Self { - service_name: "myservice".to_string(), - } - } -} +use common::config::{LoggingConfig, RedisConfig, RmqConfig, TlsConfig}; #[derive(Debug, Clone, PartialEq, config::Config)] #[config(default)] diff --git a/backend/api/src/main.rs b/backend/api/src/main.rs index b060d7b0..74b631a3 100644 --- a/backend/api/src/main.rs +++ b/backend/api/src/main.rs @@ -21,7 +21,7 @@ mod tests; #[tokio::main] async fn main() -> Result<()> { let config = config::AppConfig::parse()?; - logging::init(&config.logging.level, config.logging.json)?; + logging::init(&config.logging.level, config.logging.mode)?; if let Some(file) = &config.config_file { tracing::info!(file = file, "loaded config from file"); diff --git a/backend/api/src/tests/global/mod.rs b/backend/api/src/tests/global/mod.rs index a041a56c..f4354684 100644 --- a/backend/api/src/tests/global/mod.rs +++ b/backend/api/src/tests/global/mod.rs @@ -2,6 +2,7 @@ use std::{sync::Arc, time::Duration}; use crate::{config::AppConfig, global::GlobalState}; use common::{ + config::RedisSentinelConfig, context::{Context, Handler}, logging, prelude::FutureTimeout, @@ -16,7 +17,7 @@ pub async fn mock_global_state(mut config: AppConfig) -> (Arc, Hand dotenvy::dotenv().ok(); - logging::init(&config.logging.level, config.logging.json) + logging::init(&config.logging.level, config.logging.mode) .expect("failed to initialize logging"); let db = Arc::new( @@ -53,9 +54,7 @@ pub async fn mock_global_state(mut config: AppConfig) -> (Arc, Hand config.redis.password = redis_config.password; config.redis.username = redis_config.username; config.redis.sentinel = match redis_config.server { - ServerConfig::Sentinel { service_name, .. } => { - Some(crate::config::RedisSentinelConfig { service_name }) - } + ServerConfig::Sentinel { service_name, .. } => Some(RedisSentinelConfig { service_name }), _ => None, }; diff --git a/backend/api/src/tests/grpc/tls.rs b/backend/api/src/tests/grpc/tls.rs index 4a6927c2..3255f60f 100644 --- a/backend/api/src/tests/grpc/tls.rs +++ b/backend/api/src/tests/grpc/tls.rs @@ -1,10 +1,11 @@ +use common::config::TlsConfig; use common::grpc::{make_channel, TlsSettings}; use common::prelude::FutureTimeout; use std::path::PathBuf; use std::time::Duration; use tonic::transport::{Certificate, Identity}; -use crate::config::{AppConfig, GrpcConfig, TlsConfig}; +use crate::config::{AppConfig, GrpcConfig}; use crate::grpc::run; use crate::pb; use crate::tests::global::mock_global_state; diff --git a/common/Cargo.toml b/common/Cargo.toml index bff8a636..4b34ab1f 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -6,14 +6,14 @@ authors = ["Scuffle "] description = "Scuffle Common Library" [features] -logging = ["dep:log", "dep:tracing", "dep:tracing-log", "dep:tracing-subscriber", "dep:arc-swap", "dep:anyhow", "dep:once_cell"] +logging = ["dep:log", "dep:tracing", "dep:tracing-log", "dep:tracing-subscriber", "dep:arc-swap", "dep:anyhow", "dep:once_cell", "dep:thiserror"] rmq = ["dep:lapin", "dep:arc-swap", "dep:anyhow", "dep:futures", "dep:tracing", "dep:tokio", "dep:async-stream", "prelude"] grpc = ["dep:tonic", "dep:anyhow", "dep:async-trait", "dep:futures", "dep:http", "dep:tower", "dep:trust-dns-resolver", "dep:tracing"] context = ["dep:tokio", "dep:tokio-util"] prelude = ["dep:tokio"] signal = [] macros = [] -config = ["dep:config"] +config = ["dep:config", "logging"] default = ["logging", "rmq", "grpc", "context", "prelude", "signal", "macros", "config"] @@ -37,6 +37,7 @@ tracing-log = { version = "0", features = ["env_logger"], optional = true, git = once_cell = { version = "1", optional = true } trust-dns-resolver = { version = "0", features = ["tokio-runtime"], optional = true } tracing-subscriber = { version = "0", features = ["fmt", "env-filter", "json"], optional = true } +thiserror = { version = "1", optional = true } [dev-dependencies] prost = "0" diff --git a/common/src/config.rs b/common/src/config.rs index e0d11fae..df863a17 100644 --- a/common/src/config.rs +++ b/common/src/config.rs @@ -1,5 +1,113 @@ use anyhow::Result; +use crate::logging; + +#[derive(Debug, Clone, Default, PartialEq, config::Config)] +pub struct TlsConfig { + /// Domain name to use for TLS + /// Only used for gRPC TLS connections + #[config(default)] + pub domain: Option, + + /// The path to the TLS certificate + pub cert: String, + + /// The path to the TLS private key + pub key: String, + + /// The path to the TLS CA certificate + pub ca_cert: String, +} + +#[derive(Debug, Clone, PartialEq, config::Config)] +#[config(default)] +pub struct LoggingConfig { + /// The log level to use, this is a tracing env filter + pub level: String, + + /// What logging mode we should use + #[config(from_str)] + pub mode: logging::Mode, +} + +impl Default for LoggingConfig { + fn default() -> Self { + Self { + level: "info".to_string(), + mode: logging::Mode::Default, + } + } +} + +#[derive(Debug, Clone, PartialEq, config::Config)] +#[config(default)] +pub struct RedisConfig { + /// The address of the Redis server + pub addresses: Vec, + + /// Number of connections to keep in the pool + pub pool_size: usize, + + /// The username to use for authentication + pub username: Option, + + /// The password to use for authentication + pub password: Option, + + /// The database to use + pub database: u8, + + /// The TLS configuration + pub tls: Option, + + /// To use Redis Sentinel + pub sentinel: Option, +} + +#[derive(Debug, Clone, PartialEq, config::Config)] +// #[config(default)] +pub struct RedisSentinelConfig { + /// The master group name + pub service_name: String, +} + +#[derive(Debug, Clone, PartialEq, config::Config)] +#[config(default)] +pub struct RmqConfig { + /// The URI to use for connecting to RabbitMQ + pub uri: String, +} + +impl Default for RmqConfig { + fn default() -> Self { + Self { + uri: "amqp://rabbitmq:rabbitmq@localhost:5672/scuffle".to_string(), + } + } +} + +impl Default for RedisSentinelConfig { + fn default() -> Self { + Self { + service_name: "myservice".to_string(), + } + } +} + +impl Default for RedisConfig { + fn default() -> Self { + Self { + addresses: vec!["localhost:6379".to_string()], + pool_size: 10, + username: None, + password: None, + database: 0, + tls: None, + sentinel: None, + } + } +} + pub fn parse( enable_cli: bool, config_file: Option, diff --git a/common/src/logging.rs b/common/src/logging.rs index 22de6463..b6560391 100644 --- a/common/src/logging.rs +++ b/common/src/logging.rs @@ -1,12 +1,43 @@ use std::str::FromStr; -use anyhow::Result; use once_cell::sync::OnceCell; use tracing_subscriber::{prelude::*, reload::Handle, EnvFilter}; static RELOAD_HANDLE: OnceCell> = OnceCell::new(); -pub fn init(level: &str, json: bool) -> Result<()> { +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Mode { + Default, + Json, + Pretty, + Compact, +} + +#[derive(Debug, thiserror::Error)] +pub enum LoggingError { + #[error("invalid logging mode: {0}")] + InvalidMode(String), + #[error("failed to init logger: {0}")] + Init(#[from] tracing_subscriber::util::TryInitError), + #[error("failed to reload logger: {0}")] + Reload(#[from] tracing_subscriber::reload::Error), +} + +impl FromStr for Mode { + type Err = LoggingError; + + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_str() { + "" | "default" => Ok(Self::Default), + "json" => Ok(Self::Json), + "pretty" => Ok(Self::Pretty), + "compact" => Ok(Self::Compact), + _ => Err(LoggingError::InvalidMode(s.to_string())), + } + } +} + +pub fn init(level: &str, mode: Mode) -> Result<(), LoggingError> { let reload = RELOAD_HANDLE.get_or_try_init(|| { let env_filter = EnvFilter::from_str(level).expect("failed to parse log level"); @@ -18,10 +49,11 @@ pub fn init(level: &str, json: bool) -> Result<()> { let handle = filter.reload_handle(); - if json { - filter.json().finish().try_init() - } else { - filter.pretty().finish().try_init() + match mode { + Mode::Default => filter.finish().try_init(), + Mode::Json => filter.json().finish().try_init(), + Mode::Pretty => filter.pretty().finish().try_init(), + Mode::Compact => filter.compact().finish().try_init(), } .map(|_| handle) })?; diff --git a/common/src/tests/logging.rs b/common/src/tests/logging.rs index 9cfaa640..c011d5a5 100644 --- a/common/src/tests/logging.rs +++ b/common/src/tests/logging.rs @@ -1,6 +1,6 @@ -use crate::logging::init; +use crate::logging::{self, init}; #[test] fn test_init() { - init("info", false).expect("Failed to init logger"); + init("info", logging::Mode::Compact).expect("Failed to init logger"); } diff --git a/video/edge/src/config.rs b/video/edge/src/config.rs index 8925f3ad..f1c8f670 100644 --- a/video/edge/src/config.rs +++ b/video/edge/src/config.rs @@ -1,23 +1,7 @@ use std::net::SocketAddr; use anyhow::Result; - -#[derive(Debug, Clone, Default, PartialEq, config::Config)] -#[config(default)] -pub struct TlsConfig { - /// Domain name to use for TLS - /// Only used for gRPC TLS connections - pub domain: Option, - - /// The path to the TLS certificate - pub cert: String, - - /// The path to the TLS private key - pub key: String, - - /// The path to the TLS CA certificate - pub ca_cert: String, -} +use common::config::{LoggingConfig, RedisConfig, TlsConfig}; #[derive(Debug, Clone, PartialEq, config::Config)] #[config(default)] @@ -59,79 +43,6 @@ impl Default for GrpcConfig { } } -#[derive(Debug, Clone, PartialEq, config::Config)] -#[config(default)] -pub struct RedisConfig { - /// The address of the Redis server - pub addresses: Vec, - - /// Number of connections to keep in the pool - pub pool_size: usize, - - /// The username to use for authentication - pub username: Option, - - /// The password to use for authentication - pub password: Option, - - /// The database to use - pub database: u8, - - /// The TLS configuration - pub tls: Option, - - /// To use Redis Sentinel - pub sentinel: Option, -} - -impl Default for RedisConfig { - fn default() -> Self { - Self { - addresses: vec!["localhost:6379".to_string()], - pool_size: 10, - username: None, - password: None, - database: 0, - tls: None, - sentinel: None, - } - } -} - -#[derive(Debug, Clone, PartialEq, config::Config)] -#[config(default)] -pub struct RedisSentinelConfig { - /// The master group name - pub service_name: String, -} - -impl Default for RedisSentinelConfig { - fn default() -> Self { - Self { - service_name: "myservice".to_string(), - } - } -} - -#[derive(Debug, Clone, PartialEq, config::Config)] -#[config(default)] -pub struct LoggingConfig { - /// The log level to use, this is a tracing env filter - pub level: String, - - /// If we should use JSON logging - pub json: bool, -} - -impl Default for LoggingConfig { - fn default() -> Self { - Self { - level: "info".to_string(), - json: false, - } - } -} - #[derive(Debug, Clone, PartialEq, config::Config)] #[config(default)] pub struct AppConfig { diff --git a/video/edge/src/main.rs b/video/edge/src/main.rs index f000a82d..46c18e13 100644 --- a/video/edge/src/main.rs +++ b/video/edge/src/main.rs @@ -14,7 +14,7 @@ mod pb; async fn main() -> Result<()> { let config = config::AppConfig::parse()?; - logging::init(&config.logging.level, config.logging.json)?; + logging::init(&config.logging.level, config.logging.mode)?; if let Some(file) = &config.config_file { tracing::info!(file = file, "loaded config from file"); diff --git a/video/edge/src/tests/global.rs b/video/edge/src/tests/global.rs index a6a79c6d..83366109 100644 --- a/video/edge/src/tests/global.rs +++ b/video/edge/src/tests/global.rs @@ -13,7 +13,7 @@ pub async fn mock_global_state(config: AppConfig) -> (Arc, Handler) dotenvy::dotenv().ok(); - logging::init(&config.logging.level, config.logging.json) + logging::init(&config.logging.level, config.logging.mode) .expect("failed to initialize logging"); let redis = RedisPool::new( diff --git a/video/edge/src/tests/grpc/tls.rs b/video/edge/src/tests/grpc/tls.rs index 94bdbcd7..b7eb9f83 100644 --- a/video/edge/src/tests/grpc/tls.rs +++ b/video/edge/src/tests/grpc/tls.rs @@ -1,10 +1,11 @@ +use common::config::TlsConfig; use common::grpc::{make_channel, TlsSettings}; use common::prelude::FutureTimeout; use std::path::PathBuf; use std::time::Duration; use tonic::transport::{Certificate, Identity}; -use crate::config::{AppConfig, GrpcConfig, TlsConfig}; +use crate::config::{AppConfig, GrpcConfig}; use crate::grpc::run; use crate::tests::global::mock_global_state; diff --git a/video/ingest/src/config.rs b/video/ingest/src/config.rs index d1df122c..3c1c7f9a 100644 --- a/video/ingest/src/config.rs +++ b/video/ingest/src/config.rs @@ -1,23 +1,7 @@ use std::net::SocketAddr; use anyhow::Result; - -#[derive(Debug, Clone, Default, PartialEq, config::Config)] -#[config(default)] -pub struct TlsConfig { - /// Domain name to use for TLS - /// Only used for gRPC TLS connections - pub domain: Option, - - /// The path to the TLS certificate - pub cert: String, - - /// The path to the TLS private key - pub key: String, - - /// The path to the TLS CA certificate - pub ca_cert: String, -} +use common::config::{LoggingConfig, RmqConfig, TlsConfig}; #[derive(Debug, Clone, PartialEq, config::Config)] #[config(default)] @@ -86,40 +70,6 @@ impl Default for ApiConfig { } } -#[derive(Debug, Clone, PartialEq, config::Config)] -#[config(default)] -pub struct RmqConfig { - /// The address of the RMQ server - pub uri: String, -} - -impl Default for RmqConfig { - fn default() -> Self { - Self { - uri: "amqp://rabbitmq:rabbitmq@localhost:5672/scuffle".to_string(), - } - } -} - -#[derive(Debug, Clone, PartialEq, config::Config)] -#[config(default)] -pub struct LoggingConfig { - /// The log level to use, this is a tracing env filter - pub level: String, - - /// If we should use JSON logging - pub json: bool, -} - -impl Default for LoggingConfig { - fn default() -> Self { - Self { - level: "info".to_string(), - json: false, - } - } -} - #[derive(Debug, Clone, PartialEq, config::Config)] #[config(default)] pub struct TranscoderConfig { diff --git a/video/ingest/src/main.rs b/video/ingest/src/main.rs index 7f4a882c..7aa3161d 100644 --- a/video/ingest/src/main.rs +++ b/video/ingest/src/main.rs @@ -15,7 +15,7 @@ mod pb; async fn main() -> Result<()> { let config = config::AppConfig::parse()?; - logging::init(&config.logging.level, config.logging.json)?; + logging::init(&config.logging.level, config.logging.mode)?; if let Some(file) = &config.config_file { tracing::info!(file = file, "loaded config from file"); diff --git a/video/ingest/src/tests/global.rs b/video/ingest/src/tests/global.rs index 24a02685..6dc241d4 100644 --- a/video/ingest/src/tests/global.rs +++ b/video/ingest/src/tests/global.rs @@ -13,7 +13,7 @@ pub async fn mock_global_state(config: AppConfig) -> (Arc, Handler) dotenvy::dotenv().ok(); - logging::init(&config.logging.level, config.logging.json) + logging::init(&config.logging.level, config.logging.mode) .expect("failed to initialize logging"); let rmq = common::rmq::ConnectionPool::connect( diff --git a/video/ingest/src/tests/grpc/tls.rs b/video/ingest/src/tests/grpc/tls.rs index 8b9dcb3a..d4d810f4 100644 --- a/video/ingest/src/tests/grpc/tls.rs +++ b/video/ingest/src/tests/grpc/tls.rs @@ -1,10 +1,11 @@ +use common::config::TlsConfig; use common::grpc::{make_channel, TlsSettings}; use common::prelude::FutureTimeout; use std::path::PathBuf; use std::time::Duration; use tonic::transport::{Certificate, Identity}; -use crate::config::{AppConfig, GrpcConfig, TlsConfig}; +use crate::config::{AppConfig, GrpcConfig}; use crate::grpc::run; use crate::tests::global::mock_global_state; diff --git a/video/ingest/src/tests/ingest.rs b/video/ingest/src/tests/ingest.rs index 85ae62e5..c75cd5e1 100644 --- a/video/ingest/src/tests/ingest.rs +++ b/video/ingest/src/tests/ingest.rs @@ -6,6 +6,7 @@ use std::time::Duration; use async_stream::stream; use async_trait::async_trait; +use common::config::TlsConfig; use common::prelude::FutureTimeout; use futures::StreamExt; use lapin::options::QueueDeclareOptions; @@ -19,7 +20,7 @@ use tonic::{Request, Response, Status}; use transmuxer::MediaType; use uuid::Uuid; -use crate::config::{ApiConfig, AppConfig, RtmpConfig, TlsConfig, TranscoderConfig}; +use crate::config::{ApiConfig, AppConfig, RtmpConfig, TranscoderConfig}; use crate::connection_manager::{GrpcRequest, WatchStreamEvent}; use crate::global; use crate::pb::scuffle::backend::update_live_stream_request::event::Level; diff --git a/video/transcoder/src/config.rs b/video/transcoder/src/config.rs index 2f2c6574..592113c6 100644 --- a/video/transcoder/src/config.rs +++ b/video/transcoder/src/config.rs @@ -1,23 +1,7 @@ use std::net::SocketAddr; use anyhow::Result; - -#[derive(Debug, Clone, Default, PartialEq, config::Config)] -#[config(default)] -pub struct TlsConfig { - /// Domain name to use for TLS - /// Only used for gRPC TLS connections - pub domain: Option, - - /// The path to the TLS certificate - pub cert: String, - - /// The path to the TLS private key - pub key: String, - - /// The path to the TLS CA certificate - pub ca_cert: String, -} +use common::config::{LoggingConfig, RedisConfig, RmqConfig, TlsConfig}; #[derive(Debug, Clone, PartialEq, config::Config)] #[config(default)] @@ -69,104 +53,15 @@ pub struct IngestConfig { pub tls: Option, } -#[derive(Debug, Clone, PartialEq, config::Config)] -#[config(default)] -pub struct RmqConfig { - /// URI for RMQ - pub uri: String, - - /// Stream name used for transcoder requests - pub transcoder_queue: String, -} - -impl Default for RmqConfig { - fn default() -> Self { - Self { - uri: "amqp://rabbitmq:rabbitmq@localhost:5672/scuffle".to_string(), - transcoder_queue: "transcoder".to_string(), - } - } -} - -#[derive(Debug, Clone, PartialEq, config::Config)] -#[config(default)] -pub struct RedisConfig { - /// The address of the Redis server - pub addresses: Vec, - - /// Number of connections to keep in the pool - pub pool_size: usize, - - /// The username to use for authentication - pub username: Option, - - /// The password to use for authentication - pub password: Option, - - /// The database to use - pub database: u8, - - /// The TLS configuration - pub tls: Option, - - /// To use Redis Sentinel - pub sentinel: Option, -} - -impl Default for RedisConfig { - fn default() -> Self { - Self { - addresses: vec!["localhost:6379".to_string()], - pool_size: 10, - username: None, - password: None, - database: 0, - tls: None, - sentinel: None, - } - } -} - -#[derive(Debug, Clone, PartialEq, config::Config)] -#[config(default)] -pub struct RedisSentinelConfig { - /// The master group name - pub service_name: String, -} - -impl Default for RedisSentinelConfig { - fn default() -> Self { - Self { - service_name: "myservice".to_string(), - } - } -} - -#[derive(Debug, Clone, PartialEq, config::Config)] -#[config(default)] -pub struct LoggingConfig { - /// The log level to use, this is a tracing env filter - pub level: String, - - /// If we should use JSON logging - pub json: bool, -} - -impl Default for LoggingConfig { - fn default() -> Self { - Self { - level: "info".to_string(), - json: false, - } - } -} - #[derive(Debug, Clone, PartialEq, config::Config)] #[config(default)] pub struct TranscoderConfig { /// The direcory to create unix sockets in pub socket_dir: String, + /// The name of the RMQ queue to use + pub rmq_queue: String, + /// The uid to use for the unix socket and ffmpeg process pub uid: u32, /// The gid to use for the unix socket and ffmpeg process @@ -177,6 +72,7 @@ impl Default for TranscoderConfig { fn default() -> Self { Self { socket_dir: "/tmp".to_string(), + rmq_queue: "transcoder".to_string(), uid: 1000, gid: 1000, } diff --git a/video/transcoder/src/global.rs b/video/transcoder/src/global.rs index d93801f5..bd99cdb1 100644 --- a/video/transcoder/src/global.rs +++ b/video/transcoder/src/global.rs @@ -44,7 +44,7 @@ pub async fn init_rmq(global: &Arc, durable: bool) { channel .queue_declare( - &global.config.rmq.transcoder_queue, + &global.config.transcoder.rmq_queue, QueueDeclareOptions { durable, ..Default::default() diff --git a/video/transcoder/src/main.rs b/video/transcoder/src/main.rs index 60d37997..0f71374c 100644 --- a/video/transcoder/src/main.rs +++ b/video/transcoder/src/main.rs @@ -14,7 +14,7 @@ mod transcoder; async fn main() -> Result<()> { let config = config::AppConfig::parse()?; - logging::init(&config.logging.level, config.logging.json)?; + logging::init(&config.logging.level, config.logging.mode)?; if let Some(file) = &config.config_file { tracing::info!(file = file, "loaded config from file"); diff --git a/video/transcoder/src/tests/global.rs b/video/transcoder/src/tests/global.rs index d5b6c194..e5debd9d 100644 --- a/video/transcoder/src/tests/global.rs +++ b/video/transcoder/src/tests/global.rs @@ -14,7 +14,7 @@ pub async fn mock_global_state(config: AppConfig) -> (Arc, Handler) dotenvy::dotenv().ok(); - logging::init(&config.logging.level, config.logging.json) + logging::init(&config.logging.level, config.logging.mode) .expect("failed to initialize logging"); let rmq = common::rmq::ConnectionPool::connect( diff --git a/video/transcoder/src/tests/grpc/tls.rs b/video/transcoder/src/tests/grpc/tls.rs index 94bdbcd7..b7eb9f83 100644 --- a/video/transcoder/src/tests/grpc/tls.rs +++ b/video/transcoder/src/tests/grpc/tls.rs @@ -1,10 +1,11 @@ +use common::config::TlsConfig; use common::grpc::{make_channel, TlsSettings}; use common::prelude::FutureTimeout; use std::path::PathBuf; use std::time::Duration; use tonic::transport::{Certificate, Identity}; -use crate::config::{AppConfig, GrpcConfig, TlsConfig}; +use crate::config::{AppConfig, GrpcConfig}; use crate::grpc::run; use crate::tests::global::mock_global_state; diff --git a/video/transcoder/src/tests/transcoder/mod.rs b/video/transcoder/src/tests/transcoder/mod.rs index 9646d95c..7389f10f 100644 --- a/video/transcoder/src/tests/transcoder/mod.rs +++ b/video/transcoder/src/tests/transcoder/mod.rs @@ -10,6 +10,7 @@ use std::{ use async_trait::async_trait; use bytes::{Buf, Bytes}; use chrono::Utc; +use common::{config::LoggingConfig, logging}; use fred::prelude::{HashesInterface, KeysInterface}; use futures_util::Stream; use lapin::BasicProperties; @@ -22,7 +23,7 @@ use transmuxer::{MediaType, TransmuxResult, Transmuxer}; use uuid::Uuid; use crate::{ - config::{AppConfig, LoggingConfig, RmqConfig}, + config::{AppConfig, TranscoderConfig}, global::{self, GlobalState}, pb::scuffle::{ events::{self, transcoder_message}, @@ -135,13 +136,13 @@ async fn test_transcode() { let port = portpicker::pick_unused_port().unwrap(); let (global, handler) = crate::tests::global::mock_global_state(AppConfig { - rmq: RmqConfig { - transcoder_queue: Uuid::new_v4().to_string(), - uri: "".to_string(), + transcoder: TranscoderConfig { + rmq_queue: Uuid::new_v4().to_string(), + ..Default::default() }, logging: LoggingConfig { level: "info,transcoder=debug".to_string(), - json: false, + mode: logging::Mode::Default, }, ..Default::default() }) @@ -167,7 +168,7 @@ async fn test_transcode() { channel .basic_publish( "", - &global.config.rmq.transcoder_queue, + &global.config.transcoder.rmq_queue, lapin::options::BasicPublishOptions::default(), events::TranscoderMessage { id: req_id.to_string(), diff --git a/video/transcoder/src/transcoder/mod.rs b/video/transcoder/src/transcoder/mod.rs index 00a1359b..01844798 100644 --- a/video/transcoder/src/transcoder/mod.rs +++ b/video/transcoder/src/transcoder/mod.rs @@ -12,7 +12,7 @@ pub(crate) mod job; pub async fn run(global: Arc) -> Result<()> { let mut consumer = pin!(global.rmq.basic_consume( - &global.config.rmq.transcoder_queue, + &global.config.transcoder.rmq_queue, &global.config.name, BasicConsumeOptions::default(), FieldTable::default()