Fix API server and URL handling for frontend connectivity
This commit is contained in:
		
							
								
								
									
										173
									
								
								start.sh
									
									
									
									
									
								
							
							
						
						
									
										173
									
								
								start.sh
									
									
									
									
									
								
							@@ -1073,13 +1073,13 @@ else
 | 
			
		||||
    # Create a proper Go module structure - this is critical for the build
 | 
			
		||||
    echo "==> Creating proper Go module structure"
 | 
			
		||||
    
 | 
			
		||||
    # Initialize an explicit Go module 
 | 
			
		||||
    echo "module mock-server" > go.mod
 | 
			
		||||
    # Initialize an explicit Go module with specified go version
 | 
			
		||||
    echo "module main" > go.mod
 | 
			
		||||
    echo "go 1.19" >> go.mod
 | 
			
		||||
    
 | 
			
		||||
    # Write main.go as a single file with correct quoted heredoc
 | 
			
		||||
    # Write main.go as a single file without heredoc
 | 
			
		||||
    echo "==> Writing main.go file for mock API server"
 | 
			
		||||
    cat > main.go <<'ENDOFPROGRAM'
 | 
			
		||||
    cat > main.go << 'ENDOFFILE'
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
@@ -1139,6 +1139,72 @@ func main() {
 | 
			
		||||
        w.Header().Set("Content-Type", "application/json")
 | 
			
		||||
        fmt.Fprintf(w, `{"status":"ok","version":"mock-1.0.0","time":"%s"}`, time.Now().Format(time.RFC3339))
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    // Handle OTT (One-Time Token) requests
 | 
			
		||||
    http.HandleFunc("/users/ott", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
        if r.Method == "POST" {
 | 
			
		||||
            body, err := io.ReadAll(r.Body)
 | 
			
		||||
            if err != nil {
 | 
			
		||||
                http.Error(w, "Error reading request body", http.StatusBadRequest)
 | 
			
		||||
                return
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            logger.Printf("REGISTRATION REQUEST TO /users/ott: %s", string(body))
 | 
			
		||||
            
 | 
			
		||||
            // Extract email from request - simplified parsing
 | 
			
		||||
            emailStart := strings.Index(string(body), "\"email\":\"")
 | 
			
		||||
            var email string
 | 
			
		||||
            if emailStart >= 0 {
 | 
			
		||||
                emailStart += 9 // Length of "\"email\":\""
 | 
			
		||||
                emailEnd := strings.Index(string(body)[emailStart:], "\"")
 | 
			
		||||
                if emailEnd >= 0 {
 | 
			
		||||
                    email = string(body)[emailStart : emailStart+emailEnd]
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            // Generate verification code - 6 digits for OTT
 | 
			
		||||
            verificationCode := fmt.Sprintf("%06d", 100000 + rand.Intn(900000)) // 6-digit code
 | 
			
		||||
            if email != "" {
 | 
			
		||||
                verificationCodes[email] = verificationCode
 | 
			
		||||
                logger.Printf("===================================================")
 | 
			
		||||
                logger.Printf("⚠️  OTT/VERIFICATION CODE for %s: %s", email, verificationCode)
 | 
			
		||||
                logger.Printf("===================================================")
 | 
			
		||||
                
 | 
			
		||||
                // Also log to console for immediate visibility
 | 
			
		||||
                fmt.Printf("===================================================\n")
 | 
			
		||||
                fmt.Printf("⚠️  OTT/VERIFICATION CODE for %s: %s\n", email, verificationCode)
 | 
			
		||||
                fmt.Printf("===================================================\n")
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            // Return a success response with properly formatted data
 | 
			
		||||
            w.Header().Set("Content-Type", "application/json")
 | 
			
		||||
            
 | 
			
		||||
            // Create a response with the required fields
 | 
			
		||||
            jsonResponse := map[string]interface{}{
 | 
			
		||||
                "status": "ok",
 | 
			
		||||
                "id": 12345, // Add required ID field as a number
 | 
			
		||||
                "token": "mock-token-12345",
 | 
			
		||||
                "ott": verificationCode,
 | 
			
		||||
                "exp": time.Now().Add(time.Hour).Unix(),
 | 
			
		||||
                "email": email,
 | 
			
		||||
                "createdAt": time.Now().Format(time.RFC3339),
 | 
			
		||||
                "updatedAt": time.Now().Format(time.RFC3339),
 | 
			
		||||
                "key": map[string]interface{}{
 | 
			
		||||
                    "pubKey": "mockPubKey123456",
 | 
			
		||||
                    "encPubKey": "mockEncPubKey123456",
 | 
			
		||||
                    "kty": "mockKty",
 | 
			
		||||
                    "kid": "mockKid",
 | 
			
		||||
                    "alg": "mockAlg",
 | 
			
		||||
                    "verifyKey": "mockVerifyKey123456",
 | 
			
		||||
                },
 | 
			
		||||
            }
 | 
			
		||||
            json.NewEncoder(w).Encode(jsonResponse)
 | 
			
		||||
        } else {
 | 
			
		||||
            // Just handle other methods with a generic response
 | 
			
		||||
            w.Header().Set("Content-Type", "application/json")
 | 
			
		||||
            fmt.Fprintf(w, `{"status":"mock","endpoint":"%s","method":"%s"}`, r.URL.Path, r.Method)
 | 
			
		||||
        }
 | 
			
		||||
    })
 | 
			
		||||
    
 | 
			
		||||
    // Generic handler for all other requests
 | 
			
		||||
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
@@ -1172,13 +1238,15 @@ func main() {
 | 
			
		||||
        logger.Fatalf("Server failed: %v", err)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
ENDOFPROGRAM
 | 
			
		||||
ENDOFFILE
 | 
			
		||||
    
 | 
			
		||||
    # Show the created files for debugging
 | 
			
		||||
    echo "==> Listing created files:"
 | 
			
		||||
    ls -la
 | 
			
		||||
    echo "==> Contents of go.mod:"
 | 
			
		||||
    cat go.mod
 | 
			
		||||
    echo "==> First 10 lines of main.go:"
 | 
			
		||||
    head -10 main.go
 | 
			
		||||
    
 | 
			
		||||
    # Completely unset Go module environment variables
 | 
			
		||||
    echo "==> Unsetting module flags before building mock server"
 | 
			
		||||
@@ -1187,7 +1255,7 @@ ENDOFPROGRAM
 | 
			
		||||
    unset GOPATH
 | 
			
		||||
    unset GOMODCACHE
 | 
			
		||||
    
 | 
			
		||||
    # Build the mock server using the direct build command
 | 
			
		||||
    # Build the mock server using Go directly
 | 
			
		||||
    echo "==> Building mock API server on port 8080"
 | 
			
		||||
    
 | 
			
		||||
    # Show Go version
 | 
			
		||||
@@ -1196,47 +1264,20 @@ ENDOFPROGRAM
 | 
			
		||||
    # Set SERVER_PID to 0 initially - CRITICAL for avoiding unbound variable later
 | 
			
		||||
    SERVER_PID=0
 | 
			
		||||
    
 | 
			
		||||
    # Try building with explicit output file
 | 
			
		||||
    if go build -o mock_server .; 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 &
 | 
			
		||||
        SERVER_PID=$!
 | 
			
		||||
        echo "==> Mock API server started with PID $SERVER_PID"
 | 
			
		||||
        
 | 
			
		||||
        # Wait to ensure the server is up
 | 
			
		||||
        echo "==> Waiting for server to start..."
 | 
			
		||||
        sleep 5  # Increased sleep time for better reliability
 | 
			
		||||
        
 | 
			
		||||
        # Check if the server is actually running
 | 
			
		||||
        if ps -p $SERVER_PID > /dev/null; then
 | 
			
		||||
            echo "==> Mock API server is running with PID $SERVER_PID"
 | 
			
		||||
            
 | 
			
		||||
            # Check if the port is actually listening
 | 
			
		||||
            if netstat -tulpn 2>/dev/null | grep ":8080" > /dev/null; then
 | 
			
		||||
                echo "==> Mock API server is listening on port 8080"
 | 
			
		||||
            else
 | 
			
		||||
                echo "==> WARNING: Mock API server doesn't appear to be listening on port 8080"
 | 
			
		||||
                echo "==> Checking server logs:"
 | 
			
		||||
                tail -n 30 /app/data/logs/mock_server.log
 | 
			
		||||
            fi
 | 
			
		||||
        else
 | 
			
		||||
            echo "==> ERROR: Mock API server failed to start"
 | 
			
		||||
            echo "==> Server log:"
 | 
			
		||||
            cat /app/data/logs/mock_server.log
 | 
			
		||||
            # Reset SERVER_PID if process died
 | 
			
		||||
            SERVER_PID=0
 | 
			
		||||
        fi
 | 
			
		||||
    else
 | 
			
		||||
        echo "==> ERROR: Failed to build mock API server"
 | 
			
		||||
        go version
 | 
			
		||||
        # Server PID is already set to 0 above
 | 
			
		||||
    fi
 | 
			
		||||
    # Use go run instead of build to simplify the process
 | 
			
		||||
    echo "==> Running Go mock API server directly"
 | 
			
		||||
    mkdir -p /app/data/logs
 | 
			
		||||
    touch /app/data/logs/api_requests.log
 | 
			
		||||
    chmod 666 /app/data/logs/api_requests.log
 | 
			
		||||
    
 | 
			
		||||
    # Run directly with go run in the background
 | 
			
		||||
    nohup go run main.go > /app/data/logs/mock_server.log 2>&1 &
 | 
			
		||||
    SERVER_PID=$!
 | 
			
		||||
    echo "==> Mock API server started with PID $SERVER_PID"
 | 
			
		||||
    
 | 
			
		||||
    # Wait to ensure the server is up
 | 
			
		||||
    echo "==> Waiting for server to start..."
 | 
			
		||||
    sleep 5  # Increased sleep time for better reliability
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
echo "==> Server started with PID ${SERVER_PID:-0}"
 | 
			
		||||
@@ -1616,15 +1657,41 @@ 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;
 | 
			
		||||
    // Ensure the API URL has a proper protocol prefix for URL constructor
 | 
			
		||||
    if (window.ENTE_CONFIG.API_URL.startsWith('/')) {
 | 
			
		||||
        window.ENTE_CONFIG.API_URL = window.location.origin + window.ENTE_CONFIG.API_URL;
 | 
			
		||||
    } else {
 | 
			
		||||
        window.ENTE_CONFIG.API_URL = window.location.origin + '/' + window.ENTE_CONFIG.API_URL;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    // Fix environment variables too
 | 
			
		||||
    if (window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT.startsWith('/')) {
 | 
			
		||||
        window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT;
 | 
			
		||||
    } else {
 | 
			
		||||
        window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = window.location.origin + '/' + window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    // Same for other variables
 | 
			
		||||
    if (window.process.env.REACT_APP_ENTE_ENDPOINT.startsWith('/')) {
 | 
			
		||||
        window.process.env.REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.REACT_APP_ENTE_ENDPOINT;
 | 
			
		||||
    } else {
 | 
			
		||||
        window.process.env.REACT_APP_ENTE_ENDPOINT = window.location.origin + '/' + window.process.env.REACT_APP_ENTE_ENDPOINT;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    if (window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT && !window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT.startsWith('http')) {
 | 
			
		||||
        if (window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT.startsWith('/')) {
 | 
			
		||||
            window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT;
 | 
			
		||||
        } else {
 | 
			
		||||
            window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = window.location.origin + '/' + window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Extra logging to debug URL construction
 | 
			
		||||
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);
 | 
			
		||||
console.log('API_URL (final):', window.ENTE_CONFIG.API_URL);
 | 
			
		||||
console.log('PUBLIC_ALBUMS_URL (final):', window.ENTE_CONFIG.PUBLIC_ALBUMS_URL);
 | 
			
		||||
console.log('NEXT_PUBLIC_ENTE_ENDPOINT (final):', window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT);
 | 
			
		||||
EOF
 | 
			
		||||
 | 
			
		||||
    # Update the variables in the runtime config
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user