修改构建脚本
Some checks failed
build / build (api, amd64, linux) (push) Has been cancelled
build / build (api, arm64, linux) (push) Has been cancelled
build / build (api.exe, amd64, windows) (push) Has been cancelled

This commit is contained in:
CN-JS-HuiBai
2026-04-17 22:37:51 +08:00
parent e8e7664aff
commit a703315d00

View File

@@ -10,6 +10,9 @@ OUTPUT_PATH="${ROOT_DIR}/api"
TOOLS_DIR="${ROOT_DIR}/.build-tools" TOOLS_DIR="${ROOT_DIR}/.build-tools"
CACHE_DIR="${ROOT_DIR}/.build-cache" CACHE_DIR="${ROOT_DIR}/.build-cache"
CLEAN_GO="false" CLEAN_GO="false"
INSTALL_SERVICE="false"
SERVICE_NAME="singbox-gopanel"
RUN_USER="${SUDO_USER:-${USER:-root}}"
usage() { usage() {
cat <<EOF cat <<EOF
@@ -21,11 +24,15 @@ Options:
--tools-dir <dir> Toolchain directory, default: ${TOOLS_DIR} --tools-dir <dir> Toolchain directory, default: ${TOOLS_DIR}
--cache-dir <dir> Go cache directory, default: ${CACHE_DIR} --cache-dir <dir> Go cache directory, default: ${CACHE_DIR}
--clean-go Re-download the Go toolchain even if it exists --clean-go Re-download the Go toolchain even if it exists
--install-service Install and start the systemd service
--service-name <name> Systemd service name, default: ${SERVICE_NAME}
--run-user <user> Runtime user, default: ${RUN_USER}
-h, --help Show this help -h, --help Show this help
Examples: Examples:
bash scripts/build_install.sh bash scripts/build_install.sh
bash scripts/build_install.sh --output ./package/api bash scripts/build_install.sh --output ./package/api
sudo bash scripts/build_install.sh --install-service
EOF EOF
} }
@@ -52,6 +59,18 @@ parse_args() {
CLEAN_GO="true" CLEAN_GO="true"
shift shift
;; ;;
--install-service)
INSTALL_SERVICE="true"
shift
;;
--service-name)
SERVICE_NAME="$2"
shift 2
;;
--run-user)
RUN_USER="$2"
shift 2
;;
-h|--help) -h|--help)
usage usage
exit 0 exit 0
@@ -154,6 +173,49 @@ build_binary() {
chmod +x "${OUTPUT_PATH}" || true chmod +x "${OUTPUT_PATH}" || true
} }
install_systemd_service() {
if [[ "${INSTALL_SERVICE}" != "true" ]]; then
return
fi
if [[ "${EUID}" -ne 0 ]]; then
echo "error: systemd service installation requires root/sudo" >&2
exit 1
fi
if ! command -v systemctl >/dev/null 2>&1; then
echo "systemctl not found, skipping service installation"
return
fi
echo "installing systemd service: ${SERVICE_NAME}..."
cat >"/etc/systemd/system/${SERVICE_NAME}.service" <<EOF
[Unit]
Description=SingBox GoPanel API
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=${RUN_USER}
WorkingDirectory=${ROOT_DIR}
EnvironmentFile=-${ROOT_DIR}/.env
ExecStart=${OUTPUT_PATH}
Restart=always
RestartSec=5
LimitNOFILE=1048576
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable "${SERVICE_NAME}"
systemctl restart "${SERVICE_NAME}"
echo "service ${SERVICE_NAME} installed and started."
}
print_summary() { print_summary() {
cat <<EOF cat <<EOF
@@ -163,6 +225,10 @@ Go version: ${GO_VERSION}
Toolchain: ${GO_ROOT} Toolchain: ${GO_ROOT}
Output: ${OUTPUT_PATH} Output: ${OUTPUT_PATH}
Cache dir: ${CACHE_DIR} Cache dir: ${CACHE_DIR}
To use this Go environment in your current shell:
export PATH="${GO_ROOT}/bin:\$PATH"
export GOROOT="${GO_ROOT}"
EOF EOF
} }
@@ -172,6 +238,7 @@ main() {
detect_platform detect_platform
download_go download_go
build_binary build_binary
install_systemd_service
print_summary print_summary
} }