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

Fixed Caddy header syntax and moved db_override.go creation before server startup

parent 783ad628
Loading
Loading
Loading
Loading
+57 −58
Original line number Diff line number Diff line
@@ -344,6 +344,60 @@ else
    echo "==> Cannot modify /etc/hosts (read-only filesystem)"
fi

# Create the overrides directory and db_override.go file
mkdir -p "$SERVER_DIR/overrides"
echo "==> Creating db_override.go in overrides directory"
cat > "$SERVER_DIR/overrides/db_override.go" <<EOT
// Override database functions - will be added to museum build
package main

import (
	"database/sql"
	"fmt"
	"log"
	"os"
	"strings"
	
	_ "github.com/lib/pq" // Import the postgres driver
)

// This will run before main() and override the database functions
func init() {
	log.Println("Database override patch is active")
	
	host := os.Getenv("CLOUDRON_POSTGRESQL_HOST")
	if host == "" {
		host = os.Getenv("PGHOST")
	}
	
	if host == "" {
		log.Println("WARNING: No PostgreSQL host found in environment, using default")
		return
	}
	
	// Force the PGHOST environment variable
	os.Setenv("PGHOST", host)
	
	log.Printf("Forcing database connections to use host: %s", host)
}

// Force correct database setup - this will be called instead of the original setupDatabase
func forceCorrectDatabase() (*sql.DB, error) {
	host := os.Getenv("CLOUDRON_POSTGRESQL_HOST")
	port := os.Getenv("CLOUDRON_POSTGRESQL_PORT")
	user := os.Getenv("CLOUDRON_POSTGRESQL_USERNAME")
	password := os.Getenv("CLOUDRON_POSTGRESQL_PASSWORD")
	dbname := os.Getenv("CLOUDRON_POSTGRESQL_DATABASE")
	
	connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
		host, port, user, password, dbname)
	
	log.Printf("Opening database connection with: %s", connStr)
	return sql.Open("postgres", connStr)
}
EOT
echo "==> Created overrides/db_override.go"

# Patch source code directly for maximum effectiveness
if [ -d "$SERVER_DIR/cmd/museum" ]; then
    MAIN_GO="$SERVER_DIR/cmd/museum/main.go"
@@ -860,8 +914,8 @@ 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);
        " 200 {
            Content-Type "application/javascript"
        " {
            header Content-Type "application/javascript"
        }
    }
}
@@ -932,58 +986,3 @@ echo "==> Entering wait state - press Ctrl+C to stop"
wait $SERVER_PID
wait $PUBLIC_SERVER_PID
wait $CADDY_PID 
 No newline at end of file

# Create the overrides directory and db_override.go file
mkdir -p "$SERVER_DIR/overrides"
echo "==> Creating db_override.go in overrides directory"
cat > "$SERVER_DIR/overrides/db_override.go" <<EOT
// Override database functions - will be added to museum build
package main

import (
	"database/sql"
	"fmt"
	"log"
	"os"
	"strings"
	
	_ "github.com/lib/pq" // Import the postgres driver
)

// This will run before main() and override the database functions
func init() {
	log.Println("Database override patch is active")
	
	host := os.Getenv("CLOUDRON_POSTGRESQL_HOST")
	if host == "" {
		host = os.Getenv("PGHOST")
	}
	
	if host == "" {
		log.Println("WARNING: No PostgreSQL host found in environment, using default")
		return
	}
	
	// Force the PGHOST environment variable
	os.Setenv("PGHOST", host)
	
	log.Printf("Forcing database connections to use host: %s", host)
}

// Force correct database setup - this will be called instead of the original setupDatabase
func forceCorrectDatabase() (*sql.DB, error) {
	host := os.Getenv("CLOUDRON_POSTGRESQL_HOST")
	port := os.Getenv("CLOUDRON_POSTGRESQL_PORT")
	user := os.Getenv("CLOUDRON_POSTGRESQL_USERNAME")
	password := os.Getenv("CLOUDRON_POSTGRESQL_PASSWORD")
	dbname := os.Getenv("CLOUDRON_POSTGRESQL_DATABASE")
	
	connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
		host, port, user, password, dbname)
	
	log.Printf("Opening database connection with: %s", connStr)
	return sql.Open("postgres", connStr)
}
EOT

echo "==> Created overrides/db_override.go" 
 No newline at end of file