new debug server outline
This commit is contained in:
parent
b8afaba4ef
commit
ccb0ce87e1
45
build.rs
Normal file
45
build.rs
Normal file
@ -0,0 +1,45 @@
|
||||
fn main() {
|
||||
#[cfg(feature = "debug_server")]
|
||||
pack_debug_page().unwrap();
|
||||
|
||||
println!("cargo:rerun-if-changed=main.js");
|
||||
println!("cargo:rerun-if-changed=index.html");
|
||||
println!("cargo:rerun-if-changed=main.css");
|
||||
}
|
||||
|
||||
#[cfg(feature = "debug_server")]
|
||||
fn pack_debug_page() -> Result<(), Box<dyn std::error::Error>> {
|
||||
use std::io::Write;
|
||||
|
||||
use css_minify::optimizations::{Level, Minifier};
|
||||
|
||||
let js = std::fs::read_to_string("web/main.js").unwrap();
|
||||
let html = std::fs::read_to_string("web/index.html").unwrap();
|
||||
let css = std::fs::read_to_string("web/main.css").unwrap();
|
||||
|
||||
let mut out = Vec::new();
|
||||
minify_js::minify(
|
||||
&minify_js::Session::new(),
|
||||
minify_js::TopLevelMode::Global,
|
||||
js.as_bytes(),
|
||||
&mut out,
|
||||
)
|
||||
.unwrap();
|
||||
let js = std::str::from_utf8(&out)?;
|
||||
|
||||
let css = Minifier::default().minify(&css, Level::Three).unwrap();
|
||||
|
||||
let (head, body) = html
|
||||
.split_once("<!--INSERT HEAD CONTENT HERE-->")
|
||||
.expect("did not find split point in html");
|
||||
|
||||
let html = minify_html::minify(
|
||||
format!("{head}<style>{css}</style><script>{js}</script>{body}").as_bytes(),
|
||||
&minify_html::Cfg::spec_compliant(),
|
||||
);
|
||||
|
||||
std::fs::File::create(std::env::var("OUT_DIR").unwrap() + "/minified.html")?
|
||||
.write_all(&html)?;
|
||||
|
||||
Ok(())
|
||||
}
|
15
web/index.html
Normal file
15
web/index.html
Normal file
@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset='utf-8'>
|
||||
<meta name='viewport' content='width=device-width, initial-scale=1'>
|
||||
<title>Centralex State</title>
|
||||
<!--INSERT HEAD CONTENT HERE-->
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<list id="list" />
|
||||
</body>
|
||||
|
||||
</html>
|
3
web/main.css
Normal file
3
web/main.css
Normal file
@ -0,0 +1,3 @@
|
||||
.body {
|
||||
background-color: gray;
|
||||
}
|
22
web/main.js
Normal file
22
web/main.js
Normal file
@ -0,0 +1,22 @@
|
||||
window.onload = () => {
|
||||
const evtSource = new EventSource("/events");
|
||||
evtSource.addEventListener("change", event => {
|
||||
console.log(event);
|
||||
|
||||
const newElement = document.createElement("li");
|
||||
const eventList = document.getElementById("list");
|
||||
newElement.textContent = `change at ${+event.data}`;
|
||||
eventList.appendChild(newElement);
|
||||
|
||||
fetch("/data").then(res => res.json().then(res => console.log(res)));
|
||||
});
|
||||
|
||||
evtSource.addEventListener("ping", event => {
|
||||
console.log(event);
|
||||
|
||||
const newElement = document.createElement("li");
|
||||
const eventList = document.getElementById("list");
|
||||
newElement.textContent = `ping at ${+event.data}`;
|
||||
eventList.appendChild(newElement);
|
||||
});
|
||||
};
|
Loading…
Reference in New Issue
Block a user