diff --git a/run-server.sh b/run-server.sh index 63c5cbf..92c10b9 100755 --- a/run-server.sh +++ b/run-server.sh @@ -1,20 +1,17 @@ #!/usr/bin/env bash SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -# Include certificate generation functions. -. "$SCRIPT_DIR/cert-functions.sh" +AZURITE_DIR="${AZURITE_DIR:-$SCRIPT_DIR}" -AZURITE_DIR="$SCRIPT_DIR/storage" - -if [[ ! -d "$AZURITE_DIR" ]]; then - echo "No accounts found" - exit 0 +if [[ -z "$AZURITE_ACCOUNTS" && -f "$AZURITE_DIR/accounts.env" ]]; then + set -a + source "$AZURITE_DIR/accounts.env" + set +a fi -if [[ -z "$AZURITE_ACCOUNTS" && -f "$SCRIPT_DIR/accounts.env" ]]; then - set -a - . "$SCRIPT_DIR/accounts.env" - set +a +if [[ -z "$AZURITE_ACCOUNTS" ]]; then + echo "[ERROR] AZURITE_ACCOUNTS variable is not set." >&2 + exit 1 fi # Look up the account name from the AZURITE_ACCOUNTS variable, which is in the format "accountName:accountKey1:accountKey2;accountName2:accountKey1:accountKey2" @@ -22,13 +19,13 @@ ACCOUNT_NAME=$(echo "$AZURITE_ACCOUNTS" | cut -f 1 -d ';' | cut -f 1 -d ':') # Ensure /etc/hosts contains an entry the Azure endpoint names, # so Caddy can route requests to the correct service based on the hostname. -if ! grep -qE "${ACCOUNT_NAME}\.blob\.core\.windows\.net" /etc/hosts || - ! grep -qE "${ACCOUNT_NAME}\.queue\.core\.windows\.net" /etc/hosts || - ! grep -qE "${ACCOUNT_NAME}\.table\.core\.windows\.net" /etc/hosts; then - echo "ERROR: /etc/hosts does not contain entries for the Azure endpoints. Please ensure the following line is present in /etc/hosts:" - echo -e "\n127.0.0.1\t${ACCOUNT_NAME}.blob.core.windows.net ${ACCOUNT_NAME}.queue.core.windows.net ${ACCOUNT_NAME}.table.core.windows.net" - exit 1 -fi +ALTNAMES=() +for name in blob queue table; do + if ! grep -qE "${ACCOUNT_NAME}.${name}.core.windows.net" /etc/hosts; then + echo "ERROR: /etc/hosts does not contain an entry for ${ACCOUNT_NAME}.${name}.core.windows.net mapping to 127.0.0.1." + exit 1 + fi +done if ! command -v azurite &> /dev/null; then echo "ERROR: Azurite is not installed. Please install it with 'npm install -g azurite'" @@ -40,13 +37,12 @@ if ! command -v caddy &> /dev/null; then exit 1 fi -OAUTH_ARGS=() +OAUTH_ARGS=("--oauth" "basic") while [[ $# -gt 0 ]]; do case "$1" in - --oauth|-o) - OAUTH_ARGS=("--oauth" "basic") - # Ensure Caddy is disabled when using OAuth, as Azurite does not support OAuth behind a reverse proxy. + --no-oauth) + OAUTH_ARGS=() shift ;; *) @@ -56,21 +52,16 @@ while [[ $# -gt 0 ]]; do esac done -# Ensure certificates are generated before starting Azurite or Caddy. -if ! make_ca "$AZURITE_DIR"; then - echo "Error: Failed to create CA certificate and key." >&2 +if [[ ! -f "$AZURITE_DIR/storage/ca_cert.pem" || + ! -f "$AZURITE_DIR/storage/ca_key.pem" || + ! -f "$AZURITE_DIR/storage/${ACCOUNT_NAME}_cert.pem" || + ! -f "$AZURITE_DIR/storage/${ACCOUNT_NAME}_key.pem" ]]; then + echo "ERROR: SSL certificate or key files not found in $AZURITE_DIR/storage. Please run 'make-cert.sh' to create the necessary certificates and keys." exit 1 fi -if ! make_server_cert "$ACCOUNT_NAME" "$AZURITE_DIR"; then - echo "Error: Failed to create server certificate and key." >&2 - exit 1 -fi - -CERT_ARGS=() - # Generate a Caddyfile configuration based on the account name and storage directory. -sed -E "s/__ACCOUNT_NAME__/${ACCOUNT_NAME}/g; s|__AZURITE_DIR__|${AZURITE_DIR}|g" "$SCRIPT_DIR/Caddyfile.example" > "$AZURITE_DIR/Caddyfile" +sed -E "s|__ACCOUNT_NAME__|${ACCOUNT_NAME}|g; s|__AZURITE_STORAGE__|${AZURITE_DIR}/storage|g" "$SCRIPT_DIR/Caddyfile.template" > "$AZURITE_DIR/Caddyfile" echo "Starting Caddy server..." caddy start --config "$AZURITE_DIR/Caddyfile" # Use start not run, start does not block the shell process. @@ -80,7 +71,7 @@ trap "echo 'Stopping Caddy server...'; caddy stop" EXIT INT TERM HUP KILL STOP echo "Starting Azurite..." azurite \ --disableTelemetry \ - --location "$AZURITE_DIR" \ + --location "$AZURITE_DIR/storage" \ --blobHost 127.0.0.1 --queueHost 127.0.0.1 --tableHost 127.0.0.1 \ - --key "$AZURITE_DIR/${ACCOUNT_NAME}_key.pem" --cert "$AZURITE_DIR/${ACCOUNT_NAME}_cert.pem" \ + --key "$AZURITE_DIR/storage/${ACCOUNT_NAME}_key.pem" --cert "$AZURITE_DIR/storage/${ACCOUNT_NAME}_cert.pem" \ "${OAUTH_ARGS[@]}"