containered launch empty web status
This commit is contained in:
@@ -3,6 +3,12 @@ name = "mm-web"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
axum.workspace = true
|
||||
mm-core.workspace = true
|
||||
serde_json.workspace = true
|
||||
tokio.workspace = true
|
||||
tracing.workspace = true
|
||||
|
||||
@@ -1,7 +1,30 @@
|
||||
//! Web status UI.
|
||||
//!
|
||||
//! Placeholder crate. Implementation starts at the Docker/status step.
|
||||
|
||||
pub mod routes;
|
||||
pub mod state;
|
||||
pub mod templates;
|
||||
|
||||
use anyhow::Context;
|
||||
use axum::Router;
|
||||
use state::WebState;
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
pub async fn run(listen: String, state: WebState) -> anyhow::Result<()> {
|
||||
let app = router(state);
|
||||
|
||||
let listener = TcpListener::bind(&listen)
|
||||
.await
|
||||
.with_context(|| format!("failed to bind web listener {}", listen))?;
|
||||
|
||||
tracing::info!(listen = %listen, "web status listening");
|
||||
|
||||
axum::serve(listener, app)
|
||||
.await
|
||||
.context("web status server failed")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn router(state: WebState) -> Router {
|
||||
routes::router(state)
|
||||
}
|
||||
|
||||
@@ -1 +1,37 @@
|
||||
//! routes module placeholder.
|
||||
use crate::{state::WebState, templates};
|
||||
use axum::{
|
||||
extract::State,
|
||||
response::{Html, IntoResponse},
|
||||
routing::get,
|
||||
Json, Router,
|
||||
};
|
||||
use mm_core::BridgeStatus;
|
||||
use serde_json::{json, Value};
|
||||
|
||||
pub fn router(state: WebState) -> Router {
|
||||
Router::new()
|
||||
.route("/", get(index))
|
||||
.route("/health", get(health))
|
||||
.route("/status", get(status))
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
async fn index(State(state): State<WebState>) -> Html<String> {
|
||||
Html(templates::index_html(&state.status))
|
||||
}
|
||||
|
||||
async fn health() -> impl IntoResponse {
|
||||
Json(json!({
|
||||
"ok": true,
|
||||
"state": "running"
|
||||
}))
|
||||
}
|
||||
|
||||
async fn status(State(state): State<WebState>) -> Json<BridgeStatus> {
|
||||
Json(state.status)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn _value_type_check(value: Value) -> Value {
|
||||
value
|
||||
}
|
||||
|
||||
@@ -1 +1,12 @@
|
||||
//! state module placeholder.
|
||||
use mm_core::BridgeStatus;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct WebState {
|
||||
pub status: BridgeStatus,
|
||||
}
|
||||
|
||||
impl WebState {
|
||||
pub fn new(status: BridgeStatus) -> Self {
|
||||
Self { status }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1,63 @@
|
||||
//! templates module placeholder.
|
||||
use mm_core::BridgeStatus;
|
||||
|
||||
pub fn index_html(status: &BridgeStatus) -> String {
|
||||
let status_json = serde_json::to_string_pretty(status)
|
||||
.unwrap_or_else(|_| "{\"error\":\"failed to encode status\"}".to_string());
|
||||
|
||||
let escaped_json = escape_html(&status_json);
|
||||
let version = escape_html(&status.bridge.version);
|
||||
let server_count = status.servers.len();
|
||||
|
||||
format!(
|
||||
r#"<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>emacs-mattermost bridge</title>
|
||||
<style>
|
||||
body {{
|
||||
font-family: system-ui, sans-serif;
|
||||
margin: 2rem;
|
||||
background: #111827;
|
||||
color: #e5e7eb;
|
||||
}}
|
||||
a {{
|
||||
color: #93c5fd;
|
||||
}}
|
||||
pre {{
|
||||
background: #030712;
|
||||
border: 1px solid #374151;
|
||||
padding: 1rem;
|
||||
overflow: auto;
|
||||
}}
|
||||
.ok {{
|
||||
color: #86efac;
|
||||
font-weight: bold;
|
||||
}}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>emacs-mattermost bridge</h1>
|
||||
<p>Status: <span class="ok">running</span></p>
|
||||
<p>Version: {version}</p>
|
||||
<p>Configured servers: {server_count}</p>
|
||||
<p>
|
||||
<a href="/health">/health</a> |
|
||||
<a href="/status">/status</a>
|
||||
</p>
|
||||
<h2>Status JSON</h2>
|
||||
<pre>{escaped_json}</pre>
|
||||
</body>
|
||||
</html>
|
||||
"#
|
||||
)
|
||||
}
|
||||
|
||||
fn escape_html(input: &str) -> String {
|
||||
input
|
||||
.replace('&', "&")
|
||||
.replace('<', "<")
|
||||
.replace('>', ">")
|
||||
.replace('"', """)
|
||||
.replace('\'', "'")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user