refactor
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
pub mod state;
|
||||
|
||||
use crate::{api, core::config::Config, transport};
|
||||
use dashmap::DashMap;
|
||||
use rocket::routes;
|
||||
use sqlx::{SqlitePool, sqlite::SqliteConnectOptions};
|
||||
use std::sync::Arc;
|
||||
|
||||
const DATABASE_PATH: &str = "server.db";
|
||||
|
||||
pub async fn connect_database() -> Result<SqlitePool, sqlx::Error> {
|
||||
SqlitePool::connect_with(
|
||||
SqliteConnectOptions::new()
|
||||
.filename(DATABASE_PATH)
|
||||
.create_if_missing(true),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn apply_schema(database: &SqlitePool) -> Result<(), sqlx::Error> {
|
||||
for statement in include_str!("../../schema.sql")
|
||||
.split(';')
|
||||
.map(str::trim)
|
||||
.filter(|statement| !statement.is_empty())
|
||||
{
|
||||
sqlx::query(statement).execute(database).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn build_rocket(database: SqlitePool) -> rocket::Rocket<rocket::Build> {
|
||||
rocket::build()
|
||||
.manage(state::AppState {
|
||||
database,
|
||||
registry: Arc::new(DashMap::new()),
|
||||
device_counts: Arc::new(DashMap::new()),
|
||||
config: Arc::new(Config::from_env()),
|
||||
})
|
||||
.mount("/", routes![transport::socket::connect, api::mcp::route])
|
||||
.register("/", rocket::catchers![api::catchers::default_catcher])
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
use crate::core::config::Config;
|
||||
use dashmap::DashMap;
|
||||
use serde::Serialize;
|
||||
use sqlx::SqlitePool;
|
||||
use std::sync::{Arc, atomic::AtomicUsize};
|
||||
use tokio::sync::{mpsc, oneshot};
|
||||
|
||||
pub type Database = SqlitePool;
|
||||
pub type DeviceCounts = DashMap<String, Arc<AtomicUsize>>;
|
||||
pub type Registry = DashMap<(String, String), RegistryEntry>;
|
||||
|
||||
pub struct RegistryEntry {
|
||||
pub sender: mpsc::Sender<PendingExec>,
|
||||
pub in_flight: Arc<AtomicUsize>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct ExecResult {
|
||||
pub exit_code: i32,
|
||||
pub stdout: String,
|
||||
pub stderr: String,
|
||||
}
|
||||
|
||||
pub struct PendingExec {
|
||||
pub exec_id: String,
|
||||
pub command: String,
|
||||
pub reply: oneshot::Sender<ExecResult>,
|
||||
}
|
||||
|
||||
pub struct AppState {
|
||||
pub database: Database,
|
||||
pub registry: Arc<Registry>,
|
||||
pub device_counts: Arc<DeviceCounts>,
|
||||
pub config: Arc<Config>,
|
||||
}
|
||||
Reference in New Issue
Block a user