Create server.js
Browse files
server.js
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const express = require("express");
|
2 |
+
const fs = require("fs");
|
3 |
+
const path = require("path");
|
4 |
+
const { exec } = require("child_process");
|
5 |
+
|
6 |
+
const app = express();
|
7 |
+
app.use(express.json());
|
8 |
+
app.use(express.static(path.join(__dirname, "public")));
|
9 |
+
|
10 |
+
const usersFile = path.join(__dirname, "users.json");
|
11 |
+
const usersDir = path.join(__dirname, "users");
|
12 |
+
|
13 |
+
// Ensure `users.json` and `users/` exist
|
14 |
+
if (!fs.existsSync(usersFile)) fs.writeFileSync(usersFile, JSON.stringify([]));
|
15 |
+
if (!fs.existsSync(usersDir)) fs.mkdirSync(usersDir);
|
16 |
+
|
17 |
+
// Helper to write logs
|
18 |
+
function appendLog(username, log) {
|
19 |
+
const userLogsDir = path.join(usersDir, username, "logs.txt");
|
20 |
+
fs.appendFileSync(userLogsDir, log + "\n");
|
21 |
+
}
|
22 |
+
|
23 |
+
// Endpoint to handle user sign-up
|
24 |
+
app.post("/signup", (req, res) => {
|
25 |
+
const { username, password } = req.body;
|
26 |
+
|
27 |
+
const users = JSON.parse(fs.readFileSync(usersFile));
|
28 |
+
if (users.some((user) => user.username === username)) {
|
29 |
+
return res.status(400).send("User already exists.");
|
30 |
+
}
|
31 |
+
|
32 |
+
users.push({ username, password });
|
33 |
+
fs.writeFileSync(usersFile, JSON.stringify(users));
|
34 |
+
|
35 |
+
const userDir = path.join(usersDir, username);
|
36 |
+
fs.mkdirSync(userDir);
|
37 |
+
fs.writeFileSync(path.join(userDir, "logs.txt"), "");
|
38 |
+
|
39 |
+
res.send("User signed up successfully.");
|
40 |
+
});
|
41 |
+
|
42 |
+
// Endpoint to handle login
|
43 |
+
app.post("/login", (req, res) => {
|
44 |
+
const { username, password } = req.body;
|
45 |
+
|
46 |
+
const users = JSON.parse(fs.readFileSync(usersFile));
|
47 |
+
const user = users.find((u) => u.username === username && u.password === password);
|
48 |
+
|
49 |
+
if (!user) {
|
50 |
+
return res.status(400).send("Invalid credentials.");
|
51 |
+
}
|
52 |
+
|
53 |
+
res.send("Login successful.");
|
54 |
+
});
|
55 |
+
|
56 |
+
// Endpoint to handle terminal commands
|
57 |
+
app.post("/execute", (req, res) => {
|
58 |
+
const { username, command } = req.body;
|
59 |
+
|
60 |
+
const userDir = path.join(usersDir, username);
|
61 |
+
if (!fs.existsSync(userDir)) {
|
62 |
+
return res.status(400).send("User not found.");
|
63 |
+
}
|
64 |
+
|
65 |
+
const child = exec(command, { cwd: userDir });
|
66 |
+
|
67 |
+
child.stdout.on("data", (data) => {
|
68 |
+
appendLog(username, data.trim());
|
69 |
+
});
|
70 |
+
|
71 |
+
child.stderr.on("data", (data) => {
|
72 |
+
appendLog(username, data.trim());
|
73 |
+
});
|
74 |
+
|
75 |
+
child.on("close", (code) => {
|
76 |
+
appendLog(username, `Process exited with code ${code}`);
|
77 |
+
});
|
78 |
+
|
79 |
+
res.send("Command executed.");
|
80 |
+
});
|
81 |
+
|
82 |
+
// Endpoint to get user logs
|
83 |
+
app.get("/logs", (req, res) => {
|
84 |
+
const { username } = req.query;
|
85 |
+
|
86 |
+
const userDir = path.join(usersDir, username);
|
87 |
+
if (!fs.existsSync(userDir)) {
|
88 |
+
return res.status(400).send("User not found.");
|
89 |
+
}
|
90 |
+
|
91 |
+
const logs = fs.readFileSync(path.join(userDir, "logs.txt"), "utf-8");
|
92 |
+
res.send(logs);
|
93 |
+
});
|
94 |
+
|
95 |
+
// Endpoint for "Deploy Kaizen"
|
96 |
+
app.post("/deploy", (req, res) => {
|
97 |
+
const { username, repoUrl } = req.body;
|
98 |
+
|
99 |
+
const userDir = path.join(usersDir, username);
|
100 |
+
if (!fs.existsSync(userDir)) {
|
101 |
+
return res.status(400).send("User not found.");
|
102 |
+
}
|
103 |
+
|
104 |
+
// Clear user directory
|
105 |
+
fs.rmdirSync(userDir, { recursive: true });
|
106 |
+
fs.mkdirSync(userDir);
|
107 |
+
|
108 |
+
const cloneCommand = `git clone ${repoUrl} . && yarn install && yarn start`;
|
109 |
+
|
110 |
+
const child = exec(cloneCommand, { cwd: userDir });
|
111 |
+
|
112 |
+
child.stdout.on("data", (data) => {
|
113 |
+
appendLog(username, data.trim());
|
114 |
+
});
|
115 |
+
|
116 |
+
child.stderr.on("data", (data) => {
|
117 |
+
appendLog(username, data.trim());
|
118 |
+
});
|
119 |
+
|
120 |
+
child.on("close", (code) => {
|
121 |
+
appendLog(username, `Deploy process exited with code ${code}`);
|
122 |
+
});
|
123 |
+
|
124 |
+
res.send("Deployment started.");
|
125 |
+
});
|
126 |
+
|
127 |
+
// Start the server
|
128 |
+
app.listen(7860, () => {
|
129 |
+
console.log("Server running on http://localhost:3000");
|
130 |
+
});
|