Commit 71db4afa authored by Andreas Düren's avatar Andreas Düren
Browse files

Fixed empty HTML issue by copying and modifying the original HTML files

parent bdcf9615
Loading
Loading
Loading
Loading
+113 −89
Original line number Diff line number Diff line
@@ -1194,14 +1194,12 @@ done
# Set up Caddy web server
echo "==> Setting up Caddy web server"

# First inject the config.js script tags into all HTML files
echo "==> Injecting config.js into web application HTML files"

# Create writable data directories for web assets
mkdir -p /app/data/web/photos /app/data/web/accounts /app/data/web/auth /app/data/web/cast
# Create a simpler approach for injecting configuration
echo "==> Creating a static HTML file with config scripts already included"

# Create runtime-config.js files in writable locations
echo "==> Creating runtime-config.js in writable location"
mkdir -p /app/data/web
cat > /app/data/web/runtime-config.js <<EOT
// Runtime configuration for Ente
window.ENTE_CONFIG = {
@@ -1220,15 +1218,50 @@ console.log('API_URL:', window.ENTE_CONFIG.API_URL);
console.log('PUBLIC_ALBUMS_URL:', window.ENTE_CONFIG.PUBLIC_ALBUMS_URL);
EOT

# Copy the runtime-config.js to each app directory
for app_dir in /app/data/web/photos /app/data/web/accounts /app/data/web/auth /app/data/web/cast; do
    cp /app/data/web/runtime-config.js "$app_dir/"
done
# Ensure runtime-config.js is readable
chmod 644 /app/data/web/runtime-config.js

# Create the static HTML files with scripts pre-injected
for app_dir in photos accounts auth cast; do
    # Create directory for our modified files
    mkdir -p /app/data/web/$app_dir
    
# Ensure all files are readable
chmod -R 644 /app/data/web/runtime-config.js
    # If the original index.html exists, copy and modify it
    if [ -f "/app/web/$app_dir/index.html" ]; then
        echo "==> Copying and modifying index.html for $app_dir app"
        cp "/app/web/$app_dir/index.html" "/app/data/web/$app_dir/index.html"
        
# Update the Caddy configuration to serve config.js directly
        # Fix any potential issues with the head tag
        if ! grep -q "<head>" "/app/data/web/$app_dir/index.html"; then
            echo "==> Warning: No head tag found in $app_dir/index.html, adding one"
            sed -i 's/<html>/<html>\n<head><\/head>/' "/app/data/web/$app_dir/index.html"
        fi
        
        # Insert config scripts right after the opening head tag, unescaped
        sed -i 's/<head>/<head>\n    <script src="\/config.js" type="text\/javascript"><\/script>\n    <script src="\/runtime-config.js" type="text\/javascript"><\/script>/' "/app/data/web/$app_dir/index.html"
    else
        # Create a minimal HTML file with the scripts included but pointing to the original app
        echo "==> Creating minimal pre-configured index.html for $app_dir app with redirect"
        cat > "/app/data/web/$app_dir/index.html" <<HTML
<!DOCTYPE html>
<html>
<head>
    <script src="/config.js" type="text/javascript"></script>
    <script src="/runtime-config.js" type="text/javascript"></script>
    <meta http-equiv="refresh" content="0;url=/app/web/$app_dir/index.html">
    <title>Ente $app_dir</title>
</head>
<body>
    <h1>Ente $app_dir</h1>
    <p>Loading...</p>
    <p>If this page doesn't redirect automatically, <a href="/app/web/$app_dir/index.html">click here</a>.</p>
</body>
</html>
HTML
    fi
done

# Modify the Caddyfile to serve our modified HTML files
cat > /app/data/caddy/Caddyfile <<EOT
# Global settings
{
@@ -1246,7 +1279,7 @@ cat > /app/data/caddy/Caddyfile <<EOT
        output file /app/data/logs/caddy.log
    }
    
    # Configuration scripts - This must come before the root handler
    # Configuration scripts - directly served
    handle /config.js {
        header Content-Type application/javascript
        respond "
@@ -1272,50 +1305,29 @@ cat > /app/data/caddy/Caddyfile <<EOT
        file_server
    }
    
    # Create special routes for index.html that add script tags
    handle /index-with-config.html {
        @is_index_html {
            path /index-with-config.html
        }
        header @is_index_html Content-Type text/html
        respond @is_index_html 200 {
            body "<!DOCTYPE html>
<html>
<head>
    <script src=\"/config.js\" type=\"text\/javascript\"></script>
    <script src=\"/runtime-config.js\" type=\"text\/javascript\"></script>
    <!-- Placeholder for original head content -->
    {http.vars.head_content}
</head>
<body>
    {http.vars.body_content}
</body>
</html>"
        }
    }

    # Main photos app - rewrite to use the index-with-config
    # Root path serves the photos app
    handle / {
        root * /app/web/photos
        
        # Rewrite index.html to include our config
        @isroot {
            path /
        # Special handling for index.html
        @is_index path /
        handle @is_index {
            root * /app/data/web/photos
            try_files {path} /index.html
            file_server
        }
        
        # For other files just serve them directly
        # Serve other static files from the original location
        @not_index {
            not path /
            not path /api/*
            not path /public/*
            not path /accounts/*
            not path /auth/*
            not path /cast/*
        }
        handle @not_index {
            root * /app/web/photos
            try_files {path} /index.html
            file_server
        
        # Special handling for index.html to inject scripts
        handle @isroot {
            rewrite * /index-with-config.html
            vars {
                head_content "<!-- Head content from photos app -->"
                body_content "<!-- Body content from photos app -->"
            }
            reverse_proxy http://localhost:$CADDY_PORT {
                header_up Host {host}
            }
        }
    }
    
@@ -1336,71 +1348,83 @@ cat > /app/data/caddy/Caddyfile <<EOT

    # Accounts app
    handle /accounts {
        header Content-Type text/html
        respond 200 {file}/app/data/web/accounts/index.html
        root * /app/data/web/accounts
        try_files {path} /index.html
        file_server
    }
    
    handle /accounts/* {
        @is_index {
            path /accounts/
            path /accounts/index.html
        @is_index path /accounts/ /accounts/index.html
        handle @is_index {
            root * /app/data/web
            try_files /accounts/index.html
            file_server
        }
        header @is_index Content-Type text/html
        respond @is_index 200 {file}/app/data/web/accounts/index.html
        
        @not_index {
            not path /accounts/
            not path /accounts/index.html
        }
        handle @not_index {
            uri strip_prefix /accounts
        root @not_index /app/web/accounts
        file_server @not_index
            root * /app/web/accounts
            try_files {path} /index.html
            file_server
        }
    }

    # Auth app
    handle /auth {
        header Content-Type text/html
        respond 200 {file}/app/data/web/auth/index.html
        root * /app/data/web/auth
        try_files {path} /index.html
        file_server
    }
    
    handle /auth/* {
        @is_index {
            path /auth/
            path /auth/index.html
        @is_index path /auth/ /auth/index.html
        handle @is_index {
            root * /app/data/web
            try_files /auth/index.html
            file_server
        }
        header @is_index Content-Type text/html
        respond @is_index 200 {file}/app/data/web/auth/index.html
        
        @not_index {
            not path /auth/
            not path /auth/index.html
        }
        handle @not_index {
            uri strip_prefix /auth
        root @not_index /app/web/auth
        file_server @not_index
            root * /app/web/auth
            try_files {path} /index.html
            file_server
        }
    }

    # Cast app
    handle /cast {
        header Content-Type text/html
        respond 200 {file}/app/data/web/cast/index.html
        root * /app/data/web/cast
        try_files {path} /index.html
        file_server
    }
    
    handle /cast/* {
        @is_index {
            path /cast/
            path /cast/index.html
        @is_index path /cast/ /cast/index.html
        handle @is_index {
            root * /app/data/web
            try_files /cast/index.html
            file_server
        }
        header @is_index Content-Type text/html
        respond @is_index 200 {file}/app/data/web/cast/index.html
        
        @not_index {
            not path /cast/
            not path /cast/index.html
        }
        handle @not_index {
            uri strip_prefix /cast
        root @not_index /app/web/cast
        file_server @not_index
            root * /app/web/cast
            try_files {path} /index.html
            file_server
        }
    }

    # Main API proxy
@@ -1436,7 +1460,7 @@ cat > /app/data/caddy/Caddyfile <<EOT
}
EOT

echo "==> Created Caddy config with static HTML files at /app/data/caddy/Caddyfile"
echo "==> Created Caddy config with properly modified HTML files at /app/data/caddy/Caddyfile"

# Start Caddy server
echo "==> Starting Caddy server"