diff --git a/docker-compose.yml b/docker-compose.yml index e3301acf0..1b77d68fe 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,7 +17,7 @@ services: environment: # Edit the below line to match your PostgresDB URL if you have an outside DB (make sure to update the .env file as well) - DATABASE_URL=postgresql://postgres:testpass@hoppscotch-db:5432/hoppscotch?connect_timeout=300 - - PORT=3170 + - PORT=8080 volumes: # Uncomment the line below when modifying code. Only applicable when using the "dev" target. # - ./packages/hoppscotch-backend/:/usr/src/app @@ -26,6 +26,7 @@ services: hoppscotch-db: condition: service_healthy ports: + - "3180:80" - "3170:3170" # The main hoppscotch app. This will be hosted at port 3000 diff --git a/packages/hoppscotch-backend/Caddyfile b/packages/hoppscotch-backend/Caddyfile new file mode 100644 index 000000000..452e6704f --- /dev/null +++ b/packages/hoppscotch-backend/Caddyfile @@ -0,0 +1,3 @@ +:3170 { + reverse_proxy localhost:80 +} diff --git a/packages/hoppscotch-backend/backend-multiport.Caddyfile b/packages/hoppscotch-backend/backend-multiport.Caddyfile new file mode 100644 index 000000000..72c0efb6b --- /dev/null +++ b/packages/hoppscotch-backend/backend-multiport.Caddyfile @@ -0,0 +1,3 @@ +:80 :3170 { +reverse_proxy localhost:8080 +} diff --git a/packages/hoppscotch-backend/backend-subpath.Caddyfile b/packages/hoppscotch-backend/backend-subpath.Caddyfile new file mode 100644 index 000000000..e39005e98 --- /dev/null +++ b/packages/hoppscotch-backend/backend-subpath.Caddyfile @@ -0,0 +1,7 @@ +:80 :3170 { + handle_path /backend* { + reverse_proxy localhost:8080 + } +} + + diff --git a/packages/hoppscotch-backend/prod_run.mjs b/packages/hoppscotch-backend/prod_run.mjs new file mode 100644 index 000000000..94f79a0a1 --- /dev/null +++ b/packages/hoppscotch-backend/prod_run.mjs @@ -0,0 +1,71 @@ +#!/usr/local/bin/node +// @ts-check + +import { execSync, spawn } from 'child_process'; +import fs from 'fs'; +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.log('error'); + console.log(stuff); + }); + + return childProcess; +} + +const caddyFileName = + process.env.ENABLE_SUBPATH_BASED_ACCESS === 'true' + ? 'backend-subpath.Caddyfile' + : 'backend-multiport.Caddyfile'; +const caddyProcess = runChildProcessWithPrefix( + 'caddy', + ['run', '--config', `/etc/caddy/${caddyFileName}`, '--adapter', 'caddyfile'], + 'App/Admin Dashboard Caddy', +); +const backendProcess = runChildProcessWithPrefix( + 'pnpm', + ['run', 'start:prod'], + '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); +}); diff --git a/prod.Dockerfile b/prod.Dockerfile index 8f50679cb..9994261b4 100644 --- a/prod.Dockerfile +++ b/prod.Dockerfile @@ -13,16 +13,20 @@ RUN pnpm install -f --offline FROM base_builder as backend +RUN apk add caddy WORKDIR /usr/src/app/packages/hoppscotch-backend RUN pnpm exec prisma generate RUN pnpm run build +COPY --from=base_builder /usr/src/app/packages/hoppscotch-backend/backend-subpath.Caddyfile /etc/caddy/backend-subpath.Caddyfile +COPY --from=base_builder /usr/src/app/packages/hoppscotch-backend/backend-multiport.Caddyfile /etc/caddy/backend-multiport.Caddyfile # Remove the env file to avoid backend copying it in and using it RUN rm "../../.env" ENV PRODUCTION="true" -ENV PORT=3170 +ENV PORT=8080 ENV APP_PORT=${PORT} ENV DB_URL=${DATABASE_URL} -CMD ["pnpm", "run", "start:prod"] +CMD ["node", "/usr/src/app/packages/hoppscotch-backend/prod_run.mjs"] +EXPOSE 80 EXPOSE 3170 FROM base_builder as fe_builder