containered launch empty web status
This commit is contained in:
@@ -3,6 +3,7 @@ name = "mm-bridge"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
[[bin]]
|
||||
name = "mm-bridge"
|
||||
@@ -11,6 +12,7 @@ path = "src/main.rs"
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
mm-core.workspace = true
|
||||
mm-web.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
tokio.workspace = true
|
||||
|
||||
@@ -3,28 +3,27 @@ use std::env;
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BridgeConfig {
|
||||
pub listen: String,
|
||||
pub web_listen: String,
|
||||
}
|
||||
|
||||
impl BridgeConfig {
|
||||
pub fn from_env() -> anyhow::Result<Self> {
|
||||
let listen =
|
||||
env::var("MM_BRIDGE_LISTEN").unwrap_or_else(|_| "127.0.0.1:8765".to_string());
|
||||
let listen = read_addr("MM_BRIDGE_LISTEN", "127.0.0.1:8765")?;
|
||||
let web_listen = read_addr("MM_WEB_LISTEN", "127.0.0.1:8766")?;
|
||||
|
||||
validate_listen(&listen)?;
|
||||
|
||||
Ok(Self { listen })
|
||||
Ok(Self { listen, web_listen })
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_listen(listen: &str) -> anyhow::Result<()> {
|
||||
anyhow::ensure!(
|
||||
!listen.trim().is_empty(),
|
||||
"MM_BRIDGE_LISTEN must not be empty"
|
||||
);
|
||||
anyhow::ensure!(
|
||||
listen.contains(':'),
|
||||
"MM_BRIDGE_LISTEN must be host:port"
|
||||
);
|
||||
fn read_addr(name: &str, default: &str) -> anyhow::Result<String> {
|
||||
let value = env::var(name).unwrap_or_else(|_| default.to_string());
|
||||
validate_listen(name, &value)?;
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
fn validate_listen(name: &str, listen: &str) -> anyhow::Result<()> {
|
||||
anyhow::ensure!(!listen.trim().is_empty(), "{name} must not be empty");
|
||||
anyhow::ensure!(listen.contains(':'), "{name} must be host:port");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -34,11 +33,11 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn rejects_empty_listen() {
|
||||
assert!(validate_listen("").is_err());
|
||||
assert!(validate_listen("MM_BRIDGE_LISTEN", "").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepts_host_port() {
|
||||
assert!(validate_listen("127.0.0.1:8765").is_ok());
|
||||
assert!(validate_listen("MM_BRIDGE_LISTEN", "127.0.0.1:8765").is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,8 +13,8 @@ async fn main() -> anyhow::Result<()> {
|
||||
}
|
||||
|
||||
fn init_tracing() {
|
||||
let filter = EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| EnvFilter::new("mm_bridge=info,warn"));
|
||||
let filter =
|
||||
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("mm_bridge=info,warn"));
|
||||
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(filter)
|
||||
|
||||
@@ -1,14 +1,33 @@
|
||||
use crate::{config::BridgeConfig, transport};
|
||||
use crate::{config::BridgeConfig, status, transport};
|
||||
use anyhow::Context;
|
||||
use mm_web::state::WebState;
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
pub async fn run(config: BridgeConfig) -> anyhow::Result<()> {
|
||||
let listener = TcpListener::bind(&config.listen)
|
||||
let tcp_listener = TcpListener::bind(&config.listen)
|
||||
.await
|
||||
.with_context(|| format!("failed to bind {}", config.listen))?;
|
||||
|
||||
tracing::info!(listen = %config.listen, "bridge listening");
|
||||
|
||||
let web_state = WebState::new(status::current_status());
|
||||
let web_task = tokio::spawn(mm_web::run(config.web_listen.clone(), web_state));
|
||||
|
||||
tokio::select! {
|
||||
result = accept_loop(tcp_listener) => {
|
||||
result
|
||||
}
|
||||
result = web_task => {
|
||||
match result {
|
||||
Ok(Ok(())) => Ok(()),
|
||||
Ok(Err(error)) => Err(error).context("web status task failed"),
|
||||
Err(error) => Err(error).context("web status task join failed"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn accept_loop(listener: TcpListener) -> anyhow::Result<()> {
|
||||
loop {
|
||||
let (stream, peer) = listener.accept().await.context("accept failed")?;
|
||||
tracing::debug!(%peer, "accepted connection");
|
||||
|
||||
@@ -1,17 +1,21 @@
|
||||
use mm_core::{BridgeState, ServerSummary};
|
||||
use serde_json::{json, Value};
|
||||
use mm_core::BridgeStatus;
|
||||
use serde_json::Value;
|
||||
|
||||
pub const BRIDGE_VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
pub fn status_result() -> Value {
|
||||
let servers: Vec<ServerSummary> = Vec::new();
|
||||
pub fn current_status() -> BridgeStatus {
|
||||
BridgeStatus::running(BRIDGE_VERSION)
|
||||
}
|
||||
|
||||
json!({
|
||||
"bridge": {
|
||||
"version": BRIDGE_VERSION,
|
||||
"state": BridgeState::Running,
|
||||
},
|
||||
"servers": servers,
|
||||
pub fn status_result() -> Value {
|
||||
serde_json::to_value(current_status()).unwrap_or_else(|_| {
|
||||
serde_json::json!({
|
||||
"bridge": {
|
||||
"version": BRIDGE_VERSION,
|
||||
"state": "running"
|
||||
},
|
||||
"servers": []
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ name = "mm-client"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
mm-core.workspace = true
|
||||
|
||||
@@ -3,6 +3,7 @@ name = "mm-core"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
serde.workspace = true
|
||||
|
||||
@@ -8,4 +8,4 @@ pub mod model;
|
||||
|
||||
pub use error::{BridgeError, ErrorKind};
|
||||
pub use ids::{ChannelId, PostId, ServerId, TeamId, UserId};
|
||||
pub use model::{BridgeState, ServerSummary};
|
||||
pub use model::{BridgeInfo, BridgeState, BridgeStatus, ServerSummary};
|
||||
|
||||
@@ -12,3 +12,27 @@ pub struct ServerSummary {
|
||||
pub server_id: ServerId,
|
||||
pub state: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct BridgeInfo {
|
||||
pub version: String,
|
||||
pub state: BridgeState,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct BridgeStatus {
|
||||
pub bridge: BridgeInfo,
|
||||
pub servers: Vec<ServerSummary>,
|
||||
}
|
||||
|
||||
impl BridgeStatus {
|
||||
pub fn running(version: impl Into<String>) -> Self {
|
||||
Self {
|
||||
bridge: BridgeInfo {
|
||||
version: version.into(),
|
||||
state: BridgeState::Running,
|
||||
},
|
||||
servers: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ name = "mm-store"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
mm-core.workspace = true
|
||||
|
||||
@@ -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