This commit is contained in:
Edgar 2023-10-02 18:06:54 +02:00
parent 3f34e69b20
commit 09375e6175
No known key found for this signature in database
GPG key ID: 70ADAE8F35904387
2 changed files with 47 additions and 2 deletions

45
src/log-viewer.html Normal file
View file

@ -0,0 +1,45 @@
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
</head>
<body>
<ul id="event-list" style="max-height: 100vh; overflow-y: auto; border: 1px dashed black; padding: 4px; margin: auto">
</ul>
</body>
<script>
// Create WebSocket connection.
var loc = window.location, ws_uri;
if (loc.protocol === "https:") {
ws_uri = "wss:";
} else {
ws_uri = "ws:";
}
ws_uri += "//" + loc.host;
ws_uri += loc.pathname + "ws";
const socket = new WebSocket(ws_uri);
const target = document.querySelector('#event-list');
// Listen for messages
socket.addEventListener("message", (event) => {
let li = document.createElement("li");
let pre = document.createElement("pre");
pre.innerText = event.data;
li.appendChild(pre);
target.appendChild(li);
target.scrollTop = target.scrollHeight;
// console.log("Message from server ", event.data);
});
</script>
</html>

View file

@ -5,7 +5,7 @@ use std::{
time::Duration, time::Duration,
}; };
use axum::{routing::get, Router}; use axum::{routing::get, Router, response::Html};
use http::Method; use http::Method;
use reqwest::Client; use reqwest::Client;
use sqlx::{postgres::PgPoolOptions, PgPool}; use sqlx::{postgres::PgPoolOptions, PgPool};
@ -199,7 +199,7 @@ async fn run() -> anyhow::Result<()> {
}); });
let app = Router::new() let app = Router::new()
.route("/", get(|| async { include_str!("../README.md") })) .route("/", get(|| async { Html(include_str!("log-viewer.html")) }))
.route("/ws", get(routes::ws_handler)) .route("/ws", get(routes::ws_handler))
.layer(TraceLayer::new_for_http()) .layer(TraceLayer::new_for_http())
.layer(TimeoutLayer::new(Duration::from_secs(5))) .layer(TimeoutLayer::new(Duration::from_secs(5)))