generated from methanium/template
(feat): some connection improvements
This commit is contained in:
parent
2058847d25
commit
d55b1326f8
4 changed files with 27 additions and 1 deletions
|
|
@ -7,6 +7,7 @@ use axum::{
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
routing::{delete, get, post},
|
routing::{delete, get, post},
|
||||||
};
|
};
|
||||||
|
use serde::Serialize;
|
||||||
use tower_http::{
|
use tower_http::{
|
||||||
services::{ServeDir, ServeFile},
|
services::{ServeDir, ServeFile},
|
||||||
trace::TraceLayer,
|
trace::TraceLayer,
|
||||||
|
|
@ -23,6 +24,7 @@ pub async fn serve(
|
||||||
let index = frontend.join("index.html");
|
let index = frontend.join("index.html");
|
||||||
let assets = ServeDir::new(frontend).fallback(ServeFile::new(index));
|
let assets = ServeDir::new(frontend).fallback(ServeFile::new(index));
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
|
.route("/api/config", get(public_config))
|
||||||
.route("/api/admin", get(admin))
|
.route("/api/admin", get(admin))
|
||||||
.route("/api/incidents", get(list).post(create))
|
.route("/api/incidents", get(list).post(create))
|
||||||
.route("/api/incidents/{id}", delete(remove))
|
.route("/api/incidents/{id}", delete(remove))
|
||||||
|
|
@ -35,6 +37,17 @@ pub async fn serve(
|
||||||
axum::serve(listener, app).await
|
axum::serve(listener, app).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct PublicConfig {
|
||||||
|
public_port: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn public_config(State(state): State<Arc<AppState>>) -> Json<PublicConfig> {
|
||||||
|
Json(PublicConfig {
|
||||||
|
public_port: state.public_port,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
async fn admin() -> StatusCode {
|
async fn admin() -> StatusCode {
|
||||||
StatusCode::NO_CONTENT
|
StatusCode::NO_CONTENT
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ use tokio::sync::{RwLock, watch};
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
categories: Arc<RwLock<Vec<Category>>>,
|
categories: Arc<RwLock<Vec<Category>>>,
|
||||||
incidents: IncidentStore,
|
incidents: IncidentStore,
|
||||||
|
public_port: u16,
|
||||||
updates: watch::Sender<String>,
|
updates: watch::Sender<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -70,6 +71,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
let state = Arc::new(AppState {
|
let state = Arc::new(AppState {
|
||||||
categories: Arc::clone(&categories),
|
categories: Arc::clone(&categories),
|
||||||
incidents,
|
incidents,
|
||||||
|
public_port: config.public_port,
|
||||||
updates,
|
updates,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<meta name="theme-color" content="#171e26" />
|
<meta name="theme-color" content="#171e26" />
|
||||||
|
<link rel="icon" href="/methanium.svg" type="image/svg+xml" />
|
||||||
<title>Methanium Status</title>
|
<title>Methanium Status</title>
|
||||||
<style>
|
<style>
|
||||||
html.dark,
|
html.dark,
|
||||||
|
|
|
||||||
|
|
@ -15,12 +15,22 @@ function logMTP(event: MTPLogEvent) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getMTPUrl() {
|
||||||
|
const response = await fetch("/api/config");
|
||||||
|
if (!response.ok) throw new Error("Failed to load MTP configuration");
|
||||||
|
|
||||||
|
const config = (await response.json()) as { public_port: number };
|
||||||
|
const url = new URL(location.origin);
|
||||||
|
url.port = String(config.public_port);
|
||||||
|
return url.origin;
|
||||||
|
}
|
||||||
|
|
||||||
export const mtpOptions: MTPClientOptions | null = location.pathname.startsWith(
|
export const mtpOptions: MTPClientOptions | null = location.pathname.startsWith(
|
||||||
"/edit",
|
"/edit",
|
||||||
)
|
)
|
||||||
? null
|
? null
|
||||||
: {
|
: {
|
||||||
url: configuredUrl ?? `${location.origin}/mtp`,
|
url: configuredUrl ?? (await getMTPUrl()),
|
||||||
descriptor: "methanium-status-frontend",
|
descriptor: "methanium-status-frontend",
|
||||||
pings: false,
|
pings: false,
|
||||||
logger: logMTP,
|
logger: logMTP,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue