more refactoring

This commit is contained in:
pavel 2026-02-11 01:41:07 +01:00
commit 7268d49b4a
12 changed files with 319 additions and 189 deletions

View file

@ -14,6 +14,8 @@ use tower_http::cors::{AllowOrigin, CorsLayer};
use crate::entities::task::Entity as Task;
use crate::scheduler::Scheduler;
use crate::error::AppResult;
#[derive(Clone)]
pub struct AppState {
pub db: DatabaseConnection,
@ -23,37 +25,29 @@ pub struct AppState {
pub authenticator: Arc<crate::domain::auth::Authenticator>,
}
pub async fn start(config: crate::config::Config) -> Result<(), Box<dyn std::error::Error>> {
let db = Database::connect(&config.database_url).await?;
Migrator::up(&db, None).await?;
pub async fn start(config: crate::config::Config) -> AppResult<()> {
let db = setup_database(&config.database_url).await?;
let config = Arc::new(config);
let scheduler = Arc::new(Scheduler::new(db.clone(), config.clone()).await?);
let scheduler = Arc::new(
Scheduler::new(db.clone(), config.clone())
.await
.map_err(|e| crate::error::AppError::Internal(e.to_string()))?,
);
// Load existing scheduled tasks
let existing_tasks = Task::find().all(&db).await?;
let existing_tasks = Task::find()
.all(&db)
.await
.map_err(crate::error::AppError::Database)?;
for task in existing_tasks {
if let Some(cron) = task.cron {
let _ = scheduler.add_task_job(task.id, &cron).await;
}
}
let verifier = Arc::new(
crate::domain::auth::JwksVerifier::new(
config.authentik_issuer.clone(),
config.authentik_client_id.clone(),
)
.await?,
);
let authenticator = Arc::new(
crate::domain::auth::Authenticator::new(
config.authentik_issuer.clone(),
config.authentik_client_id.clone(),
config.authentik_client_secret.clone(),
)
.await?,
);
let (verifier, authenticator) = setup_auth(&config).await?;
let state = Arc::new(AppState {
db,
@ -63,9 +57,61 @@ pub async fn start(config: crate::config::Config) -> Result<(), Box<dyn std::err
authenticator,
});
let cors = build_cors_layer(&config);
let app = build_app(state, &config);
let app = Router::new()
let addr = format!("0.0.0.0:{}", config.port);
let listener = tokio::net::TcpListener::bind(&addr)
.await
.map_err(|e| crate::error::AppError::Internal(e.to_string()))?;
println!("Server running on http://localhost:{}", config.port);
axum::serve(listener, app)
.await
.map_err(|e| crate::error::AppError::Internal(e.to_string()))?;
Ok(())
}
async fn setup_database(database_url: &str) -> AppResult<DatabaseConnection> {
let db = Database::connect(database_url)
.await
.map_err(crate::error::AppError::Database)?;
Migrator::up(&db, None)
.await
.map_err(crate::error::AppError::Database)?;
Ok(db)
}
async fn setup_auth(
config: &crate::config::Config,
) -> AppResult<(
Arc<crate::domain::auth::JwksVerifier>,
Arc<crate::domain::auth::Authenticator>,
)> {
let verifier = Arc::new(
crate::domain::auth::JwksVerifier::new(
config.authentik_issuer.clone(),
config.authentik_client_id.clone(),
)
.await
.map_err(|e| crate::error::AppError::Internal(e.to_string()))?,
);
let authenticator = Arc::new(
crate::domain::auth::Authenticator::new(
config.authentik_issuer.clone(),
config.authentik_client_id.clone(),
config.authentik_client_secret.clone(),
)
.await
.map_err(|e| crate::error::AppError::Internal(e.to_string()))?,
);
Ok((verifier, authenticator))
}
fn build_app(state: Arc<AppState>, config: &crate::config::Config) -> Router {
let cors = build_cors_layer(config);
Router::new()
.route("/api/tasks", post(tasks::create_task).get(tasks::list_tasks))
.route("/api/tasks/:id", get(tasks::get_task).put(tasks::update_task))
.route("/api/tasks/:id/runs", post(tasks::rerun_task))
@ -88,14 +134,7 @@ pub async fn start(config: crate::config::Config) -> Result<(), Box<dyn std::err
HeaderValue::from_static("strict-origin-when-cross-origin"),
))
.layer(tower_http::limit::RequestBodyLimitLayer::new(1024 * 1024)) // 1MB limit
.with_state(state);
let addr = format!("0.0.0.0:{}", config.port);
let listener = tokio::net::TcpListener::bind(&addr).await?;
println!("Server running on http://localhost:{}", config.port);
axum::serve(listener, app).await?;
Ok(())
.with_state(state)
}
fn build_cors_layer(config: &crate::config::Config) -> CorsLayer {