Skip to content

Commit 0b235d9

Browse files
authored
Update app.js
1 parent a186df6 commit 0b235d9

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

app.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
const express = require('express');
22
const bodyParser = require('body-parser');
3+
const cors = require('cors');
34
const { exec } = require('child_process');
45
const path = require('path');
56
const fs = require('fs');
6-
const axios = require('axios'); // For making HTTP requests
77

88
const app = express();
99
const PORT = 3000;
1010

11+
// Middleware to enable CORS
12+
app.use(cors());
13+
1114
// Middleware to parse JSON
1215
app.use(bodyParser.json());
1316

@@ -19,12 +22,17 @@ if (!fs.existsSync(WEBSITES_DIR)) {
1922
fs.mkdirSync(WEBSITES_DIR);
2023
}
2124

25+
// Serve webhosting.html on the root route
26+
app.get('/', (req, res) => {
27+
res.sendFile(path.join(__dirname, 'webhosting.html'));
28+
});
29+
2230
// Serve static files from the websites folder
2331
app.use('/websites', express.static(WEBSITES_DIR));
2432

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

2937
// Validate the GitHub URL
3038
if (!repoUrl || !repoUrl.startsWith('https://github.com/')) {
@@ -48,33 +56,33 @@ app.post('/clone-repo', async (req, res) => {
4856
const cloneCommand = `git clone ${repoUrl} ${repoPath}`;
4957

5058
// Execute git clone command
51-
exec(cloneCommand, async (error, stdout, stderr) => {
59+
exec(cloneCommand, (error, stdout, stderr) => {
5260
if (error) {
5361
console.error(`Error cloning repo: ${stderr}`);
5462
return res.status(500).json({ error: 'Error cloning the repository.' });
5563
}
5664

5765
console.log(`Repository cloned successfully to ${repoPath}`);
5866

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
63-
});
67+
// Check if there is an index.html in the repo
68+
const indexHtmlPath = path.join(repoPath, 'index.html');
6469

65-
console.log('Deployment triggered:', deployResponse.data);
70+
// Set the redirect URL based on whether index.html exists
71+
let redirectUrl = `/websites/${repoName}/index.html`;
6672

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.' });
73+
// If there's no index.html, we can redirect to a general directory or handle accordingly
74+
if (!fs.existsSync(indexHtmlPath)) {
75+
redirectUrl = `/websites/${repoName}`; // Default fallback
7776
}
77+
78+
// Return success response with URL to access the cloned repo
79+
const repoUrlPath = `https://sch-ai1z.onrender.com/websites/${repoName}`;
80+
res.status(200).json({
81+
message: 'Repository cloned successfully!',
82+
folderPath: repoPath,
83+
repoUrl: repoUrlPath,
84+
redirectUrl: redirectUrl, // Return the redirect URL with the correct file path
85+
});
7886
});
7987
});
8088

0 commit comments

Comments
 (0)