#!/usr/bin/env bash set -euo pipefail # Standalone launcher for running OpenIDEA in browser web server mode. # Designed to work directly from a repository checkout or inside a container / # remote workspace environment with zero required arguments. # # Environment variables (all optional): # OPENIDEA_HOST Bind address (default: 127.0.0.1; 0.0.0.0 in a container) # OPENIDEA_PORT Listen port (default: 8080) # OPENIDEA_WORKSPACE_DIR Workspace folder to open on startup (default: the # first argument if it is a folder, else /workspace if # it exists, else the current directory) # OPENIDEA_SERVER_HOME Persistent config/data directory (defaults to # ~/.openidea, outside the workspace so caches and # tools are never indexed or watched as project files) # OPENIDEA_WEB_DIR Built frontend directory (default: /dist) # OPENIDEA_SERVER_BIN Path to openidea-server binary (auto-detected) # OPENIDEA_BASE_PATH Optional reverse-proxy URL prefix # OPENIDEA_WEB_TOKEN Access token (default: auto-generated by openidea-server) # OPENIDEA_WEB_NO_AUTH Set to 1 to disable token auth behind an authenticating proxy SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)" HOST="${OPENIDEA_HOST:-127.0.0.1}" PORT="${OPENIDEA_PORT:-8080}" # Limit glibc malloc arenas so bursty thread pools do not retain excess RSS in containers. export MALLOC_ARENA_MAX="${MALLOC_ARENA_MAX:-2}" # 1. Resolve workspace directory. WORKSPACE_DIR="${OPENIDEA_WORKSPACE_DIR:-${WORKSPACE_DIR:-}}" if [[ -z "${WORKSPACE_DIR}" && $# -gt 0 && "${1}" != -* && -d "${1}" ]]; then WORKSPACE_DIR="${1}" shift fi if [[ -z "${WORKSPACE_DIR}" ]]; then if [[ -d "/workspace" ]]; then WORKSPACE_DIR="/workspace" else WORKSPACE_DIR="${PWD}" fi fi # 2. Resolve persistent config/data directory (keep outside WORKSPACE_DIR). if [[ -z "${OPENIDEA_SERVER_HOME:-}" ]]; then export OPENIDEA_SERVER_HOME="${HOME:-/tmp}/.openidea" fi mkdir -p "${OPENIDEA_SERVER_HOME}/config" "${OPENIDEA_SERVER_HOME}/data" # 3. Resolve openidea-server binary. BIN="${OPENIDEA_SERVER_BIN:-}" if [[ -z "${BIN}" ]]; then if command -v openidea-server >/dev/null 2>&1; then BIN="$(command -v openidea-server)" elif [[ -x "${REPO_ROOT}/target/release/openidea-server" ]]; then BIN="${REPO_ROOT}/target/release/openidea-server" elif [[ -x "${REPO_ROOT}/target/debug/openidea-server" ]]; then BIN="${REPO_ROOT}/target/debug/openidea-server" else echo "error: openidea-server binary not found. Build it with:" >&2 echo " cargo build --release -p ide-host --bin openidea-server" >&2 exit 1 fi fi # 4. Resolve built frontend assets directory. WEB_DIR="${OPENIDEA_WEB_DIR:-}" if [[ -z "${WEB_DIR}" ]]; then for candidate in "${REPO_ROOT}/dist" "${PWD}/dist"; do if [[ -f "${candidate}/index.html" ]]; then WEB_DIR="${candidate}" break fi done fi if [[ -z "${WEB_DIR}" || ! -f "${WEB_DIR}/index.html" ]]; then echo "error: built frontend (index.html) not found. Build it with:" >&2 echo " pnpm build" >&2 exit 1 fi ARGS=( --web --host "${HOST}" --port "${PORT}" --web-dir "${WEB_DIR}" --home "${OPENIDEA_SERVER_HOME}" ) if [[ -n "${WORKSPACE_DIR}" ]]; then ARGS+=(--project "${WORKSPACE_DIR}") fi if [[ -n "${OPENIDEA_BASE_PATH:-}" ]]; then ARGS+=(--base-path "${OPENIDEA_BASE_PATH}") fi if [[ -n "${OPENIDEA_WEB_TOKEN:-}" ]]; then ARGS+=(--token "${OPENIDEA_WEB_TOKEN}") elif [[ "${OPENIDEA_WEB_NO_AUTH:-}" == "1" || "${OPENIDEA_WEB_NO_AUTH:-}" == "true" ]]; then ARGS+=(--no-auth) fi echo "Starting OpenIDEA Web Server on ${HOST}:${PORT} (workspace: ${WORKSPACE_DIR}, home: ${OPENIDEA_SERVER_HOME})" >&2 exec "${BIN}" "${ARGS[@]}" "$@"