Skip to content

Commit

Permalink
feat(refactor): refactor for crates.io
Browse files Browse the repository at this point in the history
Published crates to crates.io

`scuffle-config`
`scuffle-utils`
  • Loading branch information
TroyKomodo committed Jan 30, 2024
1 parent 549cfff commit 430475d
Show file tree
Hide file tree
Showing 223 changed files with 1,423 additions and 1,451 deletions.
452 changes: 217 additions & 235 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ members = [
"video/common",
"video/cli",
"binary-helper",
"common",
"utils",
"proto",
"config",
"config/derive",
Expand Down Expand Up @@ -46,8 +46,8 @@ h265 = { path = "video/lib/h265" }
mp4 = { path = "video/lib/mp4" }
rtmp = { path = "video/lib/rtmp" }
transmuxer = { path = "video/lib/transmuxer" }
common = { path = "common", default-features = false }
config = { path = "config" }
utils = { path = "utils", default-features = false, package = "scuffle-utils" }
config = { path = "config", package = "scuffle-config" }
pb = { path = "proto" }
video-common = { path = "video/common" }
video-player-types = { path = "video/player_types" }
Expand Down
18 changes: 17 additions & 1 deletion binary-helper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,23 @@ rustls = "0.22"
rustls-pemfile = "2.0"
fred = { version = "8.0.0", features = ["enable-rustls", "sentinel-client", "dns"] }
tokio-postgres-rustls = "0.11"
tracing-subscriber = { features = ["env-filter", "fmt", "json"], version = "0.3" }
once_cell = "1.19"
aws-config = { version = "1.1" }
aws-sdk-s3 = { version = "1.12", features = ["behavior-version-latest"] }
aws-credential-types = { version = "1.1", features = ["hardcoded-credentials"] }
aws-smithy-types = { version = "1.1", features = ["http-body-1-x"] }
http-body = { version = "1.0.0"}
hyper = "1"
bytes = "1.0"
pin-project = "1"

tokio-postgres = { version = "0.7" }
postgres-types = { version = "0.2", features = ["with-serde_json-1", "with-chrono-0_4", "derive"] }
deadpool-postgres = { version = "0.12" }
postgres-from-row = { version = "0.5" }
prost = { version = "0.12" }

config = { workspace = true }
common = { workspace = true, features = ["default"] }
utils = { workspace = true, features = ["all"] }
pb = { workspace = true }
246 changes: 244 additions & 2 deletions binary-helper/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use common::config::{DatabaseConfig, GrpcConfig, LoggingConfig, NatsConfig};
use std::{net::SocketAddr, sync::Arc};

use crate::logging;

use super::Config;

Expand Down Expand Up @@ -28,6 +30,206 @@ pub struct AppConfig<T: ConfigExtention> {
pub extra: T,
}

#[derive(Debug, Clone, Default, PartialEq, config::Config, serde::Deserialize)]
#[serde(default)]
pub struct TlsConfig {
/// Domain name to use for TLS
/// Only used for gRPC TLS connections
pub domain: Option<String>,

/// 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: Option<String>,
}

#[derive(Debug, Clone, PartialEq, config::Config, serde::Deserialize)]
#[serde(default)]
pub struct LoggingConfig {
/// The log level to use, this is a tracing env filter
pub level: String,

/// What logging mode we should use
pub mode: logging::Mode,
}

impl ::config::Config for logging::Mode {
fn graph() -> Arc<::config::KeyGraph> {
Arc::new(::config::KeyGraph::String)
}
}

impl Default for LoggingConfig {
fn default() -> Self {
Self {
level: "info".to_string(),
mode: logging::Mode::Default,
}
}
}

#[derive(Debug, Clone, PartialEq, config::Config, serde::Deserialize)]
#[serde(default)]
pub struct RedisConfig {
/// The address of the Redis server
pub addresses: Vec<String>,

/// Number of connections to keep in the pool
pub pool_size: usize,

/// The username to use for authentication
pub username: Option<String>,

/// The password to use for authentication
pub password: Option<String>,

/// The database to use
pub database: u8,

/// The TLS configuration
pub tls: Option<TlsConfig>,

/// To use Redis Sentinel
pub sentinel: Option<RedisSentinelConfig>,
}

#[derive(Debug, Clone, PartialEq, config::Config, serde::Deserialize)]
#[serde(default)]
pub struct RedisSentinelConfig {
/// The master group name
pub service_name: String,
}

#[derive(Debug, Clone, PartialEq, config::Config, serde::Deserialize)]
#[serde(default)]
pub struct NatsConfig {
/// The URI to use for connecting to Nats
pub servers: Vec<String>,

/// The username to use for authentication (user-pass auth)
pub username: Option<String>,

/// The password to use for authentication (user-pass auth)
pub password: Option<String>,

/// The token to use for authentication (token auth)
pub token: Option<String>,

/// The TLS configuration (can be used for mTLS)
pub tls: Option<TlsConfig>,
}

impl Default for NatsConfig {
fn default() -> Self {
Self {
servers: vec!["localhost:4222".into()],
token: None,
password: None,
tls: None,
username: None,
}
}
}

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,
}
}
}

