2024-10-16 18:50:41 +00:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use loco_rs::{
|
|
|
|
app::{AppContext, Hooks, Initializer},
|
|
|
|
bgworker::{BackgroundWorker, Queue},
|
|
|
|
boot::{create_app, BootResult, StartMode},
|
|
|
|
controller::AppRoutes,
|
|
|
|
db::{self, truncate_table},
|
|
|
|
environment::Environment,
|
|
|
|
task::Tasks,
|
|
|
|
Result,
|
|
|
|
};
|
|
|
|
use migration::Migrator;
|
|
|
|
use sea_orm::DatabaseConnection;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
controllers, initializers,
|
2024-10-21 15:50:55 +00:00
|
|
|
models::_entities::{servers, bundles, services, bundles_services, users},
|
2024-10-16 18:50:41 +00:00
|
|
|
tasks,
|
|
|
|
workers::downloader::DownloadWorker,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub struct App;
|
|
|
|
#[async_trait]
|
|
|
|
impl Hooks for App {
|
|
|
|
fn app_name() -> &'static str {
|
|
|
|
env!("CARGO_CRATE_NAME")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn app_version() -> String {
|
|
|
|
format!(
|
|
|
|
"{} ({})",
|
|
|
|
env!("CARGO_PKG_VERSION"),
|
|
|
|
option_env!("BUILD_SHA")
|
|
|
|
.or(option_env!("GITHUB_SHA"))
|
|
|
|
.unwrap_or("dev")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> {
|
|
|
|
create_app::<Self, Migrator>(mode, environment).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn initializers(_ctx: &AppContext) -> Result<Vec<Box<dyn Initializer>>> {
|
|
|
|
Ok(vec![Box::new(
|
|
|
|
initializers::view_engine::ViewEngineInitializer,
|
|
|
|
)])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn routes(_ctx: &AppContext) -> AppRoutes {
|
|
|
|
AppRoutes::with_default_routes()
|
2024-10-21 12:19:14 +00:00
|
|
|
.add_route(controllers::service::routes())
|
2024-10-21 12:16:25 +00:00
|
|
|
.add_route(controllers::bundle::routes())
|
2024-10-16 18:50:41 +00:00
|
|
|
.add_route(controllers::server::routes())
|
|
|
|
.add_route(controllers::auth::routes())
|
|
|
|
.add_route(controllers::user::routes())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn connect_workers(ctx: &AppContext, queue: &Queue) -> Result<()> {
|
|
|
|
queue.register(DownloadWorker::build(ctx)).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn register_tasks(tasks: &mut Tasks) {
|
|
|
|
tasks.register(tasks::seed::SeedData);
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn truncate(db: &DatabaseConnection) -> Result<()> {
|
|
|
|
truncate_table(db, users::Entity).await?;
|
2024-10-21 15:50:55 +00:00
|
|
|
truncate_table(db, bundles_services::Entity).await?;
|
|
|
|
truncate_table(db, services::Entity).await?;
|
|
|
|
truncate_table(db, bundles::Entity).await?;
|
|
|
|
truncate_table(db, servers::Entity).await?;
|
2024-10-16 18:50:41 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn seed(db: &DatabaseConnection, base: &Path) -> Result<()> {
|
|
|
|
db::seed::<users::ActiveModel>(db, &base.join("users.yaml").display().to_string()).await?;
|
2024-10-21 15:50:55 +00:00
|
|
|
// ToDo: add seeds for other entities
|
2024-10-16 18:50:41 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|