diff --git a/start.sh b/start.sh index 10146be..48913f5 100644 --- a/start.sh +++ b/start.sh @@ -264,125 +264,241 @@ if [ -f "${MUSEUM_DIR}/museum" ] && [ -x "${MUSEUM_DIR}/museum" ]; then done else echo "==> ERROR: Museum server not found at ${MUSEUM_DIR}/museum" - echo "==> Starting a mock server for demonstration purposes" + echo "==> Starting a mock server with Node.js for demonstration purposes" - # Create a temporary directory for a simple Go server + # Create a temporary directory for a simple Node.js server mkdir -p /tmp/mock-server cd /tmp/mock-server - # Create a minimal Go server file that just logs requests and returns mock responses - cat > server.go << 'ENDOFCODE' -package main + # Create a minimal Node.js server file + cat > server.js << 'ENDOFCODE' +const http = require('http'); +const fs = require('fs'); +const path = require('path'); -import ( - "encoding/json" - "fmt" - "log" - "math/rand" - "net/http" - "os" - "time" -) +// Ensure log directory exists +const logDir = '/app/data/logs'; +fs.mkdirSync(logDir, { recursive: true }); +const logFile = path.join(logDir, 'api_requests.log'); -func main() { - // Create log file - os.MkdirAll("/app/data/logs", 0755) - logFile, err := os.OpenFile("/app/data/logs/api_requests.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) - if err != nil { - fmt.Printf("Error creating log file: %v\n", err) - } else { - defer logFile.Close() - log.SetOutput(logFile) - } +// Open log file +fs.writeFileSync(logFile, `API Server started at ${new Date().toISOString()}\n`, { flag: 'a' }); - // Log startup - fmt.Println("Starting mock Ente API server on port 8080") - log.Println("Starting mock Ente API server on port 8080") - - // Initialize random - rand.Seed(time.Now().UnixNano()) - - // Health check endpoint - http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - fmt.Fprintf(w, `{"status":"ok","time":"%s"}`, time.Now().Format(time.RFC3339)) - }) - - // Login/token endpoints - http.HandleFunc("/users/ott", func(w http.ResponseWriter, r *http.Request) { - log.Printf("OTT Request received: %s %s", r.Method, r.URL.Path) - - // Generate verification code - code := fmt.Sprintf("%06d", 100000+rand.Intn(900000)) - - // Log the verification code prominently - message := fmt.Sprintf("⚠️ VERIFICATION CODE: %s", code) - fmt.Println(message) - log.Println(message) - - w.Header().Set("Content-Type", "application/json") - response := map[string]interface{}{ - "status": "ok", - "id": 12345, - "token": "mock-token-12345", - "ott": code, - "exp": time.Now().Add(time.Hour).Unix(), - "email": "user@example.com", - "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(response) - }) - - // User verification endpoint - http.HandleFunc("/users/verification", func(w http.ResponseWriter, r *http.Request) { - log.Printf("Verification request received: %s %s", r.Method, r.URL.Path) - - // Accept any 6-digit code - fmt.Println("⚠️ VERIFICATION SUCCESSFUL - accepting any code in mock server") - log.Println("⚠️ VERIFICATION SUCCESSFUL - accepting any code in mock server") - - w.Header().Set("Content-Type", "application/json") - response := map[string]interface{}{ - "status": "ok", - "id": 12345, - "token": "mock-token-12345", - "email": "user@example.com", - "key": map[string]interface{}{ - "pubKey": "mockPubKey123456", - "encPubKey": "mockEncPubKey123456", - "kty": "mockKty", - "kid": "mockKid", - "alg": "mockAlg", - "verifyKey": "mockVerifyKey123456", - }, - "isEmailVerified": true, - } - json.NewEncoder(w).Encode(response) - }) - - // Default handler - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - log.Printf("Request: %s %s", r.Method, r.URL.Path) - w.Header().Set("Content-Type", "application/json") - fmt.Fprintf(w, `{"status":"ok","path":"%s"}`, r.URL.Path) - }) - - // Start server - fmt.Println("Server listening on 0.0.0.0:8080") - if err := http.ListenAndServe("0.0.0.0:8080", nil); err != nil { - fmt.Printf("Server error: %v\n", err) - log.Fatalf("Server error: %v", err) - } +// Log function +function log(message) { + const timestamp = new Date().toISOString(); + const logMessage = `${timestamp} - ${message}\n`; + console.log(message); + fs.writeFileSync(logFile, logMessage, { flag: 'a' }); } + +// Generate random 6-digit code +function generateCode() { + return Math.floor(100000 + Math.random() * 900000).toString(); +} + +// Create HTTP server +const server = http.createServer((req, res) => { + const url = req.url; + const method = req.method; + + log(`Received ${method} request for ${url}`); + + // Set CORS headers + res.setHeader('Access-Control-Allow-Origin', '*'); + res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); + res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); + + // Handle preflight requests + if (method === 'OPTIONS') { + res.statusCode = 200; + res.end(); + return; + } + + // Handle requests based on URL path + if (url === '/health') { + // Health check endpoint + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify({ + status: "ok", + time: new Date().toISOString() + })); + } + else if (url === '/users/ott') { + // OTT verification code endpoint + let body = ''; + + req.on('data', chunk => { + body += chunk.toString(); + }); + + req.on('end', () => { + let email = 'user@example.com'; + + // Try to parse email from request if possible + try { + const data = JSON.parse(body); + if (data.email) { + email = data.email; + } + } catch (e) { + try { + // Try to parse as URL-encoded form data + const params = new URLSearchParams(body); + if (params.has('email')) { + email = params.get('email'); + } + } catch (e2) { + // Ignore parsing errors + } + } + + // Generate verification code + const code = generateCode(); + + // Log the code prominently + const codeMessage = `⚠️ VERIFICATION CODE FOR ${email}: ${code}`; + log(codeMessage); + console.log('\n' + codeMessage + '\n'); + + // Send response + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify({ + status: "ok", + id: 12345, + token: "mock-token-12345", + ott: code, + exp: Math.floor(Date.now()/1000) + 3600, + email: email, + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + key: { + pubKey: "mockPubKey123456", + encPubKey: "mockEncPubKey123456", + kty: "mockKty", + kid: "mockKid", + alg: "mockAlg", + verifyKey: "mockVerifyKey123456" + } + })); + }); + } + else if (url === '/users/verification') { + // Verification endpoint + let body = ''; + + req.on('data', chunk => { + body += chunk.toString(); + }); + + req.on('end', () => { + log("Verification request received with body: " + body); + log("⚠️ VERIFICATION SUCCESSFUL - accepting any code in mock server"); + + // Send success response + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify({ + status: "ok", + id: 12345, + token: "mock-token-12345", + email: "user@example.com", + key: { + pubKey: "mockPubKey123456", + encPubKey: "mockEncPubKey123456", + kty: "mockKty", + kid: "mockKid", + alg: "mockAlg", + verifyKey: "mockVerifyKey123456" + }, + isEmailVerified: true + })); + }); + } + else { + // Default handler for other paths + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify({ + status: "ok", + path: url + })); + } +}); + +// Start server +const PORT = 8080; +server.listen(PORT, '0.0.0.0', () => { + log(`Mock API server running at http://0.0.0.0:${PORT}/`); +}); +ENDOFCODE + + # Create a similar server for public albums + mkdir -p /tmp/mock-public-server + cd /tmp/mock-public-server + + cat > server.js << 'ENDOFCODE' +const http = require('http'); +const fs = require('fs'); +const path = require('path'); + +// Ensure log directory exists +const logDir = '/app/data/logs'; +fs.mkdirSync(logDir, { recursive: true }); +const logFile = path.join(logDir, 'public_api_requests.log'); + +// Open log file +fs.writeFileSync(logFile, `Public Albums API Server started at ${new Date().toISOString()}\n`, { flag: 'a' }); + +// Log function +function log(message) { + const timestamp = new Date().toISOString(); + const logMessage = `${timestamp} - ${message}\n`; + console.log(message); + fs.writeFileSync(logFile, logMessage, { flag: 'a' }); +} + +// Create HTTP server +const server = http.createServer((req, res) => { + const url = req.url; + const method = req.method; + + log(`Received ${method} request for ${url}`); + + // Set CORS headers + res.setHeader('Access-Control-Allow-Origin', '*'); + res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); + res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); + + // Handle preflight requests + if (method === 'OPTIONS') { + res.statusCode = 200; + res.end(); + return; + } + + // Health check endpoint + if (url === '/health') { + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify({ + status: "ok", + time: new Date().toISOString() + })); + } + else { + // Default handler for other paths + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify({ + status: "ok", + path: url + })); + } +}); + +// Start server +const PORT = 8081; +server.listen(PORT, '0.0.0.0', () => { + log(`Mock Public Albums API server running at http://0.0.0.0:${PORT}/`); +}); ENDOFCODE # Set SERVER_PID to 0 for safety @@ -394,8 +510,9 @@ ENDOFCODE chmod 666 "${LOGS_DIR}/api_requests.log" # Run the mock server - echo "==> Running mock API server" - go run server.go > "${LOGS_DIR}/mock_server.log" 2>&1 & + echo "==> Running mock API server with Node.js" + cd /tmp/mock-server + node server.js > "${LOGS_DIR}/mock_server.log" 2>&1 & SERVER_PID=$! echo "==> Mock API server started with PID $SERVER_PID" @@ -404,65 +521,12 @@ ENDOFCODE echo "==> Testing mock API connectivity" curl -s --max-time 2 --fail http://0.0.0.0:${API_PORT}/health || echo "==> Warning: Mock API server not responding!" - # Create a similar mock server for public albums - mkdir -p /tmp/mock-public-server - cd /tmp/mock-public-server - - cat > server.go << 'ENDOFCODE' -package main - -import ( - "encoding/json" - "fmt" - "log" - "net/http" - "os" - "time" -) - -func main() { - port := "8081" - - // Create log directory and file - os.MkdirAll("/app/data/logs", 0755) - logFile, err := os.OpenFile("/app/data/logs/public_api_requests.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) - if err != nil { - fmt.Printf("Error creating log file: %v\n", err) - } else { - defer logFile.Close() - log.SetOutput(logFile) - } - - // Log startup - fmt.Println("Starting mock Public Albums server on port", port) - log.Println("Starting mock Public Albums server on port", port) - - // Health check endpoint - http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - fmt.Fprintf(w, `{"status":"ok","time":"%s"}`, time.Now().Format(time.RFC3339)) - }) - - // Default handler - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - log.Printf("Public Albums Request: %s %s", r.Method, r.URL.Path) - w.Header().Set("Content-Type", "application/json") - fmt.Fprintf(w, `{"status":"ok","path":"%s"}`, r.URL.Path) - }) - - // Start server - fmt.Println("Public Albums server listening on 0.0.0.0:" + port) - if err := http.ListenAndServe("0.0.0.0:"+port, nil); err != nil { - fmt.Printf("Public Albums server error: %v\n", err) - log.Fatalf("Public Albums server error: %v", err) - } -} -ENDOFCODE - # Run the public albums mock server - go run server.go > "${LOGS_DIR}/public_mock_server.log" 2>&1 & + echo "==> Running Public Albums mock server with Node.js" + cd /tmp/mock-public-server + node server.js > "${LOGS_DIR}/public_mock_server.log" 2>&1 & PUBLIC_SERVER_PID=$! - echo "==> Started Public Albums mock server with PID $PUBLIC_SERVER_PID" + echo "==> Public Albums mock server started with PID $PUBLIC_SERVER_PID" # Wait for it to start sleep 3