#!/usr/bin/env bash SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" AZURITE_DIR="${AZURITE_DIR:-$SCRIPT_DIR}" if [[ -z "$AZURITE_ACCOUNTS" && -f "$AZURITE_DIR/accounts.env" ]]; then set -a source "$AZURITE_DIR/accounts.env" set +a fi 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" 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. 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'" exit 1 fi if ! command -v caddy &> /dev/null; then echo "ERROR: Caddy is not installed. Please install it from https://caddyserver.com/docs/install or with 'brew install caddy' on macOS." exit 1 fi OAUTH_ARGS=("--oauth" "basic") while [[ $# -gt 0 ]]; do case "$1" in --no-oauth) OAUTH_ARGS=() shift ;; *) echo "Unknown argument: $1" exit 1 ;; esac done 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 # Generate a Caddyfile configuration based on the account name and storage directory. 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. trap "echo 'Stopping Caddy server...'; caddy stop" EXIT INT TERM HUP KILL STOP # Start Azurite echo "Starting Azurite..." azurite \ --disableTelemetry \ --location "$AZURITE_DIR/storage" \ --blobHost 127.0.0.1 --queueHost 127.0.0.1 --tableHost 127.0.0.1 \ --key "$AZURITE_DIR/storage/${ACCOUNT_NAME}_key.pem" --cert "$AZURITE_DIR/storage/${ACCOUNT_NAME}_cert.pem" \ "${OAUTH_ARGS[@]}"