Reaperxxxx commited on
Commit
2c0bd38
·
verified ·
1 Parent(s): 340c655

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +65 -47
server.js CHANGED
@@ -17,8 +17,20 @@ if (!fs.existsSync(usersDir)) fs.mkdirSync(usersDir);
17
 
18
  // Helper to write logs
19
  function appendLog(username, log) {
20
- const userLogsDir = path.join(usersDir, username, "logs.txt");
21
- fs.appendFileSync(userLogsDir, log + "\n");
 
 
 
 
 
 
 
 
 
 
 
 
22
  }
23
 
24
  // Endpoint to handle user sign-up
@@ -62,33 +74,37 @@ app.post("/signup", async (req, res) => {
62
  });
63
 
64
  // Endpoint to handle terminal commands
65
- app.post("/execute", (req, res) => {
66
- const { username, command } = req.body;
67
 
68
- const userDir = path.join(usersDir, username);
69
- if (!fs.existsSync(userDir)) {
70
- return res.status(400).send("User not found.");
71
- }
 
 
 
 
72
 
73
- const child = exec(command, { cwd: userDir });
74
 
75
- child.stdout.on("data", (data) => {
76
- appendLog(username, data.trim());
77
- });
78
 
79
- child.stderr.on("data", (data) => {
80
- appendLog(username, data.trim());
81
- });
82
 
83
- child.on("close", (code) => {
84
- appendLog(username, `Process exited with code ${code}`);
85
- });
86
 
87
- res.send("Command executed.");
88
  });
89
 
90
  // Endpoint to get user logs
91
- app.get("/logs", (req, res) => {
92
  const { username } = req.query;
93
 
94
  if (!username) {
@@ -98,49 +114,51 @@ app.get("/logs", (req, res) => {
98
  const userDir = path.join(usersDir, username);
99
  const logsFile = path.join(userDir, "logs.txt");
100
 
101
- if (!fs.existsSync(logsFile)) {
102
- return res.status(404).send("Logs not found.");
103
- }
104
-
105
  try {
106
- const logs = fs.readFileSync(logsFile, "utf8");
107
  res.send(logs);
108
  } catch (error) {
109
  console.error("Error fetching logs:", error);
110
- res.status(500).send("Failed to fetch logs.");
111
  }
112
  });
113
 
114
  // Endpoint for "Deploy Kaizen"
115
- app.post("/deploy", (req, res) => {
116
- const { username, repoUrl } = req.body;
117
 
118
- const userDir = path.join(usersDir, username);
119
- if (!fs.existsSync(userDir)) {
120
- return res.status(400).send("User not found.");
121
- }
122
 
123
- // Clear user directory
124
- fs.rmdirSync(userDir, { recursive: true });
125
- fs.mkdirSync(userDir);
 
 
126
 
127
- const cloneCommand = `git clone ${repoUrl} . && yarn install && yarn start`;
 
128
 
129
- const child = exec(cloneCommand, { cwd: userDir });
130
 
131
- child.stdout.on("data", (data) => {
132
- appendLog(username, data.trim());
133
- });
134
 
135
- child.stderr.on("data", (data) => {
136
- appendLog(username, data.trim());
137
- });
138
 
139
- child.on("close", (code) => {
140
- appendLog(username, `Deploy process exited with code ${code}`);
141
- });
142
 
143
- res.send("Deployment started.");
 
 
 
 
144
  });
145
 
146
  // Start the server
 
17
 
18
  // Helper to write logs
19
  function appendLog(username, log) {
20
+ const userDir = path.join(usersDir, username);
21
+ const logFile = path.join(userDir, "logs.txt");
22
+
23
+ try {
24
+ // Ensure the user directory exists
25
+ if (!fs.existsSync(userDir)) {
26
+ fs.mkdirSync(userDir, { recursive: true });
27
+ }
28
+
29
+ // Append the log to logs.txt
30
+ fs.appendFileSync(logFile, log + "\n");
31
+ } catch (error) {
32
+ console.error(`Error appending log for user ${username}:`, error);
33
+ }
34
  }
35
 
36
  // Endpoint to handle user sign-up
 
74
  });
75
 
76
  // Endpoint to handle terminal commands
77
+ app.post("/execute", async (req, res) => {
78
+ const { username, command } = req.body;
79
 
80
+ if (!username || !command) {
81
+ return res.status(400).send("Username and command are required.");
82
+ }
83
+
84
+ const userDir = path.join(usersDir, username);
85
+ if (!await fs.access(userDir).catch(() => false)) {
86
+ return res.status(400).send("User directory not found.");
87
+ }
88
 
89
+ const child = exec(command, { cwd: userDir, shell: "/bin/bash" });
90
 
91
+ child.stdout.on("data", (data) => {
92
+ appendLog(username, `STDOUT: ${data.trim()}`);
93
+ });
94
 
95
+ child.stderr.on("data", (data) => {
96
+ appendLog(username, `STDERR: ${data.trim()}`);
97
+ });
98
 
99
+ child.on("close", (code) => {
100
+ appendLog(username, `Process exited with code ${code}`);
101
+ });
102
 
103
+ res.send("Command execution started.");
104
  });
105
 
106
  // Endpoint to get user logs
107
+ app.get("/logs", async (req, res) => {
108
  const { username } = req.query;
109
 
110
  if (!username) {
 
114
  const userDir = path.join(usersDir, username);
115
  const logsFile = path.join(userDir, "logs.txt");
116
 
 
 
 
 
117
  try {
118
+ const logs = await fs.readFile(logsFile, "utf8");
119
  res.send(logs);
120
  } catch (error) {
121
  console.error("Error fetching logs:", error);
122
+ res.status(404).send("Logs not found.");
123
  }
124
  });
125
 
126
  // Endpoint for "Deploy Kaizen"
127
+ app.post("/deploy", async (req, res) => {
128
+ const { username, repoUrl } = req.body;
129
 
130
+ if (!username || !repoUrl) {
131
+ return res.status(400).send("Username and repository URL are required.");
132
+ }
 
133
 
134
+ const userDir = path.join(usersDir, username);
135
+ try {
136
+ // Clear user directory
137
+ await fs.rmdir(userDir, { recursive: true }).catch(() => {});
138
+ await fs.mkdir(userDir, { recursive: true });
139
 
140
+ // Clone repository and install dependencies
141
+ const cloneCommand = `git clone ${repoUrl} . && /usr/bin/yarn install && /usr/bin/yarn start`;
142
 
143
+ const child = exec(cloneCommand, { cwd: userDir, shell: "/bin/bash" });
144
 
145
+ child.stdout.on("data", (data) => {
146
+ appendLog(username, `STDOUT: ${data.trim()}`);
147
+ });
148
 
149
+ child.stderr.on("data", (data) => {
150
+ appendLog(username, `STDERR: ${data.trim()}`);
151
+ });
152
 
153
+ child.on("close", (code) => {
154
+ appendLog(username, `Deploy process exited with code ${code}`);
155
+ });
156
 
157
+ res.send("Deployment started.");
158
+ } catch (error) {
159
+ console.error("Error during deployment:", error);
160
+ res.status(500).send("Failed to start deployment.");
161
+ }
162
  });
163
 
164
  // Start the server