hoppscotch/packages/hoppscotch-backend/prod_run.mjs
Andrew Bastin afea75694f
refactor: slim down the backend containers (#4233)
* refactor: slim down the backend containers

* refactor: make containers run as non-root user in container

* chore: correct casing for the build stage definitions

* chore: remove docker compose version field as its obsolete

* chore: optimise chown and chmod into the COPY command itself

* chore: add package overrides for packages with reported vulns

* chore: add pnpm to containers + set workdir dir to the backend project

* fix: permission issues with the fe containers

* chore: define env variables on AIO
2024-08-08 11:31:13 +05:30

67 lines
1.5 KiB
JavaScript

#!/usr/local/bin/node
// @ts-check
import { spawn } from 'child_process';
import process from 'process';
function runChildProcessWithPrefix(command, args, prefix) {
const childProcess = spawn(command, args);
childProcess.stdout.on('data', (data) => {
const output = data.toString().trim().split('\n');
output.forEach((line) => {
console.log(`${prefix} | ${line}`);
});
});
childProcess.stderr.on('data', (data) => {
const error = data.toString().trim().split('\n');
error.forEach((line) => {
console.error(`${prefix} | ${line}`);
});
});
childProcess.on('close', (code) => {
console.log(`${prefix} Child process exited with code ${code}`);
});
childProcess.on('error', (stuff) => {
console.error('error');
console.error(stuff);
});
return childProcess;
}
const caddyProcess = runChildProcessWithPrefix(
'caddy',
['run', '--config', '/etc/caddy/backend.Caddyfile', '--adapter', 'caddyfile'],
'App/Admin Dashboard Caddy',
);
const backendProcess = runChildProcessWithPrefix(
'node',
['/dist/backend/dist/main.js'],
'Backend Server',
);
caddyProcess.on('exit', (code) => {
console.log(`Exiting process because Caddy Server exited with code ${code}`);
process.exit(code);
});
backendProcess.on('exit', (code) => {
console.log(
`Exiting process because Backend Server exited with code ${code}`,
);
process.exit(code);
});
process.on('SIGINT', () => {
console.log('SIGINT received, exiting...');
caddyProcess.kill('SIGINT');
backendProcess.kill('SIGINT');
process.exit(0);
});