#!/bin/sh set -e # Ensure data directory exists mkdir -p /app/data # Handle custom CA certificates if [ -d "/app/certs" ] && [ "$(ls -A /app/certs/*.crt 2>/dev/null)" ]; then echo "Custom CA certificates found, configuring Node.js to use them..." # Combine all CA certificates into a bundle for Node.js CA_BUNDLE="/app/certs/ca-bundle.crt" > "$CA_BUNDLE" for cert in /app/certs/*.crt; do if [ -f "$cert" ]; then echo "Adding certificate: $(basename "$cert")" cat "$cert" >> "$CA_BUNDLE" echo "" >> "$CA_BUNDLE" # Add newline between certificates fi done # Set Node.js to use the custom CA bundle export NODE_EXTRA_CA_CERTS="$CA_BUNDLE" echo "NODE_EXTRA_CA_CERTS set to: $NODE_EXTRA_CA_CERTS" # For Bun compatibility, also set the CA bundle in system location if writable if [ -f "/etc/ssl/certs/ca-certificates.crt" ] && [ -w "/etc/ssl/certs/" ]; then echo "Appending custom certificates to system CA bundle..." cat "$CA_BUNDLE" >> /etc/ssl/certs/ca-certificates.crt fi else echo "No custom CA certificates found in /app/certs" fi # Check if system CA bundle is mounted and use it if [ -f "/etc/ssl/certs/ca-certificates.crt" ] && [ ! -L "/etc/ssl/certs/ca-certificates.crt" ]; then # Check if it's a mounted file (not the default symlink) if [ "$(stat -c '%d' /etc/ssl/certs/ca-certificates.crt 2>/dev/null)" != "$(stat -c '%d' / 2>/dev/null)" ] || \ [ "$(stat -f '%d' /etc/ssl/certs/ca-certificates.crt 2>/dev/null)" != "$(stat -f '%d' / 2>/dev/null)" ]; then echo "System CA bundle mounted, configuring Node.js to use it..." export NODE_EXTRA_CA_CERTS="/etc/ssl/certs/ca-certificates.crt" echo "NODE_EXTRA_CA_CERTS set to: $NODE_EXTRA_CA_CERTS" fi fi # Optional: If GITEA_SKIP_TLS_VERIFY is set, configure accordingly if [ "$GITEA_SKIP_TLS_VERIFY" = "true" ]; then echo "Warning: GITEA_SKIP_TLS_VERIFY is set to true. This is insecure!" export NODE_TLS_REJECT_UNAUTHORIZED=0 fi # Generate a secure JWT secret if one isn't provided or is using the default value JWT_SECRET_FILE="/app/data/.jwt_secret" if [ "$JWT_SECRET" = "your-secret-key-change-this-in-production" ] || [ -z "$JWT_SECRET" ]; then # Check if we have a previously generated secret if [ -f "$JWT_SECRET_FILE" ]; then echo "Using previously generated JWT secret" export JWT_SECRET=$(cat "$JWT_SECRET_FILE") else echo "Generating a secure random JWT secret" # Try to generate a secure random string using OpenSSL if command -v openssl >/dev/null 2>&1; then GENERATED_SECRET=$(openssl rand -hex 32) else # Fallback to using /dev/urandom if openssl is not available echo "OpenSSL not found, using fallback method for random generation" GENERATED_SECRET=$(head -c 32 /dev/urandom | sha256sum | cut -d' ' -f1) fi export JWT_SECRET="$GENERATED_SECRET" # Save the secret to a file for persistence across container restarts echo "$GENERATED_SECRET" > "$JWT_SECRET_FILE" chmod 600 "$JWT_SECRET_FILE" fi echo "JWT_SECRET has been set to a secure random value" fi # Skip dependency installation entirely for pre-built images # Dependencies are already installed during the Docker build process # Initialize the database if it doesn't exist if [ ! -f "/app/data/gitea-mirror.db" ]; then echo "Initializing database..." if [ -f "dist/scripts/init-db.js" ]; then bun dist/scripts/init-db.js elif [ -f "dist/scripts/manage-db.js" ]; then bun dist/scripts/manage-db.js init elif [ -f "scripts/manage-db.ts" ]; then bun scripts/manage-db.ts init else echo "Warning: Could not find database initialization scripts in dist/scripts." echo "Creating and initializing database manually..." # Create the database file touch /app/data/gitea-mirror.db # Initialize the database with required tables sqlite3 /app/data/gitea-mirror.db <