getzero11 commited on
Commit
8a9cf4b
·
verified ·
1 Parent(s): b6b2121

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +47 -13
server.js CHANGED
@@ -1,39 +1,73 @@
1
  const express = require("express");
2
  const { spawn } = require("child_process");
 
3
 
4
  const app = express();
5
  app.use(express.json());
6
 
7
  const PORT = process.env.PORT || 7860;
8
 
 
 
 
9
  app.get("/", (req, res) => {
10
  res.send("✅ Moltbot is running on HF Space");
11
  });
12
 
 
 
 
 
 
 
 
 
 
 
 
13
  app.post("/run", (req, res) => {
14
- const args = req.body.args || [];
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- const moltbot = spawn(
17
- "npx",
18
- ["moltbot", ...args],
19
- { shell: true }
 
 
 
20
  );
21
 
22
- let output = "";
 
23
 
24
- moltbot.stdout.on("data", data => {
25
- output += data.toString();
26
  });
27
 
28
- moltbot.stderr.on("data", data => {
29
- output += data.toString();
30
  });
31
 
32
- moltbot.on("close", () => {
33
- res.json({ output });
 
 
 
 
34
  });
35
  });
36
 
37
  app.listen(PORT, "0.0.0.0", () => {
38
- console.log(`🚀 Moltbot server listening on ${PORT}`);
39
  });
 
1
  const express = require("express");
2
  const { spawn } = require("child_process");
3
+ const path = require("path");
4
 
5
  const app = express();
6
  app.use(express.json());
7
 
8
  const PORT = process.env.PORT || 7860;
9
 
10
+ /**
11
+ * Health check (important for HF)
12
+ */
13
  app.get("/", (req, res) => {
14
  res.send("✅ Moltbot is running on HF Space");
15
  });
16
 
17
+ app.get("/health", (req, res) => {
18
+ res.json({ ok: true });
19
+ });
20
+
21
+ /**
22
+ * Run Moltbot
23
+ * Body:
24
+ * {
25
+ * "args": ["--help"]
26
+ * }
27
+ */
28
  app.post("/run", (req, res) => {
29
+ const args = Array.isArray(req.body?.args) ? req.body.args : [];
30
+
31
+ let moltbotEntry;
32
+ try {
33
+ // Resolve Moltbot's actual JS entry file
34
+ moltbotEntry = require.resolve("moltbot");
35
+ } catch (err) {
36
+ return res.status(500).json({
37
+ error: "Failed to resolve Moltbot entry file",
38
+ details: err.message
39
+ });
40
+ }
41
 
42
+ const child = spawn(
43
+ "node",
44
+ [moltbotEntry, ...args],
45
+ {
46
+ cwd: process.cwd(),
47
+ env: process.env
48
+ }
49
  );
50
 
51
+ let stdout = "";
52
+ let stderr = "";
53
 
54
+ child.stdout.on("data", (data) => {
55
+ stdout += data.toString();
56
  });
57
 
58
+ child.stderr.on("data", (data) => {
59
+ stderr += data.toString();
60
  });
61
 
62
+ child.on("close", (code) => {
63
+ res.json({
64
+ exitCode: code,
65
+ stdout,
66
+ stderr
67
+ });
68
  });
69
  });
70
 
71
  app.listen(PORT, "0.0.0.0", () => {
72
+ console.log(`🚀 Moltbot server listening on port ${PORT}`);
73
  });