Update server.js
Browse files
server.js
CHANGED
|
@@ -1,24 +1,32 @@
|
|
| 1 |
import express from "express";
|
| 2 |
-
import { spawn } from "child_process";
|
| 3 |
import dotenv from "dotenv";
|
| 4 |
|
| 5 |
console.log('OpenClaw HF Space: Starting server...');
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
} catch (err) {
|
| 11 |
-
console.error('Failed to load environment:', err.message);
|
| 12 |
-
}
|
| 13 |
|
| 14 |
const app = express();
|
| 15 |
app.use(express.json());
|
| 16 |
|
| 17 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
app.get("/", (req, res) => {
|
| 19 |
res.json({
|
| 20 |
status: "OpenClaw HF Space Running",
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
timestamp: new Date().toISOString()
|
| 23 |
});
|
| 24 |
});
|
|
@@ -31,14 +39,11 @@ app.get("/health", (req, res) => {
|
|
| 31 |
});
|
| 32 |
});
|
| 33 |
|
| 34 |
-
// Market research endpoint
|
| 35 |
-
app.post("/api/market-research",
|
| 36 |
-
console.log('
|
| 37 |
-
keyword: req.body.keyword,
|
| 38 |
-
timestamp: new Date().toISOString()
|
| 39 |
-
});
|
| 40 |
|
| 41 |
-
const { keyword, api_key
|
| 42 |
|
| 43 |
if (!keyword) {
|
| 44 |
return res.status(400).json({ error: "keyword is required" });
|
|
@@ -51,75 +56,71 @@ app.post("/api/market-research", async (req, res) => {
|
|
| 51 |
});
|
| 52 |
}
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
};
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
const result = await runOpenClaw(env, { keyword });
|
| 65 |
-
console.log('OpenClaw agent succeeded');
|
| 66 |
-
return res.json(result);
|
| 67 |
-
} catch (err) {
|
| 68 |
-
console.error('OpenClaw agent failed:', err.message);
|
| 69 |
-
return res.status(500).json({
|
| 70 |
-
error: "OpenClaw agent failed",
|
| 71 |
-
details: err.message,
|
| 72 |
-
keyword: keyword,
|
| 73 |
-
timestamp: new Date().toISOString()
|
| 74 |
-
});
|
| 75 |
-
}
|
| 76 |
});
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
let stderr = "";
|
| 87 |
-
|
| 88 |
-
proc.on("error", err => {
|
| 89 |
-
console.error('Spawn error:', err);
|
| 90 |
-
reject(err);
|
| 91 |
-
});
|
| 92 |
-
|
| 93 |
-
proc.stdout.on("data", data => {
|
| 94 |
-
stdout += data.toString();
|
| 95 |
-
});
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
console.error('JSON parse error:', err.message);
|
| 112 |
-
console.error('Raw output (first 500 chars):', stdout.substring(0, 500));
|
| 113 |
-
reject(new Error("Invalid JSON from agent"));
|
| 114 |
-
}
|
| 115 |
-
});
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
| 119 |
});
|
| 120 |
-
}
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
console.log(`🚀 Server running on port ${PORT}`);
|
| 125 |
-
});
|
|
|
|
| 1 |
import express from "express";
|
|
|
|
| 2 |
import dotenv from "dotenv";
|
| 3 |
|
| 4 |
console.log('OpenClaw HF Space: Starting server...');
|
| 5 |
|
| 6 |
+
// Load environment
|
| 7 |
+
dotenv.config();
|
| 8 |
+
console.log('Environment loaded, PORT:', process.env.PORT);
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
const app = express();
|
| 11 |
app.use(express.json());
|
| 12 |
|
| 13 |
+
// Handle JSON parsing errors
|
| 14 |
+
app.use((err, req, res, next) => {
|
| 15 |
+
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
|
| 16 |
+
console.error('JSON parsing error:', err.message);
|
| 17 |
+
return res.status(400).json({ error: "Invalid JSON" });
|
| 18 |
+
}
|
| 19 |
+
next();
|
| 20 |
+
});
|
| 21 |
+
|
| 22 |
+
// Simple endpoints
|
| 23 |
app.get("/", (req, res) => {
|
| 24 |
res.json({
|
| 25 |
status: "OpenClaw HF Space Running",
|
| 26 |
+
endpoints: {
|
| 27 |
+
health: "GET /health",
|
| 28 |
+
market_research: "POST /api/market-research"
|
| 29 |
+
},
|
| 30 |
timestamp: new Date().toISOString()
|
| 31 |
});
|
| 32 |
});
|
|
|
|
| 39 |
});
|
| 40 |
});
|
| 41 |
|
| 42 |
+
// Market research endpoint - SIMPLIFIED
|
| 43 |
+
app.post("/api/market-research", (req, res) => {
|
| 44 |
+
console.log('Received market research request');
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
const { keyword, api_key } = req.body || {};
|
| 47 |
|
| 48 |
if (!keyword) {
|
| 49 |
return res.status(400).json({ error: "keyword is required" });
|
|
|
|
| 56 |
});
|
| 57 |
}
|
| 58 |
|
| 59 |
+
console.log('Processing keyword:', keyword, 'with API key (first 10 chars):', api_key.substring(0, 10));
|
| 60 |
+
|
| 61 |
+
// Return mock response for testing
|
| 62 |
+
const mockResponse = {
|
| 63 |
+
marketTitle: `Global ${keyword} Market Analysis`,
|
| 64 |
+
marketOverview: {
|
| 65 |
+
pastYear_2023: 10.5,
|
| 66 |
+
currentYear_2025: 15.2,
|
| 67 |
+
forecastYear_2033: 28.7,
|
| 68 |
+
global_cagr_Forecast: 12.5
|
| 69 |
+
},
|
| 70 |
+
marketSegments: [
|
| 71 |
+
{
|
| 72 |
+
segment: "Primary Segment",
|
| 73 |
+
segment_marketShare_2023: "35%",
|
| 74 |
+
segment_marketShare_2025: "38%",
|
| 75 |
+
segment_marketShare_2033: "42%",
|
| 76 |
+
segment_cagr: "12%"
|
| 77 |
+
}
|
| 78 |
+
],
|
| 79 |
+
competitiveLandscape: [
|
| 80 |
+
{
|
| 81 |
+
company: "Leading Company",
|
| 82 |
+
player_marketShare_2025: 25
|
| 83 |
+
}
|
| 84 |
+
],
|
| 85 |
+
status: "success",
|
| 86 |
+
message: "Mock data for testing - OpenClaw agent is running!"
|
| 87 |
};
|
| 88 |
|
| 89 |
+
console.log('Returning mock response');
|
| 90 |
+
res.json(mockResponse);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
});
|
| 92 |
|
| 93 |
+
// Error handling middleware
|
| 94 |
+
app.use((err, req, res, next) => {
|
| 95 |
+
console.error('Server error:', err.message);
|
| 96 |
+
res.status(500).json({
|
| 97 |
+
error: "Internal server error",
|
| 98 |
+
message: err.message
|
| 99 |
+
});
|
| 100 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
// Start server
|
| 103 |
+
const PORT = process.env.PORT || 7860;
|
| 104 |
+
console.log('Starting server on port:', PORT);
|
| 105 |
|
| 106 |
+
// Global error handlers to prevent crashes
|
| 107 |
+
process.on('uncaughtException', (error) => {
|
| 108 |
+
console.error('Uncaught Exception:', error.message);
|
| 109 |
+
console.error('Stack:', error.stack);
|
| 110 |
+
});
|
| 111 |
|
| 112 |
+
process.on('unhandledRejection', (reason, promise) => {
|
| 113 |
+
console.error('Unhandled Rejection at:', promise);
|
| 114 |
+
console.error('Reason:', reason);
|
| 115 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
+
try {
|
| 118 |
+
app.listen(PORT, "0.0.0.0", () => {
|
| 119 |
+
console.log(`✅ Server successfully running on port ${PORT}`);
|
| 120 |
+
console.log(`✅ Health endpoint: GET /health`);
|
| 121 |
+
console.log(`✅ API endpoint: POST /api/market-research`);
|
| 122 |
});
|
| 123 |
+
} catch (error) {
|
| 124 |
+
console.error('❌ Failed to start server:', error.message);
|
| 125 |
+
process.exit(1);
|
| 126 |
+
}
|
|
|
|
|
|