Initial Headless Functions
This commit is contained in:
parent
c3c60efb81
commit
615c5c9edd
5 changed files with 176 additions and 7 deletions
|
|
@ -37,11 +37,27 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||
status.textContent = 'Bitte IP eingeben.';
|
||||
return;
|
||||
}
|
||||
status.textContent = `Erkenne Gerät bei ${val} … (Demo)`;
|
||||
|
||||
status.textContent = `Starte Erkennung für ${val} ...`;
|
||||
|
||||
try {
|
||||
const res = await fetch('/api/start', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ ip: val })
|
||||
});
|
||||
|
||||
const text = await res.text();
|
||||
status.textContent = text || 'Anfrage gesendet.';
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
status.textContent = 'Fehler beim Senden der Anfrage.';
|
||||
}
|
||||
});
|
||||
});
|
||||
"#;
|
||||
|
||||
|
||||
pub const DEFAULT_STYLES_CSS: &str = r#"
|
||||
* { box-sizing: border-box; }
|
||||
body { margin: 0; font: 16px system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
|
||||
|
|
|
|||
|
|
@ -3,13 +3,19 @@ use std::{
|
|||
net::SocketAddr,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use axum::{routing::get, Router, response::Html};
|
||||
use axum::{
|
||||
routing::{get, post},
|
||||
extract::Json,
|
||||
Router,
|
||||
response::Html,
|
||||
};
|
||||
use tower_http::services::ServeDir;
|
||||
use serde::Deserialize;
|
||||
|
||||
mod assets;
|
||||
use assets::{DEFAULT_INDEX_HTML, DEFAULT_APP_JS, DEFAULT_STYLES_CSS};
|
||||
|
||||
use crate::state::SharedState; // ⬅️ nur noch state importieren
|
||||
use crate::{state::SharedState, headless};
|
||||
|
||||
const WEB_DIR: &str = "web";
|
||||
|
||||
|
|
@ -22,7 +28,9 @@ fn exe_dir() -> PathBuf {
|
|||
|
||||
fn rebuild_web_assets(dir: &Path) -> std::io::Result<()> {
|
||||
let web = dir.join(WEB_DIR);
|
||||
if web.exists() { fs::remove_dir_all(&web)?; }
|
||||
if web.exists() {
|
||||
fs::remove_dir_all(&web)?;
|
||||
}
|
||||
fs::create_dir_all(&web)?;
|
||||
fs::write(web.join("index.html"), DEFAULT_INDEX_HTML.trim_start())?;
|
||||
fs::write(web.join("app.js"), DEFAULT_APP_JS.trim_start())?;
|
||||
|
|
@ -30,6 +38,28 @@ fn rebuild_web_assets(dir: &Path) -> std::io::Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct StartRequest {
|
||||
ip: String,
|
||||
}
|
||||
|
||||
async fn start_job(Json(req): Json<StartRequest>) -> String {
|
||||
let ip = req.ip.trim().to_string();
|
||||
println!("[WebServer] Starte Headless-Test für {}", ip);
|
||||
|
||||
// Klon für den Task
|
||||
let ip_for_task = ip.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = headless::run_test(&ip_for_task).await {
|
||||
eprintln!("[Headless Error] {}", e);
|
||||
}
|
||||
});
|
||||
|
||||
format!("Starte Setup für {}", ip)
|
||||
|
||||
}
|
||||
|
||||
// Port wird jetzt von außen übergeben
|
||||
pub async fn start(state: SharedState, port: u16)
|
||||
-> Result<tokio::task::JoinHandle<()>, Box<dyn std::error::Error>>
|
||||
|
|
@ -53,6 +83,7 @@ pub async fn start(state: SharedState, port: u16)
|
|||
}
|
||||
}
|
||||
}))
|
||||
.route("/api/start", post(start_job)) // 👈 Neue Route
|
||||
.nest_service("/static", ServeDir::new(static_root))
|
||||
.route("/healthz", get(|| async { "ok" }));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue