Skip to content

Commit

Permalink
feat: serde
Browse files Browse the repository at this point in the history
  • Loading branch information
TroyKomodo committed Jul 6, 2023
1 parent 9a5419f commit 96c70e4
Show file tree
Hide file tree
Showing 25 changed files with 961 additions and 919 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 15 additions & 14 deletions backend/api/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use std::net::SocketAddr;

use anyhow::Result;
use common::config::{LoggingConfig, RedisConfig, RmqConfig, TlsConfig};
use config::KeyTree;

#[derive(Debug, Clone, PartialEq, config::Config)]
#[config(default)]
#[derive(Debug, Clone, PartialEq, config::Config, serde::Deserialize, serde::Serialize)]
#[serde(default)]
/// The API is the backend for the Scuffle service
pub struct AppConfig {
/// The path to the config file
Expand Down Expand Up @@ -38,11 +39,11 @@ pub struct AppConfig {
pub redis: RedisConfig,
}

#[derive(Debug, Clone, PartialEq, config::Config)]
#[config(default)]
#[derive(Debug, Clone, PartialEq, config::Config, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct ApiConfig {
/// Bind address for the API
#[config(from_str)]
#[config(tree = "KeyTree::String")]
pub bind_address: SocketAddr,

/// If we should use TLS for the API server
Expand All @@ -58,8 +59,8 @@ impl Default for ApiConfig {
}
}

#[derive(Debug, Clone, PartialEq, config::Config)]
#[config(default)]
#[derive(Debug, Clone, PartialEq, config::Config, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct DatabaseConfig {
/// The database URL to use
pub uri: String,
Expand All @@ -73,8 +74,8 @@ impl Default for DatabaseConfig {
}
}

#[derive(Debug, Clone, PartialEq, config::Config)]
#[config(default)]
#[derive(Debug, Clone, PartialEq, config::Config, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct TurnstileConfig {
/// The Cloudflare Turnstile site key to use
pub secret_key: String,
Expand All @@ -92,8 +93,8 @@ impl Default for TurnstileConfig {
}
}

#[derive(Debug, Clone, PartialEq, config::Config)]
#[config(default)]
#[derive(Debug, Clone, PartialEq, config::Config, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct JwtConfig {
/// JWT secret
pub secret: String,
Expand All @@ -111,11 +112,11 @@ impl Default for JwtConfig {
}
}

#[derive(Debug, Clone, PartialEq, config::Config)]
#[config(default)]
#[derive(Debug, Clone, PartialEq, config::Config, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct GrpcConfig {
/// Bind address for the GRPC server
#[config(from_str)]
#[config(tree = "KeyTree::String")]
pub bind_address: SocketAddr,

/// If we should use TLS for the gRPC server
Expand Down
4 changes: 2 additions & 2 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ authors = ["Scuffle <[email protected]>"]
description = "Scuffle Common Library"

[features]
logging = ["dep:log", "dep:tracing", "dep:tracing-log", "dep:tracing-subscriber", "dep:arc-swap", "dep:anyhow", "dep:once_cell", "dep:thiserror"]
logging = ["dep:log", "dep:tracing", "dep:tracing-log", "dep:tracing-subscriber", "dep:arc-swap", "dep:anyhow", "dep:once_cell", "dep:thiserror", "dep:serde"]
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", "logging"]
config = ["dep:config", "dep:serde", "logging"]

default = ["logging", "rmq", "grpc", "context", "prelude", "signal", "macros", "config"]

Expand Down
31 changes: 17 additions & 14 deletions common/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use anyhow::Result;

use crate::logging;
use config::KeyTree;

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

/// The path to the TLS certificate
Expand All @@ -19,14 +22,14 @@ pub struct TlsConfig {
pub ca_cert: String,
}

#[derive(Debug, Clone, PartialEq, config::Config)]
#[config(default)]
#[derive(Debug, Clone, PartialEq, config::Config, serde::Deserialize, serde::Serialize)]
#[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
#[config(from_str)]
#[config(tree = "KeyTree::String")]
pub mode: logging::Mode,
}

Expand All @@ -39,8 +42,8 @@ impl Default for LoggingConfig {
}
}

#[derive(Debug, Clone, PartialEq, config::Config)]
#[config(default)]
#[derive(Debug, Clone, PartialEq, config::Config, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct RedisConfig {
/// The address of the Redis server
pub addresses: Vec<String>,
Expand All @@ -64,15 +67,15 @@ pub struct RedisConfig {
pub sentinel: Option<RedisSentinelConfig>,
}

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

#[derive(Debug, Clone, PartialEq, config::Config)]
#[config(default)]
#[derive(Debug, Clone, PartialEq, config::Config, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct RmqConfig {
/// The URI to use for connecting to RabbitMQ
pub uri: String,
Expand Down Expand Up @@ -115,12 +118,12 @@ pub fn parse<C: config::Config + 'static>(
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::CliSource::new()?, 3);
}

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

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

let key_provided = key.is_some();

Expand Down
2 changes: 1 addition & 1 deletion common/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tracing_subscriber::{prelude::*, reload::Handle, EnvFilter};

static RELOAD_HANDLE: OnceCell<Handle<EnvFilter>> = OnceCell::new();

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub enum Mode {
Default,
Json,
Expand Down
3 changes: 2 additions & 1 deletion config/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ serde_yaml = "0"
toml = "0"
clap = { version = "4", features = ["cargo", "string"] }
convert_case = "0"

serde = { version = "1", features = ["derive"] }
serde-value = "0"
tracing = { version = "0" }

# Derive macro
Expand Down
28 changes: 6 additions & 22 deletions config/config/example/derive.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
//! Run with: `cargo run --example derive`
//! Look at the generated code with: `cargo expand --example derive`

use std::net::SocketAddr;
use std::str::FromStr;

use config::{sources, ConfigBuilder};

type TypeAlias = bool;

#[derive(config::Config, Debug, PartialEq)]
#[config(default)]
#[derive(config::Config, Debug, PartialEq, serde::Deserialize, serde::Serialize, Default)]
#[serde(default)]
struct AppConfig {
enabled: TypeAlias,
logging: LoggingConfig,
optional: Option<()>,
#[config(from_str = "SocketAddr::from_str")]
bind_address: Option<SocketAddr>,
}

impl Default for AppConfig {
fn default() -> Self {
Self {
enabled: false,
logging: LoggingConfig::default(),
optional: None,
bind_address: Some("127.0.0.1:5000".parse().unwrap()),
}
}
optional: Option<LoggingConfig>,
}

#[derive(config::Config, Debug, PartialEq)]
#[config(default)]
#[derive(config::Config, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(default)]
struct LoggingConfig {
level: String,
json: bool,
Expand All @@ -47,7 +31,7 @@ impl Default for LoggingConfig {

fn main() {
let mut builder: ConfigBuilder<AppConfig> = ConfigBuilder::new();
builder.add_source(sources::CliSource::new());
builder.add_source(sources::CliSource::new().unwrap());

match builder.build() {
Ok(config) => println!("{:?}", config),
Expand Down
Loading

0 comments on commit 96c70e4

Please sign in to comment.