#[derive(Debug, Clone, PartialEq, config::Config, serde::Deserialize)]
pub struct DatabaseConfig {
/// The database URL to use
pub uri: String,

/// The TLS configuration
pub tls: Option<TlsConfig>,
}

impl Default for DatabaseConfig {
fn default() -> Self {
Self {
uri: "postgres://localhost:5432".to_string(),
tls: None,
}
}
}

#[derive(Debug, Clone, PartialEq, config::Config, serde::Deserialize)]
#[serde(default)]
pub struct GrpcConfig {
/// The bind address for the gRPC server
pub bind_address: SocketAddr,

/// If we should use TLS for the gRPC server
pub tls: Option<TlsConfig>,
}

impl Default for GrpcConfig {
fn default() -> Self {
Self {
bind_address: "[::]:50055".to_string().parse().unwrap(),
tls: None,
}
}
}

#[derive(Debug, Default, Clone, PartialEq, config::Config, serde::Deserialize)]
pub struct S3CredentialsConfig {
/// The access key for the S3 bucket
pub access_key: Option<String>,

/// The secret key for the S3 bucket
pub secret_key: Option<String>,
}

#[derive(Debug, Clone, PartialEq, config::Config, serde::Deserialize)]
#[serde(default)]
pub struct S3BucketConfig {
/// The name of the S3 bucket
pub name: String,

/// The region the S3 bucket is in
pub region: String,

/// The custom endpoint for the S3 bucket
pub endpoint: Option<String>,

/// The credentials for the S3 bucket
pub credentials: S3CredentialsConfig,
}

impl Default for S3BucketConfig {
fn default() -> Self {
Self {
name: "scuffle".to_owned(),
region: "us-east-1".to_owned(),
endpoint: Some("http://localhost:9000".to_string()),
credentials: S3CredentialsConfig::default(),
}
}
}

pub trait ConfigExtention: config::Config + Default {
const APP_NAME: &'static str;

Expand Down Expand Up @@ -63,7 +265,7 @@ impl<'de, T: ConfigExtention + serde::Deserialize<'de>> Config for AppConfig<T>
where
Self: Sized,
{
let (mut config, config_file) = common::config::parse::<Self>(!cfg!(test), Self::default().config_file)?;
let (mut config, config_file) = parse::<Self>(!cfg!(test), Self::default().config_file)?;

config.config_file = config_file;

Expand All @@ -78,3 +280,43 @@ impl<'de, T: ConfigExtention + serde::Deserialize<'de>> Config for AppConfig<T>
T::pre_hook(self)
}
}

pub fn parse<'de, C: config::Config + serde::Deserialize<'de> + 'static>(
enable_cli: bool,
config_file: Option<String>,
) -> config::Result<(C, Option<String>)> {
let mut builder = config::ConfigBuilder::new();

if enable_cli {
builder.add_source_with_priority(config::sources::CliSource::new()?, 3);
}

builder.add_source_with_priority(config::sources::EnvSource::with_prefix("SCUF")?, 2);

let key = builder.parse_key::<Option<String>>("config_file")?;

let key_provided = key.is_some();

let mut config_path = None;

if let Some(path) = key.or(config_file) {
match config::sources::FileSource::with_path(path) {
Ok(source) => {
config_path = Some(source.location().to_string());
builder.add_source_with_priority(source, 1);
}
Err(err) => {
if key_provided || !err.is_io() {
return Err(err);
}

tracing::debug!("failed to load config file: {}", err);
}
}
}

Ok((
builder.build()?,
config_path.map(|p| std::fs::canonicalize(p).unwrap().display().to_string()),
))
}
Loading

0 comments on commit 430475d

Please sign in to comment.