mirror of
https://codeberg.org/likwid/likwid.git
synced 2026-02-10 05:23:09 +00:00
186 lines
6.2 KiB
Rust
186 lines
6.2 KiB
Rust
|
|
mod api;
|
||
|
|
mod auth;
|
||
|
|
mod config;
|
||
|
|
mod db;
|
||
|
|
mod demo;
|
||
|
|
mod models;
|
||
|
|
mod plugins;
|
||
|
|
mod voting;
|
||
|
|
|
||
|
|
use std::net::SocketAddr;
|
||
|
|
use std::sync::Arc;
|
||
|
|
use axum::Extension;
|
||
|
|
use chrono::{Datelike, Timelike, Utc, Weekday};
|
||
|
|
use serde_json::json;
|
||
|
|
use tower_http::cors::{Any, CorsLayer};
|
||
|
|
use tower_http::trace::TraceLayer;
|
||
|
|
use uuid::Uuid;
|
||
|
|
|
||
|
|
use crate::config::Config;
|
||
|
|
use crate::plugins::HookContext;
|
||
|
|
|
||
|
|
#[tokio::main]
|
||
|
|
async fn main() {
|
||
|
|
tracing_subscriber::fmt::init();
|
||
|
|
|
||
|
|
dotenvy::dotenv().ok();
|
||
|
|
|
||
|
|
// Load configuration
|
||
|
|
let config = Config::from_env().unwrap_or_default();
|
||
|
|
let config = Arc::new(config);
|
||
|
|
|
||
|
|
if config.is_demo() {
|
||
|
|
tracing::info!("🎭 DEMO MODE ENABLED - Some actions are restricted");
|
||
|
|
}
|
||
|
|
|
||
|
|
let database_url = std::env::var("DATABASE_URL")
|
||
|
|
.unwrap_or_else(|_| config.database_url.clone());
|
||
|
|
|
||
|
|
let pool = db::create_pool(&database_url)
|
||
|
|
.await
|
||
|
|
.expect("Failed to create database pool");
|
||
|
|
|
||
|
|
tracing::info!("Connected to database");
|
||
|
|
|
||
|
|
sqlx::migrate!("./migrations")
|
||
|
|
.run(&pool)
|
||
|
|
.await
|
||
|
|
.expect("Failed to run database migrations");
|
||
|
|
|
||
|
|
let cors = CorsLayer::new()
|
||
|
|
.allow_origin(Any)
|
||
|
|
.allow_methods(Any)
|
||
|
|
.allow_headers(Any);
|
||
|
|
|
||
|
|
let plugins = plugins::PluginManager::new(pool.clone())
|
||
|
|
.register_builtin_plugins()
|
||
|
|
.initialize()
|
||
|
|
.await
|
||
|
|
.expect("Failed to initialize plugins");
|
||
|
|
|
||
|
|
{
|
||
|
|
let cron_plugins = plugins.clone();
|
||
|
|
let cron_pool = pool.clone();
|
||
|
|
tokio::spawn(async move {
|
||
|
|
let mut last_minute_key: i64 = -1;
|
||
|
|
let mut last_hour_key: i64 = -1;
|
||
|
|
let mut last_day_key: i64 = -1;
|
||
|
|
let mut last_week_key: i64 = -1;
|
||
|
|
let mut last_15min_key: i64 = -1;
|
||
|
|
|
||
|
|
let mut interval = tokio::time::interval(std::time::Duration::from_secs(5));
|
||
|
|
loop {
|
||
|
|
interval.tick().await;
|
||
|
|
|
||
|
|
let now = Utc::now();
|
||
|
|
let minute_key = now.timestamp() / 60;
|
||
|
|
if minute_key == last_minute_key {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
last_minute_key = minute_key;
|
||
|
|
|
||
|
|
let ctx = HookContext {
|
||
|
|
pool: cron_pool.clone(),
|
||
|
|
community_id: None,
|
||
|
|
actor_user_id: None,
|
||
|
|
};
|
||
|
|
|
||
|
|
let payload = json!({"ts": now.to_rfc3339()});
|
||
|
|
cron_plugins.do_action("cron.minute", ctx.clone(), payload.clone()).await;
|
||
|
|
cron_plugins
|
||
|
|
.do_action("cron.minutely", ctx.clone(), payload.clone())
|
||
|
|
.await;
|
||
|
|
|
||
|
|
let min15_key = now.timestamp() / 900;
|
||
|
|
if min15_key != last_15min_key {
|
||
|
|
last_15min_key = min15_key;
|
||
|
|
cron_plugins
|
||
|
|
.do_action("cron.every_15_minutes", ctx.clone(), payload.clone())
|
||
|
|
.await;
|
||
|
|
}
|
||
|
|
|
||
|
|
let hour_key = now.timestamp() / 3600;
|
||
|
|
if hour_key != last_hour_key {
|
||
|
|
last_hour_key = hour_key;
|
||
|
|
if now.minute() == 0 {
|
||
|
|
cron_plugins
|
||
|
|
.do_action("cron.hourly", ctx.clone(), payload.clone())
|
||
|
|
.await;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
let day_key = now.timestamp() / 86_400;
|
||
|
|
if day_key != last_day_key {
|
||
|
|
last_day_key = day_key;
|
||
|
|
if now.hour() == 0 && now.minute() == 0 {
|
||
|
|
cron_plugins
|
||
|
|
.do_action("cron.daily", ctx.clone(), payload.clone())
|
||
|
|
.await;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
let iso_week = now.iso_week();
|
||
|
|
let week_key = (iso_week.year() as i64) * 100 + (iso_week.week() as i64);
|
||
|
|
if week_key != last_week_key {
|
||
|
|
last_week_key = week_key;
|
||
|
|
if now.weekday() == Weekday::Mon && now.hour() == 0 && now.minute() == 0 {
|
||
|
|
cron_plugins
|
||
|
|
.do_action("cron.weekly", ctx.clone(), payload.clone())
|
||
|
|
.await;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// WASM plugins need per-community context.
|
||
|
|
let community_ids: Vec<Uuid> = match sqlx::query_scalar(
|
||
|
|
"SELECT id FROM communities WHERE is_active = true",
|
||
|
|
)
|
||
|
|
.fetch_all(&cron_pool)
|
||
|
|
.await
|
||
|
|
{
|
||
|
|
Ok(ids) => ids,
|
||
|
|
Err(e) => {
|
||
|
|
tracing::error!("cron: failed to list communities: {}", e);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
let mut wasm_hooks: Vec<&'static str> = vec!["cron.minute", "cron.minutely"];
|
||
|
|
if min15_key == last_15min_key {
|
||
|
|
wasm_hooks.push("cron.every_15_minutes");
|
||
|
|
}
|
||
|
|
if now.minute() == 0 {
|
||
|
|
wasm_hooks.push("cron.hourly");
|
||
|
|
}
|
||
|
|
if now.hour() == 0 && now.minute() == 0 {
|
||
|
|
wasm_hooks.push("cron.daily");
|
||
|
|
if now.weekday() == Weekday::Mon {
|
||
|
|
wasm_hooks.push("cron.weekly");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
for cid in community_ids {
|
||
|
|
for hook in &wasm_hooks {
|
||
|
|
cron_plugins
|
||
|
|
.do_wasm_action_for_community(hook, cid, payload.clone())
|
||
|
|
.await;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
let app = api::create_router(pool.clone(), config.clone())
|
||
|
|
.layer(Extension(plugins))
|
||
|
|
.layer(Extension(config.clone()))
|
||
|
|
.layer(cors)
|
||
|
|
.layer(TraceLayer::new_for_http());
|
||
|
|
|
||
|
|
let host: std::net::IpAddr = config.server_host.parse()
|
||
|
|
.unwrap_or_else(|_| std::net::IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 1)));
|
||
|
|
let addr = SocketAddr::from((host, config.server_port));
|
||
|
|
tracing::info!("Likwid backend listening on http://{}", addr);
|
||
|
|
|
||
|
|
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
|
||
|
|
axum::serve(listener, app).await.unwrap();
|
||
|
|
}
|