1
1
const express = require ( 'express' ) ;
2
2
const bodyParser = require ( 'body-parser' ) ;
3
+ const cors = require ( 'cors' ) ;
3
4
const { exec } = require ( 'child_process' ) ;
4
5
const path = require ( 'path' ) ;
5
6
const fs = require ( 'fs' ) ;
6
- const axios = require ( 'axios' ) ; // For making HTTP requests
7
7
8
8
const app = express ( ) ;
9
9
const PORT = 3000 ;
10
10
11
+ // Middleware to enable CORS
12
+ app . use ( cors ( ) ) ;
13
+
11
14
// Middleware to parse JSON
12
15
app . use ( bodyParser . json ( ) ) ;
13
16
@@ -19,12 +22,17 @@ if (!fs.existsSync(WEBSITES_DIR)) {
19
22
fs . mkdirSync ( WEBSITES_DIR ) ;
20
23
}
21
24
25
+ // Serve webhosting.html on the root route
26
+ app . get ( '/' , ( req , res ) => {
27
+ res . sendFile ( path . join ( __dirname , 'webhosting.html' ) ) ;
28
+ } ) ;
29
+
22
30
// Serve static files from the websites folder
23
31
app . use ( '/websites' , express . static ( WEBSITES_DIR ) ) ;
24
32
25
33
// 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 ;
28
36
29
37
// Validate the GitHub URL
30
38
if ( ! repoUrl || ! repoUrl . startsWith ( 'https://github.com/' ) ) {
@@ -48,33 +56,33 @@ app.post('/clone-repo', async (req, res) => {
48
56
const cloneCommand = `git clone ${ repoUrl } ${ repoPath } ` ;
49
57
50
58
// Execute git clone command
51
- exec ( cloneCommand , async ( error , stdout , stderr ) => {
59
+ exec ( cloneCommand , ( error , stdout , stderr ) => {
52
60
if ( error ) {
53
61
console . error ( `Error cloning repo: ${ stderr } ` ) ;
54
62
return res . status ( 500 ) . json ( { error : 'Error cloning the repository.' } ) ;
55
63
}
56
64
57
65
console . log ( `Repository cloned successfully to ${ repoPath } ` ) ;
58
66
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' ) ;
64
69
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` ;
66
72
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
77
76
}
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
+ } ) ;
78
86
} ) ;
79
87
} ) ;
80
88
0 commit comments