Skip to content

dimensionalpocket/dps-config-rs

Repository files navigation

@dimensionalpocket/dps-config

Rust Tests License: MIT

Configuration management for the DPS ecosystem.

Overview

This repo provides the DpsConfig struct, a lightweight configuration container used by Rust components in the DPS ecosystem. It focuses on optional values, sensible defaults, environment variable loading, and computed getters.

Key principles:

  • No validation in the struct; consuming crates perform validation.
  • Most getters provide hardcoded defaults suitable for development.
  • Environment variables are used to populate properties automatically.
  • Computed getters derive combined values (URLs, domains) from base properties.

Installation

Add it to Cargo.toml:

[dependencies]
dps-config = { git = "https://github.com/dimensionalpocket/dps-config-rs" }

Or add via cargo:

cargo add --git https://github.com/dimensionalpocket/dps-config-rs dps-config

Quick Start

Basic usage example:

use dps_config::DpsConfig;

fn main() {
    let mut config = DpsConfig::new();

    // defaults
    let domain = config.get_domain();
    let api_path = config.get_api_path();

    // overrides
    config.set_domain("example.com");
    config.set_api_path("v1");
    config.set_development_mode(true);

    let auth_api_url = config.get_auth_api_url();
    println!("Auth API URL: {}", auth_api_url);
}

Configuration Properties

The following properties are provided. Properties load from environment variables when present.
Each property has a getter (get_<property_name>()) and a setter (set_<property_name>(value)).

Global

Property Environment Variable Default Description
domain DPS_DOMAIN dps.localhost Main domain of the website
api_path DPS_API_PATH api Path (without leading slash) for API endpoints
development_mode DPS_DEVELOPMENT_MODE false Enables development-only features

DpsAuthApi

Property Environment Variable Default Description
auth_api_subdomain DPS_AUTH_API_SUBDOMAIN auth Sub-subdomain for DpsAuthApi
auth_api_port DPS_AUTH_API_PORT none Port for DpsAuthApi (omitted from URL if unset)
auth_api_protocol DPS_AUTH_API_PROTOCOL https Protocol for DpsAuthApi
auth_api_insecure_cookie DPS_AUTH_API_INSECURE_COOKIE false Allow insecure cookies (HTTP)
auth_api_sqlite_main_file_path DPS_AUTH_API_SQLITE_MAIN_FILE_PATH data/main-development.db SQLite main database file path
auth_api_sqlite_main_pool_size DPS_AUTH_API_SQLITE_MAIN_POOL_SIZE 1 SQLite main database connection pool size
auth_api_session_secret DPS_AUTH_API_SESSION_SECRET none 32-byte session secret for encryption
auth_api_session_ttl_seconds DPS_AUTH_API_SESSION_TTL_SECONDS 1209600 (14 days) Session TTL in seconds

Computed Getters

Computed getters derive values from base properties and have no setters or environment variables.

  • get_auth_api_url() — returns {protocol}://{auth_api_subdomain}.{domain}/{api_path} (with :{port} appended after domain when port is set)
  • get_auth_api_session_secret_bytes() — returns session secret as Vec<u8> for encryption libraries
let mut c = DpsConfig::new();
c.set_api_path("v1");
c.set_domain("dps.localhost");
assert_eq!(c.get_auth_api_url(), "https://auth.dps.localhost/v1");

// Session secret as bytes (convenient for encryption libraries)
c.set_auth_api_session_secret(Some("my-32-byte-secret-key-here!!!"));
if let Some(secret_bytes) = c.get_auth_api_session_secret_bytes() {
    assert_eq!(secret_bytes.len(), 32);
}

Environment Variables

Properties auto-load from environment variables when DpsConfig::new() is called. Boolean true is expressed as "Y" in environment variables.

Example (development):

export DPS_DOMAIN="dps.localhost"
export DPS_API_PATH="api"
export DPS_DEVELOPMENT_MODE="Y"
export DPS_AUTH_API_PROTOCOL="http"
export DPS_AUTH_API_PORT="3000"
export DPS_AUTH_API_INSECURE_COOKIE="Y"
export DPS_AUTH_API_SQLITE_MAIN_FILE_PATH="data/main-development.db"
export DPS_AUTH_API_SQLITE_MAIN_POOL_SIZE="4"
export DPS_AUTH_API_SESSION_SECRET="dev-secret-key-32-bytes-long!"
export DPS_AUTH_API_SESSION_TTL_SECONDS="1209600"

Usage Examples

use dps_config::DpsConfig;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn auth_url_builds() {
        let mut c = DpsConfig::new();
        c.set_domain("test.local");
        c.set_api_path("v1");
        c.set_auth_api_protocol("http");
        c.set_auth_api_port(Some(8080));
        assert_eq!(c.get_auth_api_url(), "http://auth.test.local:8080/v1");
    }

    #[test]
    fn auth_session_ttl_examples() {
        let mut c = DpsConfig::new();

        // default is 14 days in seconds
        assert_eq!(c.get_auth_api_session_ttl_seconds(), 1209600);

        c.set_auth_api_session_ttl_seconds(Some(3600));
        assert_eq!(c.get_auth_api_session_ttl_seconds(), 3600);
    }
}

Project Structure

dps-config-rs/
├── src/          # main library code with DpsConfig
├── docs/         # documentation (LLM instructions, plans, drafts, etc.)
├── Cargo.toml
└── README.md

License

MIT

About

Config struct for the DPS ecosystem.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages