修改构建脚本
This commit is contained in:
@@ -10,6 +10,9 @@ OUTPUT_PATH="${ROOT_DIR}/api"
|
||||
TOOLS_DIR="${ROOT_DIR}/.build-tools"
|
||||
CACHE_DIR="${ROOT_DIR}/.build-cache"
|
||||
CLEAN_GO="false"
|
||||
INSTALL_SERVICE="false"
|
||||
SERVICE_NAME="singbox-gopanel"
|
||||
RUN_USER="${SUDO_USER:-${USER:-root}}"
|
||||
|
||||
usage() {
|
||||
cat <<EOF
|
||||
@@ -21,11 +24,15 @@ Options:
|
||||
--tools-dir <dir> Toolchain directory, default: ${TOOLS_DIR}
|
||||
--cache-dir <dir> Go cache directory, default: ${CACHE_DIR}
|
||||
--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
|
||||
|
||||
Examples:
|
||||
bash scripts/build_install.sh
|
||||
bash scripts/build_install.sh --output ./package/api
|
||||
sudo bash scripts/build_install.sh --install-service
|
||||
EOF
|
||||
}
|
||||
|
||||
@@ -52,6 +59,18 @@ parse_args() {
|
||||
CLEAN_GO="true"
|
||||
shift
|
||||
;;
|
||||
--install-service)
|
||||
INSTALL_SERVICE="true"
|
||||
shift
|
||||
;;
|
||||
--service-name)
|
||||
SERVICE_NAME="$2"
|
||||
shift 2
|
||||
;;
|
||||
--run-user)
|
||||
RUN_USER="$2"
|
||||
shift 2
|
||||
;;
|
||||
-h|--help)
|
||||
usage
|
||||
exit 0
|
||||
@@ -154,6 +173,49 @@ build_binary() {
|
||||
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() {
|
||||
cat <<EOF
|
||||
|
||||
@@ -163,6 +225,10 @@ Go version: ${GO_VERSION}
|
||||
Toolchain: ${GO_ROOT}
|
||||
Output: ${OUTPUT_PATH}
|
||||
Cache dir: ${CACHE_DIR}
|
||||
|
||||
To use this Go environment in your current shell:
|
||||
export PATH="${GO_ROOT}/bin:\$PATH"
|
||||
export GOROOT="${GO_ROOT}"
|
||||
EOF
|
||||
}
|
||||
|
||||
@@ -172,6 +238,7 @@ main() {
|
||||
detect_platform
|
||||
download_go
|
||||
build_binary
|
||||
install_systemd_service
|
||||
print_summary
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user