65 lines
1.8 KiB
JavaScript
65 lines
1.8 KiB
JavaScript
const express = require('express');
|
|
const cors = require('cors');
|
|
const app = express();
|
|
const bodyParser = require('body-parser');
|
|
const checkPermissions = require('./middlewares/check-permissions');
|
|
const modifyPath = require('./middlewares/modify-path');
|
|
const VCS = require('./services/vcs');
|
|
|
|
const executorRoutes = require('./routes/executor');
|
|
const vcsRoutes = require('./routes/vcs');
|
|
|
|
async function initAndCheckRepo() {
|
|
try {
|
|
const projectId = '31986';
|
|
const result = await VCS.initRepo(projectId);
|
|
console.log(result?.message ? result.message : result);
|
|
return true;
|
|
} catch (err) {
|
|
console.error('Error during repo initialization:', err);
|
|
throw new Error('Critical error: Git repository initialization failed.');
|
|
}
|
|
}
|
|
|
|
async function startServer() {
|
|
const PORT = process.env.APP_SHELL_PORT || 4000;
|
|
try {
|
|
await initAndCheckRepo();
|
|
|
|
return new Promise((resolve, reject) => {
|
|
app.listen(PORT, (err) => {
|
|
if (err) {
|
|
console.error(`Failed to listen on port ${PORT}:`, err);
|
|
return reject(err);
|
|
}
|
|
console.log(`Listening on port ${PORT}`);
|
|
resolve();
|
|
});
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error(`[ERROR] Server startup failed due to: ${error.message}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
app.use(cors({ origin: true }));
|
|
app.use(bodyParser.json());
|
|
app.use(checkPermissions);
|
|
app.use(modifyPath);
|
|
|
|
app.use('/executor', executorRoutes);
|
|
app.use('/vcs', vcsRoutes);
|
|
|
|
(async () => {
|
|
try {
|
|
await startServer();
|
|
console.log("App-shell server is fully ready!");
|
|
} catch (error) {
|
|
console.error("App-shell failed to start completely:", error);
|
|
process.exit(1);
|
|
}
|
|
})();
|
|
|
|
module.exports = app;
|