-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.cjs
More file actions
36 lines (30 loc) · 1006 Bytes
/
server.cjs
File metadata and controls
36 lines (30 loc) · 1006 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const { execSync } = require('child_process');
const http = require('http');
const PORT = 3001;
function runCmd(cmd) {
try {
return execSync(cmd, { timeout: 15000, encoding: 'utf-8' });
} catch (e) {
return e.stdout || e.message;
}
}
function tryJson(raw) {
try { return JSON.parse(raw); } catch { return { raw }; }
}
const server = http.createServer((req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Content-Type', 'application/json');
if (req.url === '/api/status') {
const raw = runCmd('openclaw status --all --json 2>/dev/null || openclaw status --all 2>&1');
res.end(JSON.stringify(tryJson(raw)));
} else if (req.url === '/api/health') {
const raw = runCmd('openclaw health --json 2>&1');
res.end(JSON.stringify(tryJson(raw)));
} else {
res.statusCode = 404;
res.end(JSON.stringify({ error: 'not found' }));
}
});
server.listen(PORT, () => {
console.log(`API server running on http://localhost:${PORT}`);
});