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