use anyhow::{Result, bail}; use clap::{ArgMatches, Args, ValueEnum, parser::ValueSource}; use object_store::{ ClientOptions, ObjectStore, aws::AmazonS3Builder, http::HttpBuilder, local::LocalFileSystem, memory::InMemory, prefix::PrefixStore, }; use serde::{Deserialize, Serialize}; use std::{net::SocketAddr, path::PathBuf, sync::Arc}; #[derive(Clone, Copy, Debug, ValueEnum, Deserialize, Serialize)] #[serde(rename_all = "lowercase")] pub enum Backend { S3, File, Memory, } /// Server configuration. Credentials are read through object_store's AWS provider chain. #[derive(Clone, Debug, Args, Deserialize, Serialize)] #[serde(deny_unknown_fields)] pub struct Config { /// Optional TOML file. Environment variables and explicit flags override it. #[arg(long = "config", env = "FLUENTCI_CACHE_CONFIG")] #[serde(skip)] pub config_file: Option, #[arg(long, env = "FLUENTCI_CACHE_BIND", default_value = "127.0.0.1:8080")] pub bind: SocketAddr, #[arg(long, env = "FLUENTCI_CACHE_BACKEND", value_enum, default_value = "s3")] pub backend: Backend, #[arg(long, env = "FLUENTCI_CACHE_BUCKET")] pub bucket: Option, #[arg(long, env = "FLUENTCI_CACHE_ENDPOINT")] pub endpoint: Option, /// Public CDN base URL pointing to the S3 bucket root. The storage prefix is appended. #[arg(long, env = "FLUENTCI_CACHE_CDN_URL")] pub cdn_url: Option, /// Trusted XRPC endpoint for resolving verified handle aliases. #[arg( long, env = "FLUENTCI_CACHE_IDENTITY_RESOLVER_URL", default_value = "https://api.fluentci.io/xrpc/io.fluentci.cache.resolveAccount" )] pub identity_resolver_url: String, #[arg(long, env = "FLUENTCI_CACHE_REGION", default_value = "us-east-1")] pub region: String, #[arg(long, env = "FLUENTCI_CACHE_ALLOW_HTTP", default_value_t = false, action = clap::ArgAction::Set, num_args = 0..=1, default_missing_value = "true")] pub allow_http: bool, #[arg( long, env = "FLUENTCI_CACHE_PREFIX", default_value = "fluentci-cache/v1" )] pub prefix: String, #[arg(long, env = "FLUENTCI_CACHE_DIRECTORY", default_value = "./cache-data")] pub directory: PathBuf, /// SQLite key index file. Omit to use an isolated in-memory SQLite index. #[arg(long, env = "FLUENTCI_CACHE_INDEX_FILE")] pub index_file: Option, /// Bearer token, or password with any Basic-auth username. Never logged. #[arg(long, env = "FLUENTCI_CACHE_TOKEN", hide_env_values = true)] pub token: Option, /// Ed25519 public PEM for account-scoped upload JWTs minted by the API. #[arg(long, env = "FLUENTCI_CACHE_UPLOAD_TOKEN_PUBLIC_KEY_FILE")] pub upload_token_public_key_file: Option, #[arg( long, env = "FLUENTCI_CACHE_UPLOAD_TOKEN_ISSUER", default_value = "did:web:api.fluentci.io" )] pub upload_token_issuer: String, #[arg(long, env = "FLUENTCI_CACHE_PUBLIC_READ", default_value_t = true, action = clap::ArgAction::Set)] pub public_read: bool, #[arg(long, env = "FLUENTCI_CACHE_READ_ONLY", default_value_t = false, action = clap::ArgAction::Set, num_args = 0..=1, default_missing_value = "true")] pub read_only: bool, #[arg(long, env = "FLUENTCI_CACHE_MAX_UPLOAD", default_value_t = 10 * 1024 * 1024 * 1024_u64)] pub max_upload: u64, /// Total temporary disk bytes reserved by resumable gRPC and OCI uploads. #[arg(long, env = "FLUENTCI_CACHE_STAGING_LIMIT", default_value_t = 20 * 1024 * 1024 * 1024_u64)] pub staging_limit: u64, /// Optional directory for resumable upload staging; defaults to the OS temporary directory. #[arg(long, env = "FLUENTCI_CACHE_STAGING_DIRECTORY")] pub staging_directory: Option, #[arg(long, env = "FLUENTCI_CACHE_CONCURRENCY", default_value_t = 128)] pub concurrency: usize, /// Entire upload deadline and per-chunk download idle timeout. #[arg(long, env = "FLUENTCI_CACHE_TIMEOUT", default_value_t = 300)] pub timeout: u64, #[arg(long, env = "FLUENTCI_CACHE_NIX_PRIORITY", default_value_t = 40)] pub nix_priority: u32, } impl Config { /// Merge a partial TOML file with CLI defaults, then apply explicit env/CLI values. pub fn resolve(self, matches: &ArgMatches) -> Result { let Some(path) = &self.config_file else { self.validate()?; return Ok(self); }; let table: toml::Table = toml::from_str(&std::fs::read_to_string(path)?)?; let mut merged = serde_json::to_value(&self)?; let cli = merged.clone(); let target = merged.as_object_mut().expect("configuration object"); for (key, value) in table { if !target.contains_key(&key) { bail!("unknown cache configuration key: {key}"); } target.insert(key, serde_json::to_value(value)?); } for key in target.keys().cloned().collect::>() { if matches!( matches.value_source(&key), Some(ValueSource::CommandLine | ValueSource::EnvVariable) ) { target.insert(key.clone(), cli[&key].clone()); } } let resolved: Self = serde_json::from_value(merged)?; resolved.validate()?; Ok(resolved) } pub fn validate(&self) -> Result<()> { let resolver = reqwest::Url::parse(&self.identity_resolver_url)?; if !(resolver.scheme() == "https" || self.allow_http && resolver.scheme() == "http") || resolver.host_str().is_none() || !resolver.username().is_empty() || resolver.password().is_some() || resolver.query().is_some() || resolver.fragment().is_some() { bail!( "identity_resolver_url must be a trusted HTTPS endpoint without credentials or query parameters" ); } if self.concurrency == 0 || self.timeout == 0 || self.max_upload == 0 || self.staging_limit == 0 { bail!("concurrency, timeout, max-upload, and staging-limit must be positive"); } if self .token .as_ref() .is_some_and(|s| s.is_empty() || s.contains(['\r', '\n'])) { bail!("token must be nonempty and contain no line breaks"); } if !self.read_only && self.token.is_none() { bail!( "a token is required for a writable cache; set FLUENTCI_CACHE_TOKEN or use --read-only" ); } if !self.public_read && self.token.is_none() { bail!("a token is required for private reads"); } if matches!(self.backend, Backend::S3) && self.cdn_url.is_none() { bail!("cdn_url is required for S3 reads; set FLUENTCI_CACHE_CDN_URL or --cdn-url"); } if let Some(value) = &self.cdn_url { let url = reqwest::Url::parse(value) .map_err(|_| anyhow::anyhow!("cdn_url must be an absolute public HTTP(S) URL"))?; if !matches!(url.scheme(), "http" | "https") || url.host_str().is_none() || !url.username().is_empty() || url.password().is_some() || url.query().is_some() || url.fragment().is_some() { bail!( "cdn_url must be an absolute public HTTP(S) URL without credentials, query, or fragment" ); } } Ok(()) } /// S3 reads use the public CDN. Other backends read from the supplied write store. pub fn read_storage(&self, write_store: Arc) -> Result> { self.validate()?; if !matches!(self.backend, Backend::S3) { return Ok(write_store); } let url = reqwest::Url::parse(self.cdn_url.as_deref().expect("validated S3 CDN URL"))?; let store = HttpBuilder::new() .with_url(url.as_str().trim_end_matches('/')) .with_client_options(ClientOptions::new().with_allow_http(url.scheme() == "http")) .build()?; Ok(Arc::new(PrefixStore::new( store, object_store::path::Path::parse(&self.prefix)?, ))) } pub fn storage(&self) -> Result> { self.validate()?; let store: Arc = match self.backend { Backend::S3 => { let mut builder = AmazonS3Builder::from_env() .with_bucket_name( self.bucket .as_deref() .ok_or_else(|| anyhow::anyhow!("--bucket is required for S3"))?, ) .with_region(&self.region) .with_allow_http(self.allow_http) .with_virtual_hosted_style_request(false); if let Some(endpoint) = &self.endpoint { builder = builder.with_endpoint(endpoint); } Arc::new(builder.build()?) } Backend::File => { std::fs::create_dir_all(&self.directory)?; Arc::new(LocalFileSystem::new_with_prefix(&self.directory)?) } Backend::Memory => Arc::new(InMemory::new()), }; let prefix = object_store::path::Path::parse(&self.prefix)?; Ok(Arc::new(PrefixStore::new(store, prefix))) } }