Compare commits
3 Commits
c65dc3af47
...
96e7f36731
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
96e7f36731 | ||
|
|
410328a2af | ||
|
|
9f122566d0 |
@@ -11,15 +11,8 @@ use crate::{
|
|||||||
cmd::CliCommand,
|
cmd::CliCommand,
|
||||||
configs::{ProgramSettings, get_program_settings, logging::LoggingSettings},
|
configs::{ProgramSettings, get_program_settings, logging::LoggingSettings},
|
||||||
log,
|
log,
|
||||||
routes::{self, AppService, AppState},
|
routes::{self, AppState},
|
||||||
services::{
|
services::get_app_service,
|
||||||
auth::{
|
|
||||||
authentication::{AuthenticationServiceImpl, strategies::password::PasswordStrategy},
|
|
||||||
user::UserServiceImpl,
|
|
||||||
},
|
|
||||||
server_state::ServerStateService,
|
|
||||||
settings::SettingsService,
|
|
||||||
},
|
|
||||||
tasks,
|
tasks,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -148,19 +141,7 @@ fn get_app_state(
|
|||||||
AppState {
|
AppState {
|
||||||
database_connection: db_connection.clone(),
|
database_connection: db_connection.clone(),
|
||||||
config: Arc::new(settings.clone()),
|
config: Arc::new(settings.clone()),
|
||||||
service: Arc::new(AppService {
|
service: Arc::new(get_app_service(db_connection, settings)),
|
||||||
server_state: Arc::new(ServerStateService::new(db_connection.clone())),
|
|
||||||
settings: Arc::new(SettingsService::new(db_connection.clone())),
|
|
||||||
auth_state: routes::AuthState {
|
|
||||||
strategy: routes::AuthStrategy {
|
|
||||||
password: Arc::new(PasswordStrategy::new(db_connection.clone())),
|
|
||||||
},
|
|
||||||
authentication: Arc::new(AuthenticationServiceImpl::new(
|
|
||||||
settings.auth.jwt_secret.clone(),
|
|
||||||
)),
|
|
||||||
},
|
|
||||||
user: Arc::new(UserServiceImpl::new(db_connection.clone())),
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
pub mod agent;
|
||||||
pub mod auth;
|
pub mod auth;
|
||||||
pub mod database;
|
pub mod database;
|
||||||
pub mod logging;
|
pub mod logging;
|
||||||
@@ -21,6 +22,7 @@ pub struct ProgramSettings {
|
|||||||
pub database: database::DatabaseSettings,
|
pub database: database::DatabaseSettings,
|
||||||
pub server: server::ServerSettings,
|
pub server: server::ServerSettings,
|
||||||
pub auth: auth::AuthSettings,
|
pub auth: auth::AuthSettings,
|
||||||
|
pub agent: agent::AgentSettings,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromConfig for ProgramSettings {
|
impl FromConfig for ProgramSettings {
|
||||||
@@ -30,6 +32,7 @@ impl FromConfig for ProgramSettings {
|
|||||||
database: database::DatabaseSettings::from_config(_config)?,
|
database: database::DatabaseSettings::from_config(_config)?,
|
||||||
server: server::ServerSettings::from_config(_config)?,
|
server: server::ServerSettings::from_config(_config)?,
|
||||||
auth: auth::AuthSettings::from_config(_config)?,
|
auth: auth::AuthSettings::from_config(_config)?,
|
||||||
|
agent: agent::AgentSettings::from_config(_config)?,
|
||||||
};
|
};
|
||||||
config.validate()?;
|
config.validate()?;
|
||||||
Ok(config)
|
Ok(config)
|
||||||
@@ -50,6 +53,7 @@ impl FromConfig for ProgramSettings {
|
|||||||
database: database::DatabaseSettings::mock(),
|
database: database::DatabaseSettings::mock(),
|
||||||
server: server::ServerSettings::mock(),
|
server: server::ServerSettings::mock(),
|
||||||
auth: auth::AuthSettings::mock(),
|
auth: auth::AuthSettings::mock(),
|
||||||
|
agent: agent::AgentSettings::mock(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
58
apps/api/src/configs/agent.rs
Normal file
58
apps/api/src/configs/agent.rs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
use config::Config;
|
||||||
|
use tracing::error;
|
||||||
|
|
||||||
|
use crate::configs::key::AGENT_SOCK_PATH_KEY;
|
||||||
|
|
||||||
|
use super::FromConfig;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct AgentSettings {
|
||||||
|
pub socket_path: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromConfig for AgentSettings {
|
||||||
|
fn from_config(_config: &Config) -> Result<Self, String> {
|
||||||
|
Ok(AgentSettings {
|
||||||
|
socket_path: _config.get_string(AGENT_SOCK_PATH_KEY).map_err(|err| {
|
||||||
|
format!(
|
||||||
|
"Failed to get {} from configuration. Err: {}",
|
||||||
|
AGENT_SOCK_PATH_KEY, err
|
||||||
|
)
|
||||||
|
})?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate(&self) -> Result<(), String> {
|
||||||
|
// ensure socket_path exists and is readable and writable
|
||||||
|
if !std::path::Path::new(&self.socket_path).exists() {
|
||||||
|
let msg = format!("Agent socket path '{}' does not exist", self.socket_path);
|
||||||
|
error!("{}", msg);
|
||||||
|
return Err(msg);
|
||||||
|
}
|
||||||
|
if std::path::Path::new(&self.socket_path)
|
||||||
|
.metadata()
|
||||||
|
.map(|meta| {
|
||||||
|
let permissions = meta.permissions();
|
||||||
|
// Check read and write permissions for the owner
|
||||||
|
!permissions.readonly()
|
||||||
|
})
|
||||||
|
.unwrap_or(false)
|
||||||
|
{
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
let msg = format!(
|
||||||
|
"Agent socket path '{}' is not readable/writable",
|
||||||
|
self.socket_path
|
||||||
|
);
|
||||||
|
error!("{}", msg);
|
||||||
|
Err(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
fn mock() -> Self {
|
||||||
|
AgentSettings {
|
||||||
|
socket_path: "/tmp/agent.sock".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,3 +14,5 @@ pub(crate) const DATABASE_MIGRATE_ON_STARTUP_KEY: &str = "DATABASE.MIGRATION.MIG
|
|||||||
pub(crate) const AUTH_JWT_SECRET_KEY: &str = "AUTH.JWT_SECRET";
|
pub(crate) const AUTH_JWT_SECRET_KEY: &str = "AUTH.JWT_SECRET";
|
||||||
pub(crate) const AUTH_DEFAULT_ADMIN_USERNAME_KEY: &str = "AUTH.DEFAULT_ADMIN_USERNAME";
|
pub(crate) const AUTH_DEFAULT_ADMIN_USERNAME_KEY: &str = "AUTH.DEFAULT_ADMIN_USERNAME";
|
||||||
pub(crate) const AUTH_DEFAULT_ADMIN_PASSWORD_KEY: &str = "AUTH.DEFAULT_ADMIN_PASSWORD";
|
pub(crate) const AUTH_DEFAULT_ADMIN_PASSWORD_KEY: &str = "AUTH.DEFAULT_ADMIN_PASSWORD";
|
||||||
|
//
|
||||||
|
pub(crate) const AGENT_SOCK_PATH_KEY: &str = "AGENT.SOCK.PATH";
|
||||||
|
|||||||
@@ -12,12 +12,8 @@ use crate::{
|
|||||||
configs::{ProgramSettings, server::CORSSettings},
|
configs::{ProgramSettings, server::CORSSettings},
|
||||||
middlewares,
|
middlewares,
|
||||||
services::{
|
services::{
|
||||||
auth::{
|
AppService, ServiceState,
|
||||||
authentication::{AuthenticationService, strategies::password::PasswordStrategy},
|
auth::authentication::{AuthenticationService, strategies::password::PasswordStrategy},
|
||||||
user::UserService,
|
|
||||||
},
|
|
||||||
server_state::ServerStateStore,
|
|
||||||
settings::SettingsStore,
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -28,8 +24,6 @@ pub struct AppState {
|
|||||||
pub config: Arc<ProgramSettings>,
|
pub config: Arc<ProgramSettings>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type ServiceState<T> = Arc<T>;
|
|
||||||
|
|
||||||
pub struct AuthStrategy {
|
pub struct AuthStrategy {
|
||||||
pub password: ServiceState<PasswordStrategy>,
|
pub password: ServiceState<PasswordStrategy>,
|
||||||
}
|
}
|
||||||
@@ -39,13 +33,6 @@ pub struct AuthState {
|
|||||||
pub authentication: ServiceState<dyn AuthenticationService>,
|
pub authentication: ServiceState<dyn AuthenticationService>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AppService {
|
|
||||||
pub settings: ServiceState<dyn SettingsStore>,
|
|
||||||
pub auth_state: AuthState,
|
|
||||||
pub user: ServiceState<dyn UserService>,
|
|
||||||
pub server_state: ServiceState<dyn ServerStateStore>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_root_router(
|
pub fn get_root_router(
|
||||||
state: impl Into<Arc<AppState>>,
|
state: impl Into<Arc<AppState>>,
|
||||||
cors_settings: Arc<CORSSettings>,
|
cors_settings: Arc<CORSSettings>,
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ pub async fn get_health_info(
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::configs::FromConfig;
|
use crate::configs::FromConfig;
|
||||||
|
use crate::services::agent_client::AgentService;
|
||||||
use crate::{
|
use crate::{
|
||||||
routes::{AppState, api::health::state::HealthState},
|
routes::{AppState, api::health::state::HealthState},
|
||||||
services::{
|
services::{
|
||||||
@@ -94,6 +95,7 @@ mod test {
|
|||||||
};
|
};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use agent_client::apis::configuration::Configuration;
|
||||||
use axum::body::to_bytes;
|
use axum::body::to_bytes;
|
||||||
use axum::{
|
use axum::{
|
||||||
Router,
|
Router,
|
||||||
@@ -124,6 +126,7 @@ mod test {
|
|||||||
},
|
},
|
||||||
user: Arc::new(UserServiceImpl::new(db.clone())),
|
user: Arc::new(UserServiceImpl::new(db.clone())),
|
||||||
server_state: Arc::new(ServerStateService::new(db.clone())),
|
server_state: Arc::new(ServerStateService::new(db.clone())),
|
||||||
|
agent_client: Arc::new(AgentService::new(Configuration::default())),
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -2,3 +2,53 @@ pub mod agent_client;
|
|||||||
pub mod auth;
|
pub mod auth;
|
||||||
pub mod server_state;
|
pub mod server_state;
|
||||||
pub mod settings;
|
pub mod settings;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use ::agent_client::apis::configuration::Configuration;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
configs::ProgramSettings,
|
||||||
|
routes::{self, AuthState},
|
||||||
|
services::{
|
||||||
|
auth::{
|
||||||
|
authentication::{AuthenticationServiceImpl, strategies::password::PasswordStrategy},
|
||||||
|
user::{UserService, UserServiceImpl},
|
||||||
|
},
|
||||||
|
server_state::{ServerStateService, ServerStateStore},
|
||||||
|
settings::{SettingsService, SettingsStore},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub type ServiceState<T> = Arc<T>;
|
||||||
|
|
||||||
|
pub struct AppService {
|
||||||
|
pub settings: ServiceState<dyn SettingsStore>,
|
||||||
|
pub auth_state: AuthState,
|
||||||
|
pub user: ServiceState<dyn UserService>,
|
||||||
|
pub server_state: ServiceState<dyn ServerStateStore>,
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub agent_client: ServiceState<agent_client::AgentService>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_app_service(
|
||||||
|
db_connection: &Arc<sea_orm::DatabaseConnection>,
|
||||||
|
settings: &ProgramSettings,
|
||||||
|
) -> AppService {
|
||||||
|
AppService {
|
||||||
|
server_state: Arc::new(ServerStateService::new(db_connection.clone())),
|
||||||
|
settings: Arc::new(SettingsService::new(db_connection.clone())),
|
||||||
|
auth_state: routes::AuthState {
|
||||||
|
strategy: routes::AuthStrategy {
|
||||||
|
password: Arc::new(PasswordStrategy::new(db_connection.clone())),
|
||||||
|
},
|
||||||
|
authentication: Arc::new(AuthenticationServiceImpl::new(
|
||||||
|
settings.auth.jwt_secret.clone(),
|
||||||
|
)),
|
||||||
|
},
|
||||||
|
user: Arc::new(UserServiceImpl::new(db_connection.clone())),
|
||||||
|
agent_client: Arc::new(agent_client::AgentService::new(Configuration::from(
|
||||||
|
settings.agent.clone(),
|
||||||
|
))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,11 +1,32 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use agent_client::apis::{ApiClient, configuration::Configuration};
|
use agent_client::apis::{ApiClient, configuration::Configuration};
|
||||||
|
use tracing::warn;
|
||||||
|
|
||||||
|
use crate::configs::agent::AgentSettings;
|
||||||
|
|
||||||
pub struct AgentService {
|
pub struct AgentService {
|
||||||
client: Arc<ApiClient>,
|
client: Arc<ApiClient>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<AgentSettings> for Configuration {
|
||||||
|
fn from(settings: AgentSettings) -> Self {
|
||||||
|
let mut config = Configuration::default();
|
||||||
|
let mut builder = reqwest::Client::builder();
|
||||||
|
|
||||||
|
let url = settings.socket_path;
|
||||||
|
if url.starts_with("unix://") {
|
||||||
|
builder = builder.unix_socket(url.to_string());
|
||||||
|
config.client = builder.build().expect("Failed to build reqwest client");
|
||||||
|
} else {
|
||||||
|
warn!("AgentSettings contains a non-unix socket path: {}", url);
|
||||||
|
config.base_path = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
config
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl AgentService {
|
impl AgentService {
|
||||||
pub fn new(config: impl Into<Arc<Configuration>>) -> Self {
|
pub fn new(config: impl Into<Arc<Configuration>>) -> Self {
|
||||||
let client = ApiClient::new(config.into());
|
let client = ApiClient::new(config.into());
|
||||||
@@ -14,6 +35,7 @@ impl AgentService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn get_client(&self) -> Arc<ApiClient> {
|
pub fn get_client(&self) -> Arc<ApiClient> {
|
||||||
Arc::clone(&self.client)
|
Arc::clone(&self.client)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user