Fix here-document syntax issues in runtime config generation and Go module setup for mock API server

This commit is contained in:
Andreas Düren 2025-03-20 12:49:06 +01:00
parent 5dbbb094b4
commit defe47f78d

479
start.sh
View File

@ -1066,16 +1066,18 @@ else
echo "==> ERROR: Museum server not found"
echo "==> Starting a mock server"
# Create a temporary directory for a simple Go server
# Create a temporary directory for a simple Go server with correct permissions
mkdir -p /tmp/mock-server
cd /tmp/mock-server
# Initialize a proper Go module
echo "==> Initializing Go module for mock API server"
go mod init mock-server
# Create a simple Go module setup
echo "==> Creating proper Go module structure"
touch go.mod
echo "module mock-server" > go.mod
# Write the program to a file with proper quoting
cat > main.go << 'GOMOCK'
# Write main.go correctly
echo "==> Writing main.go for mock API server"
cat > main.go << EOF
package main
import (
@ -1454,7 +1456,7 @@ func main() {
logger.Fatalf("Server failed: %v", err)
}
}
GOMOCK
EOF
# Completely unset Go module-related environment variables
echo "==> Unsetting module flags before building mock server"
@ -1463,24 +1465,23 @@ GOMOCK
unset GOMODCACHE
unset GOPATH
# Build and run the mock server in the background
echo "==> Building and starting mock API server on port 8080"
# Show directory content for debugging
echo "==> Current directory contents:"
ls -la
# Show go.mod for debugging
echo "==> Go module file contents:"
cat go.mod
# Build and run the mock server in the background
echo "==> Building and starting mock API server on port 8080"
# Build the mock server (explicitly in current directory)
if go build -o mock_server .; then
# Build without specifying the file (shorter command)
if go build -v .; then
echo "==> Successfully compiled mock API server"
# Create log directory if it doesn't exist
mkdir -p /app/data/logs
# Start the server and log both to file and to console
chmod +x ./mock_server
nohup ./mock_server > /app/data/logs/mock_server.log 2>&1 &
chmod +x ./mock-server
nohup ./mock-server > /app/data/logs/mock_server.log 2>&1 &
SERVER_PID=$!
echo "==> Mock API server started with PID $SERVER_PID"
@ -1877,34 +1878,34 @@ EOT
# 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 = {
API_URL: '${API_ENDPOINT}',
PUBLIC_ALBUMS_URL: '${CLOUDRON_APP_ORIGIN}/public'
};
cat << EOF > /app/data/web/runtime-config.js
// Runtime configuration for Ente
window.ENTE_CONFIG = {
API_URL: '${API_ENDPOINT}',
PUBLIC_ALBUMS_URL: '${CLOUDRON_APP_ORIGIN}/public'
};
// Next.js environment variables
window.process = window.process || {};
window.process.env = window.process.env || {};
window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = '${API_ENDPOINT}';
window.process.env.NEXT_PUBLIC_ENTE_PUBLIC_ALBUMS_ENDPOINT = '${CLOUDRON_APP_ORIGIN}/public';
window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = '${API_ENDPOINT}';
window.process.env.REACT_APP_ENTE_ENDPOINT = '${API_ENDPOINT}';
// Next.js environment variables
window.process = window.process || {};
window.process.env = window.process.env || {};
window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = '${API_ENDPOINT}';
window.process.env.NEXT_PUBLIC_ENTE_PUBLIC_ALBUMS_ENDPOINT = '${CLOUDRON_APP_ORIGIN}/public';
window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = '${API_ENDPOINT}';
window.process.env.REACT_APP_ENTE_ENDPOINT = '${API_ENDPOINT}';
// Make sure all URLs are properly formatted for URL constructor
if (!window.ENTE_CONFIG.API_URL.startsWith('http')) {
console.log('Adding https:// prefix to API_URL');
window.ENTE_CONFIG.API_URL = window.location.origin + window.ENTE_CONFIG.API_URL;
window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT;
window.process.env.REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.REACT_APP_ENTE_ENDPOINT;
window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT;
}
// Make sure all URLs are properly formatted for URL constructor
if (!window.ENTE_CONFIG.API_URL.startsWith('http')) {
console.log('Adding https:// prefix to API_URL');
window.ENTE_CONFIG.API_URL = window.location.origin + window.ENTE_CONFIG.API_URL;
window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT;
window.process.env.REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.REACT_APP_ENTE_ENDPOINT;
window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT;
}
console.log('Ente runtime config loaded from runtime-config.js');
console.log('API_URL:', window.ENTE_CONFIG.API_URL);
console.log('PUBLIC_ALBUMS_URL:', window.ENTE_CONFIG.PUBLIC_ALBUMS_URL);
EOT
console.log('Ente runtime config loaded from runtime-config.js');
console.log('API_URL:', window.ENTE_CONFIG.API_URL);
console.log('PUBLIC_ALBUMS_URL:', window.ENTE_CONFIG.PUBLIC_ALBUMS_URL);
EOF
# Update the variables in the runtime config
sed -i "s|\${API_ENDPOINT}|${API_ENDPOINT}|g" /app/data/web/runtime-config.js
@ -1954,214 +1955,214 @@ HTML
done
# Modify the Caddyfile to serve our modified HTML files
cat > /app/data/caddy/Caddyfile <<EOT
# Global settings
{
admin off
auto_https off
http_port $CADDY_PORT
https_port 0
}
cat << EOF > /app/data/caddy/Caddyfile
# Global settings
{
admin off
auto_https off
http_port $CADDY_PORT
https_port 0
}
# Main site configuration
:$CADDY_PORT {
# Basic logging
log {
level INFO
output file /app/data/logs/caddy.log
}
# Configuration scripts - directly served
handle /config.js {
header Content-Type application/javascript
respond "
// Direct configuration for Ente
window.ENTE_CONFIG = {
API_URL: '${API_ENDPOINT}',
PUBLIC_ALBUMS_URL: '${CLOUDRON_APP_ORIGIN}/public'
};
// Next.js environment variables
window.process = window.process || {};
window.process.env = window.process.env || {};
window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = '${API_ENDPOINT}';
window.process.env.NEXT_PUBLIC_ENTE_PUBLIC_ALBUMS_ENDPOINT = '${CLOUDRON_APP_ORIGIN}/public';
window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = '${API_ENDPOINT}';
window.process.env.REACT_APP_ENTE_ENDPOINT = '${API_ENDPOINT}';
// Make sure all URLs are properly formatted for URL constructor
if (!window.ENTE_CONFIG.API_URL.startsWith('http')) {
console.log('Adding https:// prefix to API_URL');
window.ENTE_CONFIG.API_URL = window.location.origin + window.ENTE_CONFIG.API_URL;
window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT;
window.process.env.REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.REACT_APP_ENTE_ENDPOINT;
window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT;
}
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);
"
}
handle /runtime-config.js {
root * /app/data/web
# Main site configuration
:$CADDY_PORT {
# Basic logging
log {
level INFO
output file /app/data/logs/caddy.log
}
# Configuration scripts - directly served
handle /config.js {
header Content-Type application/javascript
respond "
// Direct configuration for Ente
window.ENTE_CONFIG = {
API_URL: '${API_ENDPOINT}',
PUBLIC_ALBUMS_URL: '${CLOUDRON_APP_ORIGIN}/public'
};
// Next.js environment variables
window.process = window.process || {};
window.process.env = window.process.env || {};
window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = '${API_ENDPOINT}';
window.process.env.NEXT_PUBLIC_ENTE_PUBLIC_ALBUMS_ENDPOINT = '${CLOUDRON_APP_ORIGIN}/public';
window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = '${API_ENDPOINT}';
window.process.env.REACT_APP_ENTE_ENDPOINT = '${API_ENDPOINT}';
// Make sure all URLs are properly formatted for URL constructor
if (!window.ENTE_CONFIG.API_URL.startsWith('http')) {
console.log('Adding https:// prefix to API_URL');
window.ENTE_CONFIG.API_URL = window.location.origin + window.ENTE_CONFIG.API_URL;
window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT;
window.process.env.REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.REACT_APP_ENTE_ENDPOINT;
window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT;
}
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);
"
}
handle /runtime-config.js {
root * /app/data/web
file_server
}
# Root path serves the photos app
handle / {
# Special handling for index.html
@is_index path /
handle @is_index {
root * /app/data/web/photos
try_files {path} /index.html
file_server
}
# Root path serves the photos app
handle / {
# Special handling for index.html
@is_index path /
handle @is_index {
root * /app/data/web/photos
try_files {path} /index.html
file_server
}
# 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
}
# 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/*
}
# Next.js static files
handle /_next/* {
handle @not_index {
root * /app/web/photos
file_server
}
# Add global headers for common file types
header /*.js Content-Type application/javascript
header /*.css Content-Type text/css
header /*.json Content-Type application/json
header /*.svg Content-Type image/svg+xml
header /*.woff2 Content-Type font/woff2
header /_next/static/chunks/*.js Content-Type application/javascript
header /_next/static/css/*.css Content-Type text/css
# Accounts app
handle /accounts {
root * /app/data/web/accounts
try_files {path} /index.html
file_server
}
handle /accounts/* {
@is_index path /accounts/ /accounts/index.html
handle @is_index {
root * /app/data/web
try_files /accounts/index.html
file_server
}
@not_index {
not path /accounts/
not path /accounts/index.html
}
handle @not_index {
uri strip_prefix /accounts
root * /app/web/accounts
try_files {path} /index.html
file_server
}
}
# Auth app
handle /auth {
root * /app/data/web/auth
try_files {path} /index.html
file_server
}
handle /auth/* {
@is_index path /auth/ /auth/index.html
handle @is_index {
root * /app/data/web
try_files /auth/index.html
file_server
}
@not_index {
not path /auth/
not path /auth/index.html
}
handle @not_index {
uri strip_prefix /auth
root * /app/web/auth
try_files {path} /index.html
file_server
}
}
# Cast app
handle /cast {
root * /app/data/web/cast
try_files {path} /index.html
file_server
}
handle /cast/* {
@is_index path /cast/ /cast/index.html
handle @is_index {
root * /app/data/web
try_files /cast/index.html
file_server
}
@not_index {
not path /cast/
not path /cast/index.html
}
handle @not_index {
uri strip_prefix /cast
root * /app/web/cast
try_files {path} /index.html
file_server
}
}
# Main API proxy
handle /api/* {
uri strip_prefix /api
reverse_proxy 0.0.0.0:$API_PORT
}
# Public albums API proxy
handle /public/* {
uri strip_prefix /public
reverse_proxy 0.0.0.0:$PUBLIC_ALBUMS_PORT
}
# Health check endpoints
handle /health {
respond "OK"
}
handle /healthcheck {
respond "OK"
}
handle /api/health {
uri strip_prefix /api
reverse_proxy 0.0.0.0:$API_PORT
}
handle /public/health {
uri strip_prefix /public
reverse_proxy 0.0.0.0:$PUBLIC_ALBUMS_PORT
}
}
EOT
# Next.js static files
handle /_next/* {
root * /app/web/photos
file_server
}
# Add global headers for common file types
header /*.js Content-Type application/javascript
header /*.css Content-Type text/css
header /*.json Content-Type application/json
header /*.svg Content-Type image/svg+xml
header /*.woff2 Content-Type font/woff2
header /_next/static/chunks/*.js Content-Type application/javascript
header /_next/static/css/*.css Content-Type text/css
# Accounts app
handle /accounts {
root * /app/data/web/accounts
try_files {path} /index.html
file_server
}
handle /accounts/* {
@is_index path /accounts/ /accounts/index.html
handle @is_index {
root * /app/data/web
try_files /accounts/index.html
file_server
}
@not_index {
not path /accounts/
not path /accounts/index.html
}
handle @not_index {
uri strip_prefix /accounts
root * /app/web/accounts
try_files {path} /index.html
file_server
}
}
# Auth app
handle /auth {
root * /app/data/web/auth
try_files {path} /index.html
file_server
}
handle /auth/* {
@is_index path /auth/ /auth/index.html
handle @is_index {
root * /app/data/web
try_files /auth/index.html
file_server
}
@not_index {
not path /auth/
not path /auth/index.html
}
handle @not_index {
uri strip_prefix /auth
root * /app/web/auth
try_files {path} /index.html
file_server
}
}
# Cast app
handle /cast {
root * /app/data/web/cast
try_files {path} /index.html
file_server
}
handle /cast/* {
@is_index path /cast/ /cast/index.html
handle @is_index {
root * /app/data/web
try_files /cast/index.html
file_server
}
@not_index {
not path /cast/
not path /cast/index.html
}
handle @not_index {
uri strip_prefix /cast
root * /app/web/cast
try_files {path} /index.html
file_server
}
}
# Main API proxy
handle /api/* {
uri strip_prefix /api
reverse_proxy 0.0.0.0:$API_PORT
}
# Public albums API proxy
handle /public/* {
uri strip_prefix /public
reverse_proxy 0.0.0.0:$PUBLIC_ALBUMS_PORT
}
# Health check endpoints
handle /health {
respond "OK"
}
handle /healthcheck {
respond "OK"
}
handle /api/health {
uri strip_prefix /api
reverse_proxy 0.0.0.0:$API_PORT
}
handle /public/health {
uri strip_prefix /public
reverse_proxy 0.0.0.0:$PUBLIC_ALBUMS_PORT
}
}
EOF
echo "==> Created Caddy config with properly modified HTML files at /app/data/caddy/Caddyfile"