first commit

This commit is contained in:
Mateusz Gruszczyński
2025-07-19 15:51:29 +02:00
commit 7ac1ad269c
12 changed files with 328 additions and 0 deletions

118
src/main.rs Normal file
View File

@@ -0,0 +1,118 @@
use actix_web::{web, App, HttpServer, HttpResponse};
use askama::Template;
use chrono::prelude::*;
use tokio::time::{self, Duration};
use actix_files as fs;
use serde::Deserialize;
use crate::config::Config;
use crate::ts3::{get_ts3_connection, create_channel};
use crate::rrd::{update_rrd_stats, generate_graphs};
mod config;
mod ts3;
mod rrd;
#[derive(Deserialize)]
struct ChannelData {
channel_name: String,
channel_topic: String,
channel_password: String,
}
#[derive(Template)]
#[template(path = "create.html")]
struct CreateTemplate<'a> {
ts3_server: &'a str,
ts3_server_port: u16,
client_ip: &'a str,
client_uuid: Option<&'a str>,
now: DateTime<Utc>,
flash_message: Option<String>,
}
#[derive(Template)]
#[template(path = "stats.html")]
struct StatsTemplate {
graphs: Vec<Graph>,
last_update: String,
server_name: String,
now: DateTime<Utc>,
}
struct Graph {
file: String,
title: String,
}
async fn create_handler(data: web::Json<ChannelData>) -> HttpResponse {
let config = Config::load();
// Logika tworzenia kanału (uproszczona; połącz z TS3)
let mut stream = match get_ts3_connection(&config) {
Ok(s) => s,
Err(e) => {
return HttpResponse::InternalServerError().body(format!("Błąd połączenia: {}", e));
}
};
let result = create_channel(&mut stream, &data.channel_name, &data.channel_topic, &data.channel_password);
let flash = match result {
Ok(_) => Some("Kanał utworzony pomyślnie".to_string()),
Err(e) => Some(format!("Błąd: {}", e)),
};
let template = CreateTemplate {
ts3_server: &config.ts3_server,
ts3_server_port: config.ts3_query_port, // Dostosuj jeśli potrzeba
client_ip: "127.0.0.1", // Pobierz z request (np. req.connection_info().realip_remote_addr())
client_uuid: Some("example-uuid"),
now: Utc::now(),
flash_message: flash,
};
HttpResponse::Ok().content_type("text/html").body(template.render().unwrap())
}
async fn stats_handler() -> HttpResponse {
let config = Config::load();
// Generuj wykresy (uproszczone)
generate_graphs(&config);
let graphs = vec![
Graph { file: "hour.gif".to_string(), title: "Ostatnie 12 godzin".to_string() },
Graph { file: "day.gif".to_string(), title: "Ostatnie 24 godziny".to_string() },
Graph { file: "72h.gif".to_string(), title: "Ostatnie 72 godziny".to_string() },
Graph { file: "week.gif".to_string(), title: "Ostatni tydzień".to_string() },
];
let template = StatsTemplate {
graphs,
last_update: Utc::now().to_string(),
server_name: "linuxiarz.pl".to_string(),
now: Utc::now(),
};
HttpResponse::Ok().content_type("text/html").body(template.render().unwrap())
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init();
let config = Config::load();
// Tło do aktualizacji RRD
let config_clone = config.clone();
tokio::spawn(async move {
let mut interval = time::interval(Duration::from_secs(config_clone.rrd_update_interval));
loop {
interval.tick().await;
update_rrd_stats(&config_clone);
generate_graphs(&config_clone);
}
});
HttpServer::new(|| {
App::new()
.service(fs::Files::new("/static", "./static").show_files_listing())
.route("/create", web::post().to(create_handler))
.route("/stats", web::get().to(stats_handler))
})
.bind(("0.0.0.0", 5000))?
.run()
.await
}