Added Core Features chroium, config,console_ui,headless,webserver,states
This commit is contained in:
parent
4c6c6d572c
commit
c3c60efb81
13 changed files with 688 additions and 0 deletions
61
Core/web_server/assets.rs
Normal file
61
Core/web_server/assets.rs
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
pub const DEFAULT_INDEX_HTML: &str = r#"
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>Aurora Print — UI</title>
|
||||
<link rel="stylesheet" href="/static/styles.css" />
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>Aurora Print</h1>
|
||||
</header>
|
||||
<main>
|
||||
<section class="card">
|
||||
<h2>Gerät erkennen</h2>
|
||||
<label for="ip">IP-Adresse</label>
|
||||
<input id="ip" type="text" placeholder="z.B. 192.168.1.50" />
|
||||
<button id="btn-start">Start</button>
|
||||
<p id="status" class="status"></p>
|
||||
</section>
|
||||
</main>
|
||||
<script src="/static/app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
"#;
|
||||
|
||||
pub const DEFAULT_APP_JS: &str = r#"
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const ip = document.getElementById('ip');
|
||||
const btn = document.getElementById('btn-start');
|
||||
const status = document.getElementById('status');
|
||||
|
||||
btn.addEventListener('click', async () => {
|
||||
const val = (ip.value || '').trim();
|
||||
if (!val) {
|
||||
status.textContent = 'Bitte IP eingeben.';
|
||||
return;
|
||||
}
|
||||
status.textContent = `Erkenne Gerät bei ${val} … (Demo)`;
|
||||
});
|
||||
});
|
||||
"#;
|
||||
|
||||
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;
|
||||
background: #0b0f19; color: #e6eefc; }
|
||||
header { padding: 16px 24px; border-bottom: 1px solid #1c2233; }
|
||||
h1 { margin: 0; font-size: 20px; }
|
||||
main { max-width: 720px; margin: 32px auto; padding: 0 16px; }
|
||||
.card { background: #121829; border: 1px solid #1f2740; border-radius: 12px; padding: 16px;
|
||||
box-shadow: 0 6px 18px rgba(0,0,0,0.25); }
|
||||
label { display:block; margin: 8px 0 4px; color: #9db4ff; }
|
||||
input { width: 100%; padding: 10px 12px; border-radius: 8px; border: 1px solid #34406a;
|
||||
background: #0e1424; color: #e6eefc; }
|
||||
button { margin-top: 12px; padding: 10px 14px; border-radius: 8px; border: 1px solid #3a5bff;
|
||||
background: #3056ff; color: white; cursor: pointer; }
|
||||
button:hover { filter: brightness(1.05); }
|
||||
.status { margin-top: 10px; color: #9db4ff; }
|
||||
"#;
|
||||
71
Core/web_server/mod.rs
Normal file
71
Core/web_server/mod.rs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
use std::{
|
||||
env, fs,
|
||||
net::SocketAddr,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use axum::{routing::get, Router, response::Html};
|
||||
use tower_http::services::ServeDir;
|
||||
|
||||
mod assets;
|
||||
use assets::{DEFAULT_INDEX_HTML, DEFAULT_APP_JS, DEFAULT_STYLES_CSS};
|
||||
|
||||
use crate::state::SharedState; // ⬅️ nur noch state importieren
|
||||
|
||||
const WEB_DIR: &str = "web";
|
||||
|
||||
fn exe_dir() -> PathBuf {
|
||||
env::current_exe()
|
||||
.ok()
|
||||
.and_then(|p| p.parent().map(|p| p.to_path_buf()))
|
||||
.unwrap_or_else(|| PathBuf::from("."))
|
||||
}
|
||||
|
||||
fn rebuild_web_assets(dir: &Path) -> std::io::Result<()> {
|
||||
let web = dir.join(WEB_DIR);
|
||||
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())?;
|
||||
fs::write(web.join("styles.css"), DEFAULT_STYLES_CSS.trim_start())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Port wird jetzt von außen übergeben
|
||||
pub async fn start(state: SharedState, port: u16)
|
||||
-> Result<tokio::task::JoinHandle<()>, Box<dyn std::error::Error>>
|
||||
{
|
||||
let dir = exe_dir();
|
||||
rebuild_web_assets(&dir)?;
|
||||
|
||||
let web_root = dir.join(WEB_DIR);
|
||||
let static_root = web_root.clone();
|
||||
let index_path = web_root.join("index.html");
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", get({
|
||||
let index_path = index_path.clone();
|
||||
move || {
|
||||
let index_path = index_path.clone();
|
||||
async move {
|
||||
let html = fs::read_to_string(&index_path)
|
||||
.unwrap_or_else(|_| "<h1>index.html fehlt</h1>".to_string());
|
||||
Html(html)
|
||||
}
|
||||
}
|
||||
}))
|
||||
.nest_service("/static", ServeDir::new(static_root))
|
||||
.route("/healthz", get(|| async { "ok" }));
|
||||
|
||||
{
|
||||
let mut s = state.lock().unwrap();
|
||||
s.web_server_active = true;
|
||||
}
|
||||
|
||||
let addr: SocketAddr = format!("0.0.0.0:{}", port).parse()?;
|
||||
let listener = tokio::net::TcpListener::bind(addr).await?;
|
||||
let handle = tokio::spawn(async move {
|
||||
let _ = axum::serve(listener, app).await;
|
||||
});
|
||||
|
||||
Ok(handle)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue