迁移后端认证与拆分 Spacetime 客户端

This commit is contained in:
2026-04-24 14:10:11 +08:00
parent ef53028be5
commit 4f369617c7
55 changed files with 9206 additions and 343 deletions

View File

@@ -0,0 +1,122 @@
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'EOF'
用法:
npm run dev:rust:logs
npm run dev:rust:logs -- --follow
./scripts/spacetime-logs-local.sh --lines 500 --output logs/spacetime/local.log
说明:
1. 从本地 SpacetimeDB 通过 spacetime logs 提取模块日志到本地文件。
2. 默认读取 spacetime.local.json 的 database 字段,默认 server 为 http://127.0.0.1:3101。
3. 默认输出到 logs/spacetime/<database>-<timestamp>.log--follow 会持续追加并同步写到终端。
EOF
}
require_command() {
local command_name="$1"
if ! command -v "${command_name}" >/dev/null 2>&1; then
echo "[stdb:logs] 缺少命令: ${command_name}" >&2
exit 1
fi
}
read_local_spacetime_database() {
local config_path="${REPO_ROOT}/spacetime.local.json"
if [[ ! -f "${config_path}" ]]; then
return
fi
node -e '
const fs = require("fs");
const path = process.argv[1];
try {
const value = JSON.parse(fs.readFileSync(path, "utf8")).database;
if (typeof value === "string" && value.trim()) {
process.stdout.write(value.trim());
}
} catch (error) {
process.stderr.write(`[stdb:logs] ignore invalid spacetime.local.json: ${error.message}\n`);
}
' "${config_path}"
}
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)"
DATABASE=""
SPACETIME_SERVER="http://127.0.0.1:3101"
LINES="200"
OUTPUT=""
FOLLOW=0
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
--database)
DATABASE="${2:?缺少 --database 的值}"
shift 2
;;
--server)
SPACETIME_SERVER="${2:?缺少 --server 的值}"
shift 2
;;
--lines|-n)
LINES="${2:?缺少 --lines 的值}"
shift 2
;;
--output|-o)
OUTPUT="${2:?缺少 --output 的值}"
shift 2
;;
--follow|-f)
FOLLOW=1
shift
;;
*)
echo "[stdb:logs] 未知参数: $1" >&2
usage >&2
exit 1
;;
esac
done
require_command node
require_command spacetime
if [[ -z "${DATABASE//[[:space:]]/}" ]]; then
DATABASE="$(read_local_spacetime_database)"
fi
if [[ -z "${DATABASE//[[:space:]]/}" ]]; then
DATABASE="genarrative-dev"
fi
if [[ -z "${OUTPUT//[[:space:]]/}" ]]; then
LOG_DIR="${REPO_ROOT}/logs/spacetime"
mkdir -p "${LOG_DIR}"
TIMESTAMP="$(date +%Y%m%d-%H%M%S)"
OUTPUT="${LOG_DIR}/${DATABASE}-${TIMESTAMP}.log"
else
mkdir -p "$(dirname -- "${OUTPUT}")"
fi
ARGS=(logs "${DATABASE}" --server "${SPACETIME_SERVER}" -n "${LINES}")
if [[ "${FOLLOW}" -eq 1 ]]; then
ARGS+=(-f)
fi
echo "[stdb:logs] database: ${DATABASE}"
echo "[stdb:logs] server: ${SPACETIME_SERVER}"
echo "[stdb:logs] output: ${OUTPUT}"
spacetime "${ARGS[@]}" | tee -a "${OUTPUT}"