Files
ChuSQL/scripts/csql-web.sh
T

70 lines
2.4 KiB
Bash
Raw Normal View History

2026-10-01 20:48:38 +08:00
#!/bin/sh
set -eu
# csql-web:先起独占数据目录的 server,再起 Web 服务,配置读 settings.toml。
2026-10-01 20:48:38 +08:00
home_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
bin="$home_dir/bin"
# 和在 Windows 的 csql-web.ps1(Start-Process -WorkingDirectory)保持一致:
# 先切到安装目录,配置里写相对的 log_files 才有确定的解析基准。
2026-10-01 20:48:38 +08:00
cd "$home_dir"
config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/ChuSQL"
config="$config_dir/settings.toml"
2026-10-01 20:48:38 +08:00
[ -f "$config" ] || { echo "!! config not found: $config" >&2; exit 1; }
# 从配置里取某分区的一个值。
section_setting() {
awk -v section_key="$1" -v key="$2" -v def="$3" '
2026-10-01 20:48:38 +08:00
/^[[:space:]]*\[/ { section = $0; gsub(/[][[:space:]]/, "", section); next }
section != section_key { next }
2026-10-01 20:48:38 +08:00
{
line = $0
sub(/#.*/, "", line)
if (line ~ "^[[:space:]]*" key "[[:space:]]*=") {
sub(/^[^=]*=[[:space:]]*/, "", line)
gsub(/^[[:space:]]+|[[:space:]]+$/, "", line)
gsub(/^"|"$/, "", line)
print line
found = 1
exit
}
}
END { if (!found) print def }
' "$config"
}
server_exe="$bin/chusql-server"
2026-10-01 20:48:38 +08:00
web_exe="$bin/chusql-web"
[ -x "$server_exe" ] || { echo "!! server binary not found in $bin" >&2; exit 1; }
2026-10-01 20:48:38 +08:00
[ -x "$web_exe" ] || { echo "!! web binary not found in $bin" >&2; exit 1; }
# 监听地址与端口取 [web] 段,日志目录取 [storage] log_files(相对路径以安装目录为基准)。
host=$(section_setting web listen_host 127.0.0.1)
port=$(section_setting web port 7778)
log_setting=$(section_setting storage log_files ./logs)
case "$log_setting" in
/*) log_dir="$log_setting" ;;
*) log_dir="$home_dir/$log_setting" ;;
esac
2026-10-01 20:48:38 +08:00
mkdir -p "$log_dir"
# server 先起来:它进程内装入存储库、独占数据目录,Web 的存储请求都经它转发
"$server_exe" --config "$config" >>"$log_dir/server.log" 2>>"$log_dir/server.err.log" &
server_pid=$!
"$web_exe" --config "$config" >>"$log_dir/web.log" 2>>"$log_dir/web.err.log" &
2026-10-01 20:48:38 +08:00
web_pid=$!
# 退出时杀掉 web 与 server 进程。
2026-10-01 20:48:38 +08:00
cleanup() {
kill "$web_pid" "$server_pid" 2>/dev/null || true
wait "$web_pid" "$server_pid" 2>/dev/null || true
2026-10-01 20:48:38 +08:00
echo stopped.
}
trap cleanup INT TERM EXIT
echo "starting web ..."
echo "ready: http://$host:$port/"
echo "logs: $log_dir"
wait "$web_pid"