| const express = require("express"); |
| const fs = require("fs"); |
| const path = require("path"); |
| const { exec } = require("child_process"); |
|
|
| const app = express(); |
| app.use(express.json()); |
| app.use(express.static(path.join(__dirname, "public"))); |
|
|
| const usersFile = path.join(__dirname, "users.json"); |
| const usersDir = path.join(__dirname, "users"); |
|
|
| |
| if (!fs.existsSync(usersFile)) fs.writeFileSync(usersFile, JSON.stringify([])); |
| if (!fs.existsSync(usersDir)) fs.mkdirSync(usersDir); |
|
|
| |
| function appendLog(username, log) { |
| const userLogsDir = path.join(usersDir, username, "logs.txt"); |
| fs.appendFileSync(userLogsDir, log + "\n"); |
| } |
|
|
| |
| app.post("/signup", (req, res) => { |
| const { username, password } = req.body; |
|
|
| const users = JSON.parse(fs.readFileSync(usersFile)); |
| if (users.some((user) => user.username === username)) { |
| return res.status(400).send("User already exists."); |
| } |
|
|
| users.push({ username, password }); |
| fs.writeFileSync(usersFile, JSON.stringify(users)); |
|
|
| const userDir = path.join(usersDir, username); |
| fs.mkdirSync(userDir); |
| fs.writeFileSync(path.join(userDir, "logs.txt"), ""); |
|
|
| res.send("User signed up successfully."); |
| }); |
|
|
| |
| app.post("/login", (req, res) => { |
| const { username, password } = req.body; |
|
|
| const users = JSON.parse(fs.readFileSync(usersFile)); |
| const user = users.find((u) => u.username === username && u.password === password); |
|
|
| if (!user) { |
| return res.status(400).send("Invalid credentials."); |
| } |
|
|
| res.send("Login successful."); |
| }); |
|
|
| |
| app.post("/execute", (req, res) => { |
| const { username, command } = req.body; |
|
|
| const userDir = path.join(usersDir, username); |
| if (!fs.existsSync(userDir)) { |
| return res.status(400).send("User not found."); |
| } |
|
|
| const child = exec(command, { cwd: userDir }); |
|
|
| child.stdout.on("data", (data) => { |
| appendLog(username, data.trim()); |
| }); |
|
|
| child.stderr.on("data", (data) => { |
| appendLog(username, data.trim()); |
| }); |
|
|
| child.on("close", (code) => { |
| appendLog(username, `Process exited with code ${code}`); |
| }); |
|
|
| res.send("Command executed."); |
| }); |
|
|
| |
| app.get("/logs", (req, res) => { |
| const { username } = req.query; |
|
|
| const userDir = path.join(usersDir, username); |
| if (!fs.existsSync(userDir)) { |
| return res.status(400).send("User not found."); |
| } |
|
|
| const logs = fs.readFileSync(path.join(userDir, "logs.txt"), "utf-8"); |
| res.send(logs); |
| }); |
|
|
| |
| app.post("/deploy", (req, res) => { |
| const { username, repoUrl } = req.body; |
|
|
| const userDir = path.join(usersDir, username); |
| if (!fs.existsSync(userDir)) { |
| return res.status(400).send("User not found."); |
| } |
|
|
| |
| fs.rmdirSync(userDir, { recursive: true }); |
| fs.mkdirSync(userDir); |
|
|
| const cloneCommand = `git clone ${repoUrl} . && yarn install && yarn start`; |
|
|
| const child = exec(cloneCommand, { cwd: userDir }); |
|
|
| child.stdout.on("data", (data) => { |
| appendLog(username, data.trim()); |
| }); |
|
|
| child.stderr.on("data", (data) => { |
| appendLog(username, data.trim()); |
| }); |
|
|
| child.on("close", (code) => { |
| appendLog(username, `Deploy process exited with code ${code}`); |
| }); |
|
|
| res.send("Deployment started."); |
| }); |
|
|
| |
| app.listen(7860, () => { |
| console.log("Server running on http://localhost:3000"); |
| }); |