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

Fixed Caddy filter directive and Go import issues

parent 43cb6858
Loading
Loading
Loading
Loading
+156 −66
Original line number Diff line number Diff line
@@ -637,24 +637,33 @@ import (
func main() {
    port := "8080"
    
    log.Println("Starting mock Ente API server on port", port)
    fmt.Println("Starting mock Ente API server on port", port)
    log.Println("This is a standalone mock server that doesn't require any Ente modules")
    
    // Log some environment variables for debugging
    log.Println("Environment variables:")
    log.Println("PGHOST:", os.Getenv("PGHOST"))
    log.Println("PGPORT:", os.Getenv("PGPORT"))
    log.Println("API_ENDPOINT:", os.Getenv("ENTE_API_ENDPOINT"))
    fmt.Println("Environment variables:")
    fmt.Println("PGHOST:", os.Getenv("PGHOST"))
    fmt.Println("PGPORT:", os.Getenv("PGPORT"))
    fmt.Println("API_ENDPOINT:", os.Getenv("ENTE_API_ENDPOINT"))
    
    // Create a logger that logs to both stdout and a file
    os.MkdirAll("/app/data/logs", 0755) // Ensure the logs directory exists
    logFile, err := os.OpenFile("/app/data/logs/api_requests.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
    if err != nil {
        log.Printf("Error opening log file: %v", err)
        fmt.Printf("Error opening log file: %v\n", err)
    }
    defer func() {
        if logFile != nil {
            logFile.Close()
        }
    defer logFile.Close()
    }()
    
    multiWriter := io.MultiWriter(os.Stdout, logFile)
    var multiWriter io.Writer
    if logFile != nil {
        multiWriter = io.MultiWriter(os.Stdout, logFile)
    } else {
        multiWriter = os.Stdout
    }
    logger := log.New(multiWriter, "", log.LstdFlags)
    
    // Initialize random seed
@@ -701,6 +710,7 @@ func main() {
            
            // Return a success response
            w.Header().Set("Content-Type", "application/json")
            w.WriteHeader(http.StatusOK)
            
            // Use the encoding/json package to create and send the response
            jsonResponse := map[string]string{
@@ -815,7 +825,9 @@ func main() {
    logger.Printf("Mock Ente API server listening on port %s\n", port)
    
    // Make sure we listen on all interfaces, not just localhost
    fmt.Printf("Starting HTTP server on 0.0.0.0:%s\n", port)
    if err := http.ListenAndServe("0.0.0.0:" + port, nil); err != nil {
        fmt.Printf("Server failed: %v\n", err)
        logger.Fatalf("Server failed: %v", err)
    }
}
@@ -958,24 +970,33 @@ import (
func main() {
    port := "8081"
    
    log.Println("Starting mock Public Albums API server on port", port)
    fmt.Println("Starting mock Public Albums API server on port", port)
    log.Println("This is a standalone mock server that doesn't require any Ente modules")
    
    // Log some environment variables for debugging
    log.Println("Environment variables:")
    log.Println("PGHOST:", os.Getenv("PGHOST"))
    log.Println("PGPORT:", os.Getenv("PGPORT"))
    log.Println("API_ENDPOINT:", os.Getenv("ENTE_API_ENDPOINT"))
    fmt.Println("Environment variables:")
    fmt.Println("PGHOST:", os.Getenv("PGHOST"))
    fmt.Println("PGPORT:", os.Getenv("PGPORT"))
    fmt.Println("API_ENDPOINT:", os.Getenv("ENTE_API_ENDPOINT"))
    
    // Create a logger that logs to both stdout and a file
    os.MkdirAll("/app/data/logs", 0755) // Ensure the logs directory exists
    logFile, err := os.OpenFile("/app/data/logs/public_api_requests.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
    if err != nil {
        log.Printf("Error opening log file: %v", err)
        fmt.Printf("Error opening log file: %v\n", err)
    }
    defer func() {
        if logFile != nil {
            logFile.Close()
        }
    defer logFile.Close()
    }()
    
    multiWriter := io.MultiWriter(os.Stdout, logFile)
    var multiWriter io.Writer
    if logFile != nil {
        multiWriter = io.MultiWriter(os.Stdout, logFile)
    } else {
        multiWriter = os.Stdout
    }
    logger := log.New(multiWriter, "", log.LstdFlags)
    
    http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
@@ -1004,6 +1025,9 @@ func main() {
        
        w.Header().Set("Content-Type", "application/json")
        
        // Use fmt.Fprintf to explicitly use the fmt package
        fmt.Fprintf(w, "{\"status\":\"processing\"}")
        
        // Use json package to create the response
        response := map[string]string{
            "status":   "mock",
@@ -1018,7 +1042,9 @@ func main() {
    logger.Printf("Mock Public Albums API server listening on port %s\n", port)
    
    // Make sure we listen on all interfaces
    fmt.Printf("Starting Public Albums HTTP server on 0.0.0.0:%s\n", port)
    if err := http.ListenAndServe("0.0.0.0:" + port, nil); err != nil {
        fmt.Printf("Public Albums server failed: %v\n", err)
        logger.Fatalf("Server failed: %v", err)
    }
}
@@ -1054,24 +1080,33 @@ import (
func main() {
    port := "8081"
    
    log.Println("Starting mock Public Albums API server on port", port)
    fmt.Println("Starting mock Public Albums API server on port", port)
    log.Println("This is a standalone mock server that doesn't require any Ente modules")
    
    // Log some environment variables for debugging
    log.Println("Environment variables:")
    log.Println("PGHOST:", os.Getenv("PGHOST"))
    log.Println("PGPORT:", os.Getenv("PGPORT"))
    log.Println("API_ENDPOINT:", os.Getenv("ENTE_API_ENDPOINT"))
    fmt.Println("Environment variables:")
    fmt.Println("PGHOST:", os.Getenv("PGHOST"))
    fmt.Println("PGPORT:", os.Getenv("PGPORT"))
    fmt.Println("API_ENDPOINT:", os.Getenv("ENTE_API_ENDPOINT"))
    
    // Create a logger that logs to both stdout and a file
    os.MkdirAll("/app/data/logs", 0755) // Ensure the logs directory exists
    logFile, err := os.OpenFile("/app/data/logs/public_api_requests.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
    if err != nil {
        log.Printf("Error opening log file: %v", err)
        fmt.Printf("Error opening log file: %v\n", err)
    }
    defer func() {
        if logFile != nil {
            logFile.Close()
        }
    defer logFile.Close()
    }()
    
    multiWriter := io.MultiWriter(os.Stdout, logFile)
    var multiWriter io.Writer
    if logFile != nil {
        multiWriter = io.MultiWriter(os.Stdout, logFile)
    } else {
        multiWriter = os.Stdout
    }
    logger := log.New(multiWriter, "", log.LstdFlags)
    
    http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
@@ -1100,6 +1135,9 @@ func main() {
        
        w.Header().Set("Content-Type", "application/json")
        
        // Use fmt.Fprintf to explicitly use the fmt package
        fmt.Fprintf(w, "{\"status\":\"processing\"}")
        
        // Use json package to create the response
        response := map[string]string{
            "status":   "mock",
@@ -1114,7 +1152,9 @@ func main() {
    logger.Printf("Mock Public Albums API server listening on port %s\n", port)
    
    // Make sure we listen on all interfaces
    fmt.Printf("Starting Public Albums HTTP server on 0.0.0.0:%s\n", port)
    if err := http.ListenAndServe("0.0.0.0:" + port, nil); err != nil {
        fmt.Printf("Public Albums server failed: %v\n", err)
        logger.Fatalf("Server failed: %v", err)
    }
}
@@ -1224,11 +1264,6 @@ cat > /app/data/caddy/Caddyfile <<EOT
            
            console.log('Ente config loaded - API_URL:', window.ENTE_CONFIG.API_URL);
            console.log('Ente config loaded - PUBLIC_ALBUMS_URL:', window.ENTE_CONFIG.PUBLIC_ALBUMS_URL);

            // Dynamically inject the config by creating script elements
            document.addEventListener('DOMContentLoaded', function() {
                console.log('DOM loaded, injecting runtime config as needed');
            });
        "
    }

@@ -1237,21 +1272,50 @@ cat > /app/data/caddy/Caddyfile <<EOT
        file_server
    }

    # Root path serves the photos app
    # 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
    handle / {
        root * /app/web/photos
        try_files {path} /index.html
        file_server
        
        # Dynamically inject our configuration script
        header * +Link "</config.js>; rel=preload; as=script"
        header * +Link "</runtime-config.js>; rel=preload; as=script"
        header ?index.html +Content-Security-Policy "script-src 'self' 'unsafe-inline' 'unsafe-eval'; connect-src *"
        # Rewrite index.html to include our config
        @isroot {
            path /
        }
        
        # For other files just serve them directly
        file_server
        
        # Add script injection as HTML filter
        filter {
            search_pattern </head>
            replacement <script src="/config.js" type="text/javascript"></script><script src="/runtime-config.js" type="text/javascript"></script></head>
        # 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}
            }
        }
    }

@@ -1271,45 +1335,72 @@ cat > /app/data/caddy/Caddyfile <<EOT
    header /_next/static/css/*.css Content-Type text/css

    # Accounts app
    handle /accounts {
        header Content-Type text/html
        respond 200 {file}/app/data/web/accounts/index.html
    }
    
    handle /accounts/* {
        root * /app/web/accounts
        uri strip_prefix /accounts
        try_files {path} /index.html
        file_server
        @is_index {
            path /accounts/
            path /accounts/index.html
        }
        header @is_index Content-Type text/html
        respond @is_index 200 {file}/app/data/web/accounts/index.html
        
        # Dynamically inject our configuration script for accounts app
        filter ?index.html {
            search_pattern </head>
            replacement <script src="/config.js" type="text/javascript"></script><script src="/runtime-config.js" type="text/javascript"></script></head>
        @not_index {
            not path /accounts/
            not path /accounts/index.html
        }
        uri strip_prefix /accounts
        root @not_index /app/web/accounts
        file_server @not_index
    }

    # Auth app
    handle /auth {
        header Content-Type text/html
        respond 200 {file}/app/data/web/auth/index.html
    }
    
    handle /auth/* {
        root * /app/web/auth
        uri strip_prefix /auth
        try_files {path} /index.html
        file_server
        @is_index {
            path /auth/
            path /auth/index.html
        }
        header @is_index Content-Type text/html
        respond @is_index 200 {file}/app/data/web/auth/index.html
        
        # Dynamically inject our configuration script for auth app
        filter ?index.html {
            search_pattern </head>
            replacement <script src="/config.js" type="text/javascript"></script><script src="/runtime-config.js" type="text/javascript"></script></head>
        @not_index {
            not path /auth/
            not path /auth/index.html
        }
        uri strip_prefix /auth
        root @not_index /app/web/auth
        file_server @not_index
    }

    # Cast app
    handle /cast {
        header Content-Type text/html
        respond 200 {file}/app/data/web/cast/index.html
    }
    
    handle /cast/* {
        root * /app/web/cast
        uri strip_prefix /cast
        try_files {path} /index.html
        file_server
        @is_index {
            path /cast/
            path /cast/index.html
        }
        header @is_index Content-Type text/html
        respond @is_index 200 {file}/app/data/web/cast/index.html
        
        # Dynamically inject our configuration script for cast app
        filter ?index.html {
            search_pattern </head>
            replacement <script src="/config.js" type="text/javascript"></script><script src="/runtime-config.js" type="text/javascript"></script></head>
        @not_index {
            not path /cast/
            not path /cast/index.html
        }
        uri strip_prefix /cast
        root @not_index /app/web/cast
        file_server @not_index
    }

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

echo "==> Created Caddy config with dynamic script injection at /app/data/caddy/Caddyfile"
echo "==> No longer trying to modify read-only HTML files"
echo "==> Created Caddy config with static HTML files at /app/data/caddy/Caddyfile"

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