Skip to content

Commit a186df6

Browse files
authored
Update app.js
1 parent 4d38565 commit a186df6

File tree

1 file changed

+20
-38
lines changed

1 file changed

+20
-38
lines changed

app.js

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
const express = require('express');
22
const bodyParser = require('body-parser');
3-
const cors = require('cors');
43
const { exec } = require('child_process');
54
const path = require('path');
65
const fs = require('fs');
7-
const mime = require('mime-types'); // To detect mime type based on file extension
6+
const axios = require('axios'); // For making HTTP requests
87

98
const app = express();
109
const PORT = 3000;
1110

12-
// Middleware to enable CORS
13-
app.use(cors());
14-
1511
// Middleware to parse JSON
1612
app.use(bodyParser.json());
1713

@@ -23,17 +19,12 @@ if (!fs.existsSync(WEBSITES_DIR)) {
2319
fs.mkdirSync(WEBSITES_DIR);
2420
}
2521

26-
// Serve webhosting.html on the root route
27-
app.get('/', (req, res) => {
28-
res.sendFile(path.join(__dirname, 'webhosting.html'));
29-
});
30-
3122
// Serve static files from the websites folder
3223
app.use('/websites', express.static(WEBSITES_DIR));
3324

3425
// POST endpoint to receive GitHub URL
35-
app.post('/clone-repo', (req, res) => {
36-
const { repoUrl } = req.body;
26+
app.post('/clone-repo', async (req, res) => {
27+
const { repoUrl, deployUrl } = req.body; // Expect deployUrl for auto deployment
3728

3829
// Validate the GitHub URL
3930
if (!repoUrl || !repoUrl.startsWith('https://github.com/')) {
@@ -57,42 +48,33 @@ app.post('/clone-repo', (req, res) => {
5748
const cloneCommand = `git clone ${repoUrl} ${repoPath}`;
5849

5950
// Execute git clone command
60-
exec(cloneCommand, (error, stdout, stderr) => {
51+
exec(cloneCommand, async (error, stdout, stderr) => {
6152
if (error) {
6253
console.error(`Error cloning repo: ${stderr}`);
6354
return res.status(500).json({ error: 'Error cloning the repository.' });
6455
}
6556

6657
console.log(`Repository cloned successfully to ${repoPath}`);
6758

68-
// Generate a list of files in the cloned repository
69-
const files = [];
70-
function walkDir(dir) {
71-
const items = fs.readdirSync(dir);
72-
73-
items.forEach(item => {
74-
const itemPath = path.join(dir, item);
75-
const stats = fs.statSync(itemPath);
76-
77-
if (stats.isDirectory()) {
78-
walkDir(itemPath); // Recursively explore directories
79-
} else {
80-
// Store file path relative to the repo
81-
files.push(itemPath.replace(repoPath + '/', ''));
82-
}
59+
// Trigger deployment (to Render or another platform)
60+
try {
61+
const deployResponse = await axios.post(deployUrl, {
62+
repoName, // You can send additional info if needed
8363
});
84-
}
8564

86-
walkDir(repoPath); // Start walking the cloned repo folder
65+
console.log('Deployment triggered:', deployResponse.data);
8766

88-
// Return success response with URL to access the cloned repo and file paths
89-
const repoUrlPath = `https://sch-ai1z.onrender.com/websites/${repoName}`;
90-
res.status(200).json({
91-
message: 'Repository cloned successfully!',
92-
folderPath: repoPath,
93-
repoUrl: repoUrlPath,
94-
files: files, // List of files in the repo
95-
});
67+
// Construct the URL for the website hosted at Render
68+
const websiteUrl = `https://sch-ai1z.onrender.com/websites/${repoName}`;
69+
70+
res.status(200).json({
71+
message: 'Repository cloned and deployment triggered!',
72+
repoUrl: websiteUrl,
73+
});
74+
} catch (err) {
75+
console.error('Error triggering deployment:', err);
76+
res.status(500).json({ error: 'Error triggering deployment.' });
77+
}
9678
});
9779
});
9880

0 commit comments

Comments
 (0)