feat: init process: apply DB migrations and job scheduler

This commit is contained in:
Aravinth Manivannan 2023-09-29 19:18:33 +05:30
parent 963ce0174b
commit 21a283d138
Signed by: realaravinth
GPG key ID: F8F50389936984FF

View file

@ -1,20 +1,10 @@
/*
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::env;
use std::sync::Arc;
use actix_web::{
error::InternalError, http::StatusCode, middleware as actix_middleware, web::Data as WebData,
@ -39,12 +29,15 @@ pub const PKG_HOMEPAGE: &str = env!("CARGO_PKG_HOMEPAGE");
pub type AppCtx = WebData<ctx::ArcCtx>;
mod api;
mod complaince;
mod ctx;
mod db;
mod docker;
mod docker_compose;
mod errors;
mod compliance;
mod git;
mod runner;
mod settings;
mod utils;
@ -75,13 +68,17 @@ async fn main() -> std::io::Result<()> {
);
let settings = Settings::new().unwrap();
settings.init();
let ctx = AppCtx::new(Arc::new(Ctx::new(settings.clone()).await));
ctx.db.migrate().await.unwrap();
serve(settings, ctx).await?;
Ok(())
}
async fn serve(settings: Settings, ctx: AppCtx) -> std::io::Result<()> {
let ip = settings.server.get_ip();
let workers = settings.server.workers.unwrap_or_else(num_cpus::get);
let scheduler = runner::Scheduler::spawn(ctx.clone()).await;
info!("Starting server on: http://{}", ip);
HttpServer::new(move || {
@ -103,7 +100,9 @@ async fn serve(settings: Settings, ctx: AppCtx) -> std::io::Result<()> {
.bind(ip)
.unwrap()
.run()
.await
.await?;
scheduler.stop().await;
Ok(())
}
#[cfg(not(tarpaulin_include))]
@ -115,8 +114,5 @@ pub fn get_json_err() -> JsonConfig {
}
pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
// crate::api::v1::services(cfg);
// crate::pages::services(cfg);
// crate::static_assets::services(cfg);
// crate::serve::services(cfg);
crate::api::v1::services(cfg);
}