-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
63 lines (53 loc) · 1.91 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const express = require('express');
const os = require('os');
const { startTunnel } = require('untun');
const routes = require('./routes');
const { checkPort } = require('./utils/serverUtils');
const path = require('path');
const fs = require('fs');
const app = express();
// Middleware for JSON processing
app.use(express.json());
// Constants
const CACHE_DIR = path.join(os.tmpdir(), 'git-repo-cache');
// Ensure cache directory exists
if (!fs.existsSync(CACHE_DIR)) {
fs.mkdirSync(CACHE_DIR, { recursive: true });
}
// Setup routes
app.use('/', routes);
const startServer = async (port) => {
try {
const isPortAvailable = await checkPort(port);
if (!isPortAvailable) {
console.log(`Port ${port} is not available. Trying another port...`);
return startServer(port + 1);
}
const server = app.listen(port, '0.0.0.0', async () => {
console.log(`Server is running on port ${port}`);
// try {
// const tunnel = await startTunnel({ port: port });
// if (tunnel && tunnel.getURL) {
// const tunnelURL = await tunnel.getURL();
// console.log(`Tunnel created at ${tunnelURL}`);
// } else {
// console.error('Failed to retrieve tunnel URL');
// }
// } catch (error) {
// console.error('Error creating tunnel', error);
// }
});
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.log(`Port ${port} is already in use. Trying another port...`);
startServer(port + 1);
} else {
console.error('Error starting server:', err);
}
});
} catch (error) {
console.error('Error starting server:', error);
}
};
const PORT = process.env.PORT || 8084;
startServer(PORT);