Fix admin SQL count parsing for local SpacetimeDB
This commit is contained in:
@@ -4,14 +4,14 @@ use std::{
|
||||
};
|
||||
|
||||
use axum::{
|
||||
Json,
|
||||
extract::{Extension, Request, State},
|
||||
http::{
|
||||
HeaderMap, HeaderName, HeaderValue, Method, StatusCode,
|
||||
header::{AUTHORIZATION, CONTENT_TYPE},
|
||||
HeaderMap, HeaderName, HeaderValue, Method, StatusCode,
|
||||
},
|
||||
middleware::Next,
|
||||
response::{Html, Response},
|
||||
response::Response,
|
||||
Json,
|
||||
};
|
||||
use reqwest::Client;
|
||||
use serde::Deserialize;
|
||||
@@ -21,7 +21,7 @@ use shared_contracts::admin::{
|
||||
AdminDebugHttpRequest, AdminDebugHttpResponse, AdminLoginRequest, AdminLoginResponse,
|
||||
AdminMeResponse, AdminOverviewResponse, AdminServiceOverviewPayload, AdminSessionPayload,
|
||||
};
|
||||
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
|
||||
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
|
||||
|
||||
use crate::{
|
||||
api_response::json_success_body,
|
||||
@@ -76,6 +76,9 @@ const DATABASE_OVERVIEW_TABLES: &[&str] = &[
|
||||
"asset_object",
|
||||
"asset_entity_binding",
|
||||
];
|
||||
// SpacetimeDB 2.x 的 schema HTTP API 要求显式传入 BSATN JSON 版本。
|
||||
// 后台总览只读取表名,固定使用当前 CLI 2.1.0 兼容的版本参数即可。
|
||||
const SPACETIME_SCHEMA_VERSION_QUERY: &str = "version=9";
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AuthenticatedAdmin {
|
||||
@@ -100,17 +103,6 @@ struct SpacetimeSchemaTable {
|
||||
name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct SpacetimeSqlRow {
|
||||
#[serde(flatten)]
|
||||
columns: serde_json::Map<String, Value>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct SpacetimeSqlResponse {
|
||||
rows: Option<Vec<SpacetimeSqlRow>>,
|
||||
}
|
||||
|
||||
impl AuthenticatedAdmin {
|
||||
pub fn new(session: AdminSessionPayload) -> Self {
|
||||
Self { session }
|
||||
@@ -121,10 +113,6 @@ impl AuthenticatedAdmin {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn admin_console_page() -> Html<&'static str> {
|
||||
Html(ADMIN_CONSOLE_HTML)
|
||||
}
|
||||
|
||||
pub async fn admin_login(
|
||||
State(state): State<AppState>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
@@ -287,7 +275,7 @@ async fn fetch_database_overview(state: &AppState) -> AdminDatabaseOverviewPaylo
|
||||
|
||||
let schema = fetch_spacetime_json::<SpacetimeSchemaResponse>(
|
||||
&client,
|
||||
&format!("{server_root}/v1/database/{database}/schema"),
|
||||
&build_spacetime_schema_url(server_root, database),
|
||||
token,
|
||||
)
|
||||
.await
|
||||
@@ -353,6 +341,10 @@ async fn fetch_database_overview(state: &AppState) -> AdminDatabaseOverviewPaylo
|
||||
}
|
||||
}
|
||||
|
||||
fn build_spacetime_schema_url(server_root: &str, database: &str) -> String {
|
||||
format!("{server_root}/v1/database/{database}/schema?{SPACETIME_SCHEMA_VERSION_QUERY}")
|
||||
}
|
||||
|
||||
async fn fetch_spacetime_json<T>(
|
||||
client: &Client,
|
||||
url: &str,
|
||||
@@ -409,17 +401,63 @@ async fn fetch_spacetime_sql_count(
|
||||
}
|
||||
|
||||
let payload = response
|
||||
.json::<SpacetimeSqlResponse>()
|
||||
.json::<Value>()
|
||||
.await
|
||||
.map_err(|error| format!("SQL 响应解析失败:{error}"))?;
|
||||
let row = payload
|
||||
.rows
|
||||
.and_then(|rows| rows.into_iter().next())
|
||||
.ok_or_else(|| "SQL 结果为空".to_string())?;
|
||||
extract_sql_count(row.columns)
|
||||
parse_spacetime_sql_count_response(payload)
|
||||
}
|
||||
|
||||
fn extract_sql_count(columns: serde_json::Map<String, Value>) -> Result<u64, String> {
|
||||
fn parse_spacetime_sql_count_response(payload: Value) -> Result<u64, String> {
|
||||
match payload {
|
||||
// SpacetimeDB 2.x /sql 返回 statement result 数组,每个 result 内含 schema 与 rows。
|
||||
Value::Array(statements) => {
|
||||
let statement = statements
|
||||
.into_iter()
|
||||
.next()
|
||||
.ok_or_else(|| "SQL 结果为空".to_string())?;
|
||||
extract_sql_count_from_statement(statement)
|
||||
}
|
||||
// 保留兼容旧对象形状,便于本地/远端 API 小版本差异时仍能读取计数。
|
||||
Value::Object(statement) => extract_sql_count_from_statement(Value::Object(statement)),
|
||||
_ => Err("SQL 响应格式非法".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_sql_count_from_statement(statement: Value) -> Result<u64, String> {
|
||||
let Value::Object(mut statement) = statement else {
|
||||
return Err("SQL statement 结果格式非法".to_string());
|
||||
};
|
||||
|
||||
let schema = statement.remove("schema");
|
||||
let rows = statement
|
||||
.remove("rows")
|
||||
.ok_or_else(|| "SQL 响应缺少 rows 字段".to_string())?;
|
||||
extract_sql_count_from_rows(rows, schema.as_ref())
|
||||
}
|
||||
|
||||
fn extract_sql_count_from_rows(rows: Value, schema: Option<&Value>) -> Result<u64, String> {
|
||||
let Value::Array(rows) = rows else {
|
||||
return Err("SQL rows 字段格式非法".to_string());
|
||||
};
|
||||
let row = rows.first().ok_or_else(|| "SQL 结果为空".to_string())?;
|
||||
extract_sql_count_from_row(row, schema)
|
||||
}
|
||||
|
||||
fn extract_sql_count_from_row(row: &Value, schema: Option<&Value>) -> Result<u64, String> {
|
||||
match row {
|
||||
Value::Object(columns) => extract_sql_count(columns),
|
||||
Value::Array(values) => {
|
||||
let count_index = schema.and_then(find_sql_count_column_index).unwrap_or(0);
|
||||
values
|
||||
.get(count_index)
|
||||
.ok_or_else(|| "SQL 结果缺少 count 字段".to_string())
|
||||
.and_then(parse_count_value)
|
||||
}
|
||||
value => parse_count_value(value),
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_sql_count(columns: &serde_json::Map<String, Value>) -> Result<u64, String> {
|
||||
for key in ["row_count", "count", "COUNT(*)"] {
|
||||
if let Some(value) = columns.get(key) {
|
||||
return parse_count_value(value);
|
||||
@@ -432,6 +470,25 @@ fn extract_sql_count(columns: serde_json::Map<String, Value>) -> Result<u64, Str
|
||||
.and_then(parse_count_value)
|
||||
}
|
||||
|
||||
fn find_sql_count_column_index(schema: &Value) -> Option<usize> {
|
||||
let elements = schema.get("elements")?.as_array()?;
|
||||
elements.iter().position(|element| {
|
||||
element
|
||||
.get("name")
|
||||
.and_then(extract_sql_schema_name)
|
||||
.map(|name| matches!(name, "row_count" | "count" | "COUNT(*)"))
|
||||
.unwrap_or(false)
|
||||
})
|
||||
}
|
||||
|
||||
fn extract_sql_schema_name(value: &Value) -> Option<&str> {
|
||||
match value {
|
||||
Value::String(text) => Some(text.as_str()),
|
||||
Value::Object(object) => object.get("some").and_then(Value::as_str),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_count_value(value: &Value) -> Result<u64, String> {
|
||||
match value {
|
||||
Value::Number(number) => number
|
||||
@@ -602,520 +659,14 @@ fn build_admin_session_payload(session: crate::state::AdminSession) -> AdminSess
|
||||
}
|
||||
}
|
||||
|
||||
// 首版后台页面内嵌在 api-server,避免新增独立前端工程与静态资源发布链。
|
||||
static ADMIN_CONSOLE_HTML: &str = r#"<!doctype html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>Genarrative 管理后台</title>
|
||||
<style>
|
||||
:root {
|
||||
--bg: linear-gradient(180deg, #f4efe5 0%, #e7ddd0 100%);
|
||||
--panel: rgba(255, 249, 240, 0.92);
|
||||
--panel-strong: #fffaf2;
|
||||
--line: rgba(90, 61, 41, 0.14);
|
||||
--text: #2f241d;
|
||||
--muted: #7b6657;
|
||||
--accent: #b45a2f;
|
||||
--accent-strong: #8f431f;
|
||||
--ok: #2c7a54;
|
||||
--danger: #a63f2f;
|
||||
--shadow: 0 20px 50px rgba(70, 41, 19, 0.12);
|
||||
--radius: 20px;
|
||||
--font: "Microsoft YaHei", "PingFang SC", "Segoe UI", sans-serif;
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: var(--font);
|
||||
color: var(--text);
|
||||
background: var(--bg);
|
||||
min-height: 100vh;
|
||||
}
|
||||
.shell {
|
||||
max-width: 1180px;
|
||||
margin: 0 auto;
|
||||
padding: 20px 16px 40px;
|
||||
}
|
||||
.hero {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: flex-start;
|
||||
gap: 16px;
|
||||
margin-bottom: 18px;
|
||||
}
|
||||
.hero h1 {
|
||||
margin: 0;
|
||||
font-size: 28px;
|
||||
line-height: 1.1;
|
||||
letter-spacing: 0.02em;
|
||||
}
|
||||
.hero p {
|
||||
margin: 10px 0 0;
|
||||
color: var(--muted);
|
||||
font-size: 14px;
|
||||
}
|
||||
.status-chip {
|
||||
padding: 10px 14px;
|
||||
border-radius: 999px;
|
||||
background: rgba(180, 90, 47, 0.1);
|
||||
color: var(--accent-strong);
|
||||
font-size: 13px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.grid {
|
||||
display: grid;
|
||||
grid-template-columns: 340px minmax(0, 1fr);
|
||||
gap: 16px;
|
||||
}
|
||||
.panel {
|
||||
background: var(--panel);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: var(--radius);
|
||||
box-shadow: var(--shadow);
|
||||
backdrop-filter: blur(12px);
|
||||
}
|
||||
.panel-head {
|
||||
padding: 18px 18px 0;
|
||||
}
|
||||
.panel-head h2 {
|
||||
margin: 0;
|
||||
font-size: 18px;
|
||||
}
|
||||
.panel-head p {
|
||||
margin: 8px 0 0;
|
||||
color: var(--muted);
|
||||
font-size: 13px;
|
||||
}
|
||||
.panel-body {
|
||||
padding: 18px;
|
||||
}
|
||||
.form {
|
||||
display: grid;
|
||||
gap: 12px;
|
||||
}
|
||||
label {
|
||||
display: grid;
|
||||
gap: 6px;
|
||||
font-size: 13px;
|
||||
color: var(--muted);
|
||||
}
|
||||
input, textarea, select {
|
||||
width: 100%;
|
||||
border: 1px solid rgba(78, 53, 37, 0.12);
|
||||
border-radius: 14px;
|
||||
background: var(--panel-strong);
|
||||
color: var(--text);
|
||||
font: inherit;
|
||||
padding: 12px 14px;
|
||||
outline: none;
|
||||
}
|
||||
textarea {
|
||||
min-height: 140px;
|
||||
resize: vertical;
|
||||
}
|
||||
.btn-row {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
button {
|
||||
border: none;
|
||||
border-radius: 14px;
|
||||
padding: 11px 16px;
|
||||
background: var(--accent);
|
||||
color: #fff8f2;
|
||||
font: inherit;
|
||||
cursor: pointer;
|
||||
}
|
||||
button.secondary {
|
||||
background: rgba(180, 90, 47, 0.14);
|
||||
color: var(--accent-strong);
|
||||
}
|
||||
button:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: wait;
|
||||
}
|
||||
.stack {
|
||||
display: grid;
|
||||
gap: 16px;
|
||||
}
|
||||
.metrics {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
|
||||
gap: 12px;
|
||||
}
|
||||
.metric {
|
||||
padding: 14px;
|
||||
border-radius: 16px;
|
||||
border: 1px solid var(--line);
|
||||
background: rgba(255,255,255,0.45);
|
||||
}
|
||||
.metric .k {
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
}
|
||||
.metric .v {
|
||||
margin-top: 8px;
|
||||
font-size: 18px;
|
||||
font-weight: 700;
|
||||
}
|
||||
.data-grid {
|
||||
display: grid;
|
||||
gap: 10px;
|
||||
}
|
||||
.row {
|
||||
display: grid;
|
||||
grid-template-columns: 160px 1fr;
|
||||
gap: 10px;
|
||||
font-size: 13px;
|
||||
align-items: start;
|
||||
padding: 10px 0;
|
||||
border-bottom: 1px solid rgba(78, 53, 37, 0.08);
|
||||
}
|
||||
.row:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
.row .k {
|
||||
color: var(--muted);
|
||||
}
|
||||
.table-list {
|
||||
display: grid;
|
||||
gap: 10px;
|
||||
max-height: 420px;
|
||||
overflow: auto;
|
||||
padding-right: 4px;
|
||||
}
|
||||
.table-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
padding: 12px 14px;
|
||||
border-radius: 14px;
|
||||
background: rgba(255,255,255,0.52);
|
||||
border: 1px solid rgba(78, 53, 37, 0.08);
|
||||
align-items: center;
|
||||
}
|
||||
.table-item small {
|
||||
color: var(--muted);
|
||||
display: block;
|
||||
margin-top: 4px;
|
||||
}
|
||||
.count {
|
||||
font-weight: 700;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.count.err {
|
||||
color: var(--danger);
|
||||
font-weight: 600;
|
||||
}
|
||||
.result-panel {
|
||||
border-radius: 16px;
|
||||
border: 1px solid var(--line);
|
||||
background: rgba(255,255,255,0.55);
|
||||
padding: 14px;
|
||||
display: grid;
|
||||
gap: 10px;
|
||||
}
|
||||
pre {
|
||||
margin: 0;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
font-size: 12px;
|
||||
line-height: 1.5;
|
||||
background: rgba(47, 36, 29, 0.06);
|
||||
border-radius: 14px;
|
||||
padding: 12px;
|
||||
overflow: auto;
|
||||
}
|
||||
.hint {
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
}
|
||||
.err-text { color: var(--danger); }
|
||||
.ok-text { color: var(--ok); }
|
||||
@media (max-width: 900px) {
|
||||
.grid { grid-template-columns: 1fr; }
|
||||
.hero { flex-direction: column; }
|
||||
.row { grid-template-columns: 1fr; gap: 4px; }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="shell">
|
||||
<div class="hero">
|
||||
<div>
|
||||
<h1>Genarrative 管理后台</h1>
|
||||
<p>查看服务状态、数据库概览,并对当前 API 做受控调试。</p>
|
||||
</div>
|
||||
<div id="admin-status" class="status-chip">未登录</div>
|
||||
</div>
|
||||
|
||||
<div class="grid">
|
||||
<div class="stack">
|
||||
<section class="panel">
|
||||
<div class="panel-head">
|
||||
<h2>管理员登录</h2>
|
||||
<p>使用配置的后台账号进入管理域。</p>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<form id="login-form" class="form">
|
||||
<label>用户名
|
||||
<input id="login-username" name="username" autocomplete="username" />
|
||||
</label>
|
||||
<label>密码
|
||||
<input id="login-password" name="password" type="password" autocomplete="current-password" />
|
||||
</label>
|
||||
<div class="btn-row">
|
||||
<button id="login-submit" type="submit">登录后台</button>
|
||||
<button id="login-clear" class="secondary" type="button">清空令牌</button>
|
||||
</div>
|
||||
<div id="login-message" class="hint"></div>
|
||||
</form>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="panel">
|
||||
<div class="panel-head">
|
||||
<h2>API 调试</h2>
|
||||
<p>对当前服务做同源受控请求。</p>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<form id="debug-form" class="form">
|
||||
<label>方法
|
||||
<select id="debug-method">
|
||||
<option>GET</option>
|
||||
<option>POST</option>
|
||||
<option>PUT</option>
|
||||
<option>DELETE</option>
|
||||
<option>PATCH</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>路径
|
||||
<input id="debug-path" value="/healthz" />
|
||||
</label>
|
||||
<label>附加请求头(JSON 数组)
|
||||
<textarea id="debug-headers">[]</textarea>
|
||||
</label>
|
||||
<label>请求体
|
||||
<textarea id="debug-body"></textarea>
|
||||
</label>
|
||||
<div class="btn-row">
|
||||
<button id="debug-submit" type="submit">发送调试请求</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<div class="stack">
|
||||
<section class="panel">
|
||||
<div class="panel-head">
|
||||
<h2>数据库概览</h2>
|
||||
<p>读取当前服务配置和 SpacetimeDB 数据库真相。</p>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<div class="btn-row" style="margin-bottom:14px;">
|
||||
<button id="refresh-overview" type="button">刷新概览</button>
|
||||
</div>
|
||||
<div id="overview-metrics" class="metrics"></div>
|
||||
<div id="overview-detail" class="data-grid" style="margin-top:14px;"></div>
|
||||
<div id="overview-errors" class="hint err-text" style="margin-top:10px;"></div>
|
||||
<div id="overview-tables" class="table-list" style="margin-top:14px;"></div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="panel">
|
||||
<div class="panel-head">
|
||||
<h2>调试结果</h2>
|
||||
<p>返回状态、响应头和内容预览。</p>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<div id="debug-result" class="result-panel">
|
||||
<div class="hint">尚未执行调试请求。</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const TOKEN_KEY = 'genarrative_admin_token';
|
||||
const statusEl = document.getElementById('admin-status');
|
||||
const loginMessageEl = document.getElementById('login-message');
|
||||
const overviewMetricsEl = document.getElementById('overview-metrics');
|
||||
const overviewDetailEl = document.getElementById('overview-detail');
|
||||
const overviewTablesEl = document.getElementById('overview-tables');
|
||||
const overviewErrorsEl = document.getElementById('overview-errors');
|
||||
const debugResultEl = document.getElementById('debug-result');
|
||||
|
||||
function getToken() {
|
||||
return window.localStorage.getItem(TOKEN_KEY) || '';
|
||||
}
|
||||
|
||||
function setToken(token) {
|
||||
if (!token) {
|
||||
window.localStorage.removeItem(TOKEN_KEY);
|
||||
return;
|
||||
}
|
||||
window.localStorage.setItem(TOKEN_KEY, token);
|
||||
}
|
||||
|
||||
function setStatus(text, ok) {
|
||||
statusEl.textContent = text;
|
||||
statusEl.style.background = ok ? 'rgba(44,122,84,0.12)' : 'rgba(180,90,47,0.1)';
|
||||
statusEl.style.color = ok ? '#2c7a54' : '#8f431f';
|
||||
}
|
||||
|
||||
async function request(path, options = {}) {
|
||||
const headers = new Headers(options.headers || {});
|
||||
const token = getToken();
|
||||
if (token) {
|
||||
headers.set('authorization', `Bearer ${token}`);
|
||||
}
|
||||
if (options.json !== undefined) {
|
||||
headers.set('content-type', 'application/json');
|
||||
options.body = JSON.stringify(options.json);
|
||||
}
|
||||
const response = await fetch(path, { ...options, headers });
|
||||
const text = await response.text();
|
||||
let data = null;
|
||||
try { data = text ? JSON.parse(text) : null; } catch (_) {}
|
||||
if (!response.ok) {
|
||||
const message = data?.error?.message || data?.message || text || `HTTP ${response.status}`;
|
||||
throw new Error(message);
|
||||
}
|
||||
return data?.data ?? data;
|
||||
}
|
||||
|
||||
function renderOverview(overview) {
|
||||
const service = overview.service || {};
|
||||
const database = overview.database || {};
|
||||
const stats = Array.isArray(database.tableStats) ? database.tableStats : [];
|
||||
overviewMetricsEl.innerHTML = `
|
||||
<div class="metric"><div class="k">后台状态</div><div class="v">${service.adminEnabled ? '已启用' : '未启用'}</div></div>
|
||||
<div class="metric"><div class="k">服务监听</div><div class="v">${service.bindHost || '-'}:${service.bindPort || '-'}</div></div>
|
||||
<div class="metric"><div class="k">SpacetimeDB</div><div class="v">${service.spacetimeDatabase || '-'}</div></div>
|
||||
<div class="metric"><div class="k">统计表数</div><div class="v">${stats.length}</div></div>
|
||||
`;
|
||||
overviewDetailEl.innerHTML = `
|
||||
<div class="row"><div class="k">JWT Issuer</div><div>${service.jwtIssuer || '-'}</div></div>
|
||||
<div class="row"><div class="k">Spacetime 服务</div><div>${service.spacetimeServerUrl || '-'}</div></div>
|
||||
<div class="row"><div class="k">数据库 Identity</div><div>${database.databaseIdentity || '-'}</div></div>
|
||||
<div class="row"><div class="k">Owner Identity</div><div>${database.ownerIdentity || '-'}</div></div>
|
||||
<div class="row"><div class="k">Host Type</div><div>${database.hostType || '-'}</div></div>
|
||||
<div class="row"><div class="k">Schema 表数量</div><div>${(database.schemaTableNames || []).length}</div></div>
|
||||
`;
|
||||
overviewTablesEl.innerHTML = stats.map((item) => `
|
||||
<div class="table-item">
|
||||
<div>
|
||||
<strong>${item.tableName}</strong>
|
||||
${item.errorMessage ? `<small class="err-text">${item.errorMessage}</small>` : ''}
|
||||
</div>
|
||||
<div class="count ${item.errorMessage ? 'err' : ''}">${item.rowCount ?? '失败'}</div>
|
||||
</div>
|
||||
`).join('');
|
||||
overviewErrorsEl.textContent = (database.fetchErrors || []).join(' | ');
|
||||
}
|
||||
|
||||
function renderDebugResult(result) {
|
||||
const headerText = (result.headers || []).map((item) => `${item.name}: ${item.value}`).join('\n');
|
||||
debugResultEl.innerHTML = `
|
||||
<div><strong>状态:</strong><span class="${result.status < 400 ? 'ok-text' : 'err-text'}">${result.status} ${result.statusText}</span></div>
|
||||
<div><strong>响应头</strong><pre>${headerText || '(无)'}</pre></div>
|
||||
<div><strong>响应体预览</strong><pre>${result.bodyText || '(空)'}</pre></div>
|
||||
<div><strong>响应 JSON</strong><pre>${result.bodyJson ? JSON.stringify(result.bodyJson, null, 2) : '(不是 JSON)'}</pre></div>
|
||||
`;
|
||||
}
|
||||
|
||||
async function loadMe() {
|
||||
const token = getToken();
|
||||
if (!token) {
|
||||
setStatus('未登录', false);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const result = await request('/admin/api/me');
|
||||
setStatus(`管理员:${result.admin.displayName}`, true);
|
||||
} catch (error) {
|
||||
setToken('');
|
||||
setStatus('未登录', false);
|
||||
}
|
||||
}
|
||||
|
||||
async function loadOverview() {
|
||||
try {
|
||||
const overview = await request('/admin/api/overview');
|
||||
renderOverview(overview);
|
||||
} catch (error) {
|
||||
overviewMetricsEl.innerHTML = '';
|
||||
overviewDetailEl.innerHTML = '';
|
||||
overviewTablesEl.innerHTML = '';
|
||||
overviewErrorsEl.textContent = error.message;
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById('login-form').addEventListener('submit', async (event) => {
|
||||
event.preventDefault();
|
||||
loginMessageEl.textContent = '正在登录...';
|
||||
try {
|
||||
const result = await request('/admin/api/login', {
|
||||
method: 'POST',
|
||||
json: {
|
||||
username: document.getElementById('login-username').value,
|
||||
password: document.getElementById('login-password').value,
|
||||
},
|
||||
});
|
||||
setToken(result.token);
|
||||
loginMessageEl.textContent = '登录成功';
|
||||
await loadMe();
|
||||
await loadOverview();
|
||||
} catch (error) {
|
||||
loginMessageEl.textContent = error.message;
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById('login-clear').addEventListener('click', () => {
|
||||
setToken('');
|
||||
setStatus('未登录', false);
|
||||
loginMessageEl.textContent = '已清空本地令牌';
|
||||
});
|
||||
|
||||
document.getElementById('refresh-overview').addEventListener('click', async () => {
|
||||
await loadOverview();
|
||||
});
|
||||
|
||||
document.getElementById('debug-form').addEventListener('submit', async (event) => {
|
||||
event.preventDefault();
|
||||
debugResultEl.innerHTML = '<div class="hint">正在请求...</div>';
|
||||
try {
|
||||
const headers = JSON.parse(document.getElementById('debug-headers').value || '[]');
|
||||
const result = await request('/admin/api/debug/http', {
|
||||
method: 'POST',
|
||||
json: {
|
||||
method: document.getElementById('debug-method').value,
|
||||
path: document.getElementById('debug-path').value,
|
||||
headers,
|
||||
body: document.getElementById('debug-body').value,
|
||||
},
|
||||
});
|
||||
renderDebugResult(result);
|
||||
} catch (error) {
|
||||
debugResultEl.innerHTML = `<div class="err-text">${error.message}</div>`;
|
||||
}
|
||||
});
|
||||
|
||||
loadMe().then(loadOverview);
|
||||
</script>
|
||||
</body>
|
||||
</html>"#;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{build_body_preview, build_debug_base_url, normalize_debug_path, trim_preview};
|
||||
use super::{
|
||||
build_body_preview, build_debug_base_url, build_spacetime_schema_url, normalize_debug_path,
|
||||
parse_spacetime_sql_count_response, trim_preview,
|
||||
};
|
||||
use axum::{http::StatusCode, response::IntoResponse};
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn normalize_debug_path_rejects_absolute_url() {
|
||||
@@ -1161,6 +712,91 @@ mod tests {
|
||||
assert_eq!(trim_preview(&text).chars().count(), 4000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_spacetime_schema_url_includes_required_version_query() {
|
||||
let url = build_spacetime_schema_url("http://127.0.0.1:3101", "xushi-p4wfr");
|
||||
|
||||
assert_eq!(
|
||||
url,
|
||||
"http://127.0.0.1:3101/v1/database/xushi-p4wfr/schema?version=9"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_spacetime_sql_count_response_accepts_statement_array_rows() {
|
||||
let payload = json!([
|
||||
{
|
||||
"schema": {
|
||||
"elements": [
|
||||
{
|
||||
"name": {
|
||||
"some": "row_count"
|
||||
},
|
||||
"algebraic_type": {
|
||||
"U64": []
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"rows": [[7]],
|
||||
"total_duration_micros": 116,
|
||||
"stats": {
|
||||
"rows_inserted": 0,
|
||||
"rows_deleted": 0,
|
||||
"rows_updated": 0
|
||||
}
|
||||
}
|
||||
]);
|
||||
|
||||
let count =
|
||||
parse_spacetime_sql_count_response(payload).expect("statement array should parse");
|
||||
|
||||
assert_eq!(count, 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_spacetime_sql_count_response_uses_schema_column_index() {
|
||||
let payload = json!([
|
||||
{
|
||||
"schema": {
|
||||
"elements": [
|
||||
{
|
||||
"name": {
|
||||
"some": "table_name"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": {
|
||||
"some": "row_count"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"rows": [["runtime_setting", "12"]]
|
||||
}
|
||||
]);
|
||||
|
||||
let count =
|
||||
parse_spacetime_sql_count_response(payload).expect("schema column index should parse");
|
||||
|
||||
assert_eq!(count, 12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_spacetime_sql_count_response_keeps_object_row_compatibility() {
|
||||
let payload = json!({
|
||||
"rows": [
|
||||
{
|
||||
"row_count": "3"
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
let count = parse_spacetime_sql_count_response(payload).expect("object row should parse");
|
||||
|
||||
assert_eq!(count, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_body_preview_handles_utf8() {
|
||||
let preview = build_body_preview("后台测试".as_bytes());
|
||||
|
||||
@@ -13,10 +13,7 @@ use tower_http::{
|
||||
use tracing::{Level, Span, error, info, info_span, warn};
|
||||
|
||||
use crate::{
|
||||
admin::{
|
||||
admin_console_page, admin_debug_http, admin_login, admin_me, admin_overview,
|
||||
require_admin_auth,
|
||||
},
|
||||
admin::{admin_debug_http, admin_login, admin_me, admin_overview, require_admin_auth},
|
||||
ai_tasks::{
|
||||
append_ai_text_chunk, attach_ai_result_reference, cancel_ai_task, complete_ai_stage,
|
||||
complete_ai_task, create_ai_task, fail_ai_task, start_ai_task, start_ai_task_stage,
|
||||
@@ -131,7 +128,6 @@ pub fn build_router(state: AppState) -> Router {
|
||||
let slow_request_threshold_ms = state.config.slow_request_threshold_ms;
|
||||
|
||||
Router::new()
|
||||
.route("/admin", get(admin_console_page))
|
||||
.route("/admin/api/login", post(admin_login))
|
||||
.route(
|
||||
"/admin/api/me",
|
||||
@@ -3166,6 +3162,23 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn admin_page_route_is_not_mounted() {
|
||||
let app = build_router(AppState::new(AppConfig::default()).expect("state should build"));
|
||||
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/admin")
|
||||
.body(Body::empty())
|
||||
.expect("admin page request should build"),
|
||||
)
|
||||
.await
|
||||
.expect("admin page request should succeed");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn admin_login_returns_token_when_configured() {
|
||||
let mut config = AppConfig::default();
|
||||
|
||||
Reference in New Issue
Block a user