app/src/main.rs

133 lines
4.1 KiB
Rust

use std::{collections::HashMap, path::Path};
use actix::SystemService;
use actix_web::{get, web, App, HttpResponse, HttpServer};
use rust_embed::RustEmbed;
use sea_orm::{prelude::*, Database};
use tokio::sync::Mutex;
use tracing::{info, instrument};
use tracing_subscriber::prelude::*;
mod api;
mod auth;
mod entity;
mod error;
mod events;
mod socket_session;
#[cfg(all(target_env = "musl", target_pointer_width = "64"))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
#[derive(Debug)]
pub struct AppState {
pub db: DatabaseConnection,
pub healthcheck_status: Mutex<HashMap<i32, bool>>,
}
#[actix_rt::main]
async fn main() {
let subscriber = tracing_subscriber::registry().with(
tracing_subscriber::fmt::Layer::new()
.pretty()
.with_writer(std::io::stdout)
.with_ansi(true)
.with_filter(tracing_subscriber::filter::LevelFilter::INFO),
);
tracing::subscriber::set_global_default(subscriber).expect("Unable to set a global collector");
let db = setup_database().await.unwrap();
let state = web::Data::new(AppState {
db,
healthcheck_status: Mutex::new(HashMap::new()),
});
info!("Starting http server on 8080");
let st = state.clone();
actix_rt::spawn(async move {
loop {
tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
let apps: Vec<entity::application::Model> = entity::application::Entity::find()
.filter(entity::application::Column::EnableHealthcheck.eq(true))
.all(&st.db)
.await
.unwrap();
for app in apps {
match reqwest::get(app.url).await {
Ok(res) if res.status() == 200 => {
st.healthcheck_status.lock().await.insert(app.id, true);
let _ = events::EventBroker::from_registry()
.send(events::Event::HealthcheckChange {
app_id: app.id,
alive: true,
})
.await;
}
_ => {
st.healthcheck_status.lock().await.insert(app.id, false);
let _ = events::EventBroker::from_registry()
.send(events::Event::HealthcheckChange {
app_id: app.id,
alive: false,
})
.await;
}
}
}
}
});
HttpServer::new(move || {
let cors = actix_cors::Cors::permissive();
App::new()
.app_data(state.clone())
.wrap(tracing_actix_web::TracingLogger::default())
.wrap(cors)
.service(web::resource("/events/{token}").to(socket_session::event_session_index))
.service(api::routes())
.service(dist)
})
.bind("0.0.0.0:8088")
.unwrap()
.run()
.await
.expect("Couldnt launch server");
}
#[derive(RustEmbed)]
#[folder = "dist"]
struct UIAssets;
#[get("/{filename:.*}")]
async fn dist(path: web::Path<String>) -> HttpResponse {
let path = if UIAssets::get(&*path).is_some() {
&*path
} else {
"index.html"
};
let content = UIAssets::get(path).unwrap();
let body: actix_web::body::BoxBody = match content {
std::borrow::Cow::Borrowed(bytes) => actix_web::body::BoxBody::new(bytes),
std::borrow::Cow::Owned(bytes) => actix_web::body::BoxBody::new(bytes),
};
HttpResponse::Ok()
.content_type(mime_guess::from_path(path).first_or_octet_stream().as_ref())
.body(body)
}
#[instrument]
async fn setup_database() -> error::Result<DatabaseConnection> {
let db_fname = "data.db";
if !Path::new(db_fname).exists() {
std::fs::File::create(db_fname)?;
}
let pool = sqlx::SqlitePool::connect("sqlite://data.db").await?;
sqlx::migrate!("./migrations").run(&pool).await?;
tracing::info!("Database migrated");
Ok(Database::connect("sqlite://data.db").await?)
}