0.7.27 +Refactor
This commit is contained in:
@@ -4,33 +4,33 @@
|
||||
|
||||
/// Root application configuration loaded from `config.json`.
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KbConfig {
|
||||
pub struct Config {
|
||||
/// Application-level metadata and global behavior.
|
||||
pub app: KbAppConfig,
|
||||
pub app: AppConfig,
|
||||
/// Tracing and log output configuration.
|
||||
pub logging: KbLoggingConfig,
|
||||
pub logging: LoggingConfig,
|
||||
/// Data directory configuration.
|
||||
pub data: KbDataConfig,
|
||||
pub data: DataConfig,
|
||||
/// Solana endpoint configuration.
|
||||
pub solana: KbSolanaConfig,
|
||||
pub solana: SolanaConfig,
|
||||
/// Database configuration.
|
||||
pub database: KbDatabaseConfig,
|
||||
pub database: DatabaseConfig,
|
||||
}
|
||||
|
||||
impl KbConfig {
|
||||
impl Config {
|
||||
/// Returns the default path of the JSON configuration file.
|
||||
pub fn default_path() -> std::path::PathBuf {
|
||||
return kb_workspace_root_dir().join("config.json");
|
||||
return workspace_root_dir().join("config.json");
|
||||
}
|
||||
|
||||
/// Loads a configuration from a JSON file and validates it.
|
||||
pub fn load_from_path<P: AsRef<std::path::Path>>(path: P) -> Result<Self, crate::KbError> {
|
||||
pub fn load_from_path<P: AsRef<std::path::Path>>(path: P) -> Result<Self, crate::Error> {
|
||||
let path_ref = path.as_ref();
|
||||
let content_result = std::fs::read_to_string(path_ref);
|
||||
let content = match content_result {
|
||||
Ok(content) => content,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Io(format!(
|
||||
return Err(crate::Error::Io(format!(
|
||||
"cannot read configuration file '{}': {error}",
|
||||
path_ref.display()
|
||||
)));
|
||||
@@ -40,7 +40,7 @@ impl KbConfig {
|
||||
let config = match config_result {
|
||||
Ok(config) => config,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Json(format!(
|
||||
return Err(crate::Error::Json(format!(
|
||||
"cannot parse configuration file '{}': {error}",
|
||||
path_ref.display()
|
||||
)));
|
||||
@@ -54,29 +54,27 @@ impl KbConfig {
|
||||
}
|
||||
|
||||
/// Validates the current configuration.
|
||||
pub fn validate(&self) -> Result<(), crate::KbError> {
|
||||
pub fn validate(&self) -> Result<(), crate::Error> {
|
||||
if self.app.name.trim().is_empty() {
|
||||
return Err(crate::KbError::Config("app.name must not be empty".to_string()));
|
||||
return Err(crate::Error::Config("app.name must not be empty".to_string()));
|
||||
}
|
||||
if self.app.environment.trim().is_empty() {
|
||||
return Err(crate::KbError::Config("app.environment must not be empty".to_string()));
|
||||
return Err(crate::Error::Config("app.environment must not be empty".to_string()));
|
||||
}
|
||||
if self.logging.level.trim().is_empty() {
|
||||
return Err(crate::KbError::Config("logging.level must not be empty".to_string()));
|
||||
return Err(crate::Error::Config("logging.level must not be empty".to_string()));
|
||||
}
|
||||
if self.logging.directory.trim().is_empty() {
|
||||
return Err(crate::KbError::Config("logging.directory must not be empty".to_string()));
|
||||
return Err(crate::Error::Config("logging.directory must not be empty".to_string()));
|
||||
}
|
||||
if self.logging.file_prefix.trim().is_empty() {
|
||||
return Err(crate::KbError::Config(
|
||||
"logging.file_prefix must not be empty".to_string(),
|
||||
));
|
||||
return Err(crate::Error::Config("logging.file_prefix must not be empty".to_string()));
|
||||
}
|
||||
if self.data.sqlite_path.trim().is_empty() {
|
||||
return Err(crate::KbError::Config("data.sqlite_path must not be empty".to_string()));
|
||||
return Err(crate::Error::Config("data.sqlite_path must not be empty".to_string()));
|
||||
}
|
||||
if self.data.wallets_directory.trim().is_empty() {
|
||||
return Err(crate::KbError::Config(
|
||||
return Err(crate::Error::Config(
|
||||
"data.wallets_directory must not be empty".to_string(),
|
||||
));
|
||||
}
|
||||
@@ -84,7 +82,7 @@ impl KbConfig {
|
||||
&& self.logging.rotation != "hourly"
|
||||
&& self.logging.rotation != "never"
|
||||
{
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"unsupported logging.rotation '{}'",
|
||||
self.logging.rotation
|
||||
)));
|
||||
@@ -94,7 +92,7 @@ impl KbConfig {
|
||||
&& self.logging.message_format != "pretty"
|
||||
&& self.logging.message_format != "json"
|
||||
{
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"unsupported logging.message_format '{}'",
|
||||
self.logging.message_format
|
||||
)));
|
||||
@@ -103,7 +101,7 @@ impl KbConfig {
|
||||
&& self.logging.time_format != "rfc3339_millis"
|
||||
&& self.logging.time_format != "none"
|
||||
{
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"unsupported logging.time_format '{}'",
|
||||
self.logging.time_format
|
||||
)));
|
||||
@@ -125,11 +123,11 @@ impl KbConfig {
|
||||
}
|
||||
|
||||
/// Creates the basic runtime directories required by the current configuration.
|
||||
pub fn prepare_filesystem(&self) -> Result<(), crate::KbError> {
|
||||
pub fn prepare_filesystem(&self) -> Result<(), crate::Error> {
|
||||
let logging_directory = self.logging.directory_path();
|
||||
let create_logs_result = std::fs::create_dir_all(&logging_directory);
|
||||
if let Err(error) = create_logs_result {
|
||||
return Err(crate::KbError::Io(format!(
|
||||
return Err(crate::Error::Io(format!(
|
||||
"cannot create logging directory '{}': {error}",
|
||||
logging_directory.display()
|
||||
)));
|
||||
@@ -137,7 +135,7 @@ impl KbConfig {
|
||||
let wallets_directory = self.data.wallets_directory_path();
|
||||
let create_wallets_result = std::fs::create_dir_all(&wallets_directory);
|
||||
if let Err(error) = create_wallets_result {
|
||||
return Err(crate::KbError::Io(format!(
|
||||
return Err(crate::Error::Io(format!(
|
||||
"cannot create wallets directory '{}': {error}",
|
||||
wallets_directory.display()
|
||||
)));
|
||||
@@ -148,7 +146,7 @@ impl KbConfig {
|
||||
if !sqlite_parent.as_os_str().is_empty() {
|
||||
let create_db_parent_result = std::fs::create_dir_all(sqlite_parent);
|
||||
if let Err(error) = create_db_parent_result {
|
||||
return Err(crate::KbError::Io(format!(
|
||||
return Err(crate::Error::Io(format!(
|
||||
"cannot create database parent directory '{}': {error}",
|
||||
sqlite_parent.display()
|
||||
)));
|
||||
@@ -162,7 +160,7 @@ impl KbConfig {
|
||||
pub fn find_http_endpoint(
|
||||
&self,
|
||||
endpoint_name: &str,
|
||||
) -> std::option::Option<&KbHttpEndpointConfig> {
|
||||
) -> std::option::Option<&HttpEndpointConfig> {
|
||||
return self
|
||||
.solana
|
||||
.http_endpoints
|
||||
@@ -171,10 +169,7 @@ impl KbConfig {
|
||||
}
|
||||
|
||||
/// Returns a named WebSocket endpoint by reference.
|
||||
pub fn find_ws_endpoint(
|
||||
&self,
|
||||
endpoint_name: &str,
|
||||
) -> std::option::Option<&KbWsEndpointConfig> {
|
||||
pub fn find_ws_endpoint(&self, endpoint_name: &str) -> std::option::Option<&WsEndpointConfig> {
|
||||
return self
|
||||
.solana
|
||||
.ws_endpoints
|
||||
@@ -184,44 +179,44 @@ impl KbConfig {
|
||||
|
||||
fn validate_http_endpoint(
|
||||
&self,
|
||||
endpoint: &KbHttpEndpointConfig,
|
||||
endpoint: &HttpEndpointConfig,
|
||||
endpoint_names: &mut std::vec::Vec<std::string::String>,
|
||||
) -> Result<(), crate::KbError> {
|
||||
) -> Result<(), crate::Error> {
|
||||
if endpoint.name.trim().is_empty() {
|
||||
return Err(crate::KbError::Config("http endpoint name must not be empty".to_string()));
|
||||
return Err(crate::Error::Config("http endpoint name must not be empty".to_string()));
|
||||
}
|
||||
if endpoint_names.iter().any(|name| return name == &endpoint.name) {
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"duplicated endpoint name '{}'",
|
||||
endpoint.name
|
||||
)));
|
||||
}
|
||||
if !endpoint.url.starts_with("http://") && !endpoint.url.starts_with("https://") {
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"http endpoint '{}' must start with http:// or https://",
|
||||
endpoint.name
|
||||
)));
|
||||
}
|
||||
if endpoint.requests_per_second == 0 {
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"http endpoint '{}' requests_per_second must be > 0",
|
||||
endpoint.name
|
||||
)));
|
||||
}
|
||||
if endpoint.burst_capacity == 0 {
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"http endpoint '{}' burst_capacity must be > 0",
|
||||
endpoint.name
|
||||
)));
|
||||
}
|
||||
if endpoint.connect_timeout_ms == 0 {
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"http endpoint '{}' connect_timeout_ms must be > 0",
|
||||
endpoint.name
|
||||
)));
|
||||
}
|
||||
if endpoint.request_timeout_ms == 0 {
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"http endpoint '{}' request_timeout_ms must be > 0",
|
||||
endpoint.name
|
||||
)));
|
||||
@@ -232,56 +227,56 @@ impl KbConfig {
|
||||
|
||||
fn validate_ws_endpoint(
|
||||
&self,
|
||||
endpoint: &KbWsEndpointConfig,
|
||||
endpoint: &WsEndpointConfig,
|
||||
endpoint_names: &mut std::vec::Vec<std::string::String>,
|
||||
) -> Result<(), crate::KbError> {
|
||||
) -> Result<(), crate::Error> {
|
||||
if endpoint.name.trim().is_empty() {
|
||||
return Err(crate::KbError::Config("ws endpoint name must not be empty".to_string()));
|
||||
return Err(crate::Error::Config("ws endpoint name must not be empty".to_string()));
|
||||
}
|
||||
if endpoint_names.iter().any(|name| return name == &endpoint.name) {
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"duplicated endpoint name '{}'",
|
||||
endpoint.name
|
||||
)));
|
||||
}
|
||||
if !endpoint.url.starts_with("ws://") && !endpoint.url.starts_with("wss://") {
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"ws endpoint '{}' must start with ws:// or wss://",
|
||||
endpoint.name
|
||||
)));
|
||||
}
|
||||
if endpoint.max_subscriptions == 0 {
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"ws endpoint '{}' max_subscriptions must be > 0",
|
||||
endpoint.name
|
||||
)));
|
||||
}
|
||||
if endpoint.connect_timeout_ms == 0 {
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"ws endpoint '{}' connect_timeout_ms must be > 0",
|
||||
endpoint.name
|
||||
)));
|
||||
}
|
||||
if endpoint.request_timeout_ms == 0 {
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"ws endpoint '{}' request_timeout_ms must be > 0",
|
||||
endpoint.name
|
||||
)));
|
||||
}
|
||||
if endpoint.unsubscribe_timeout_ms == 0 {
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"ws endpoint '{}' unsubscribe_timeout_ms must be > 0",
|
||||
endpoint.name
|
||||
)));
|
||||
}
|
||||
if endpoint.write_channel_capacity == 0 {
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"ws endpoint '{}' write_channel_capacity must be > 0",
|
||||
endpoint.name
|
||||
)));
|
||||
}
|
||||
if endpoint.event_channel_capacity == 0 {
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"ws endpoint '{}' event_channel_capacity must be > 0",
|
||||
endpoint.name
|
||||
)));
|
||||
@@ -293,7 +288,7 @@ impl KbConfig {
|
||||
|
||||
/// Generic application settings.
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KbAppConfig {
|
||||
pub struct AppConfig {
|
||||
/// Human-readable application name.
|
||||
pub name: std::string::String,
|
||||
/// Current environment name such as `development` or `production`.
|
||||
@@ -312,7 +307,7 @@ pub struct KbAppConfig {
|
||||
/// configuration so that the format policy is stabilized early, even though
|
||||
/// their handling will be refined in later versions.
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KbLoggingConfig {
|
||||
pub struct LoggingConfig {
|
||||
/// Global default log level.
|
||||
pub level: std::string::String,
|
||||
/// Enables console logging.
|
||||
@@ -335,47 +330,47 @@ pub struct KbLoggingConfig {
|
||||
pub target_filters: std::collections::BTreeMap<std::string::String, std::string::String>,
|
||||
}
|
||||
|
||||
impl KbLoggingConfig {
|
||||
impl LoggingConfig {
|
||||
/// Returns the resolved logging directory path.
|
||||
pub fn directory_path(&self) -> std::path::PathBuf {
|
||||
return kb_resolve_workspace_relative_path(&self.directory);
|
||||
return resolve_workspace_relative_path(&self.directory);
|
||||
}
|
||||
}
|
||||
|
||||
/// Local data paths used by the application.
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KbDataConfig {
|
||||
pub struct DataConfig {
|
||||
/// SQLite database path.
|
||||
pub sqlite_path: std::string::String,
|
||||
/// Directory storing Solana wallets and related material in future versions.
|
||||
pub wallets_directory: std::string::String,
|
||||
}
|
||||
|
||||
impl KbDataConfig {
|
||||
impl DataConfig {
|
||||
/// Returns the resolved SQLite database path.
|
||||
pub fn sqlite_path_buf(&self) -> std::path::PathBuf {
|
||||
return kb_resolve_workspace_relative_path(&self.sqlite_path);
|
||||
return resolve_workspace_relative_path(&self.sqlite_path);
|
||||
}
|
||||
|
||||
/// Returns the resolved wallets directory path.
|
||||
pub fn wallets_directory_path(&self) -> std::path::PathBuf {
|
||||
return kb_resolve_workspace_relative_path(&self.wallets_directory);
|
||||
return resolve_workspace_relative_path(&self.wallets_directory);
|
||||
}
|
||||
}
|
||||
|
||||
/// Solana transport configuration.
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KbSolanaConfig {
|
||||
pub struct SolanaConfig {
|
||||
/// Named HTTP endpoints.
|
||||
pub http_endpoints: std::vec::Vec<KbHttpEndpointConfig>,
|
||||
pub http_endpoints: std::vec::Vec<HttpEndpointConfig>,
|
||||
/// Named WebSocket endpoints.
|
||||
pub ws_endpoints: std::vec::Vec<KbWsEndpointConfig>,
|
||||
pub ws_endpoints: std::vec::Vec<WsEndpointConfig>,
|
||||
}
|
||||
|
||||
/// HTTP endpoint configuration.
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct KbHttpEndpointConfig {
|
||||
pub struct HttpEndpointConfig {
|
||||
/// Logical endpoint name.
|
||||
pub name: std::string::String,
|
||||
/// Whether this endpoint is enabled.
|
||||
@@ -412,10 +407,10 @@ pub struct KbHttpEndpointConfig {
|
||||
pub max_concurrent_requests_per_endpoint: usize,
|
||||
}
|
||||
|
||||
impl KbHttpEndpointConfig {
|
||||
impl HttpEndpointConfig {
|
||||
/// Returns the resolved URL, replacing an `${ENV_VAR}` placeholder when
|
||||
/// `api_key_env_var` is configured.
|
||||
pub fn resolved_url(&self) -> Result<std::string::String, crate::KbError> {
|
||||
pub fn resolved_url(&self) -> Result<std::string::String, crate::Error> {
|
||||
let env_var_name_option = self.api_key_env_var.as_ref();
|
||||
let env_var_name = match env_var_name_option {
|
||||
Some(env_var_name) => env_var_name,
|
||||
@@ -427,7 +422,7 @@ impl KbHttpEndpointConfig {
|
||||
let api_key = match api_key_result {
|
||||
Ok(api_key) => api_key,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"cannot resolve api key env var '{}' for http endpoint '{}': {}",
|
||||
env_var_name, self.name, error
|
||||
)));
|
||||
@@ -443,7 +438,7 @@ impl KbHttpEndpointConfig {
|
||||
|
||||
/// WebSocket endpoint configuration.
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KbWsEndpointConfig {
|
||||
pub struct WsEndpointConfig {
|
||||
/// Stable internal endpoint name used by the application.
|
||||
pub name: std::string::String,
|
||||
/// Enables or disables the endpoint.
|
||||
@@ -472,17 +467,17 @@ pub struct KbWsEndpointConfig {
|
||||
pub auto_reconnect: bool,
|
||||
}
|
||||
|
||||
impl KbWsEndpointConfig {
|
||||
impl WsEndpointConfig {
|
||||
/// Returns the resolved endpoint URL.
|
||||
pub fn resolved_url(&self) -> Result<std::string::String, crate::KbError> {
|
||||
return kb_resolve_endpoint_url(&self.url, &self.api_key_env_var);
|
||||
pub fn resolved_url(&self) -> Result<std::string::String, crate::Error> {
|
||||
return resolve_endpoint_url(&self.url, &self.api_key_env_var);
|
||||
}
|
||||
}
|
||||
|
||||
/// SQLite configuration.
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct KbSqliteDatabaseConfig {
|
||||
pub struct SqliteDatabaseConfig {
|
||||
/// SQLite database path.
|
||||
pub path: std::string::String,
|
||||
/// Whether the file should be created if missing.
|
||||
@@ -497,26 +492,26 @@ pub struct KbSqliteDatabaseConfig {
|
||||
pub use_wal: bool,
|
||||
}
|
||||
|
||||
impl KbSqliteDatabaseConfig {
|
||||
impl SqliteDatabaseConfig {
|
||||
/// Returns the resolved SQLite database path.
|
||||
pub fn path_buf(&self) -> std::path::PathBuf {
|
||||
return kb_resolve_workspace_relative_path(&self.path);
|
||||
return resolve_workspace_relative_path(&self.path);
|
||||
}
|
||||
}
|
||||
|
||||
/// Database configuration.
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct KbDatabaseConfig {
|
||||
pub struct DatabaseConfig {
|
||||
/// Whether the database layer is enabled.
|
||||
pub enabled: bool,
|
||||
/// Selected backend.
|
||||
pub backend: crate::KbDatabaseBackend,
|
||||
pub backend: crate::DatabaseBackend,
|
||||
/// SQLite-specific configuration.
|
||||
pub sqlite: KbSqliteDatabaseConfig,
|
||||
pub sqlite: SqliteDatabaseConfig,
|
||||
}
|
||||
|
||||
fn kb_workspace_root_dir() -> std::path::PathBuf {
|
||||
fn workspace_root_dir() -> std::path::PathBuf {
|
||||
let manifest_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
match manifest_dir.parent() {
|
||||
Some(parent) => return parent.to_path_buf(),
|
||||
@@ -524,18 +519,18 @@ fn kb_workspace_root_dir() -> std::path::PathBuf {
|
||||
}
|
||||
}
|
||||
|
||||
fn kb_resolve_workspace_relative_path<P: AsRef<std::path::Path>>(path: P) -> std::path::PathBuf {
|
||||
fn resolve_workspace_relative_path<P: AsRef<std::path::Path>>(path: P) -> std::path::PathBuf {
|
||||
let input_path = std::path::PathBuf::from(path.as_ref());
|
||||
if input_path.is_absolute() {
|
||||
return input_path;
|
||||
}
|
||||
return kb_workspace_root_dir().join(input_path);
|
||||
return workspace_root_dir().join(input_path);
|
||||
}
|
||||
|
||||
fn kb_resolve_endpoint_url(
|
||||
fn resolve_endpoint_url(
|
||||
url: &str,
|
||||
api_key_env_var: &std::option::Option<std::string::String>,
|
||||
) -> Result<std::string::String, crate::KbError> {
|
||||
) -> Result<std::string::String, crate::Error> {
|
||||
let env_var_name_option = api_key_env_var.as_deref();
|
||||
let env_var_name = match env_var_name_option {
|
||||
Some(env_var_name) => env_var_name,
|
||||
@@ -551,7 +546,7 @@ fn kb_resolve_endpoint_url(
|
||||
let env_value = match env_value_result {
|
||||
Ok(env_value) => env_value,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Config(format!(
|
||||
return Err(crate::Error::Config(format!(
|
||||
"environment variable '{}' is required to resolve endpoint url '{}': {error}",
|
||||
env_var_name, url
|
||||
)));
|
||||
|
||||
Reference in New Issue
Block a user