File size: 2,110 Bytes
20dac45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
// Lightweight starter for Render (Web + API + Gateway only, <512MB RAM)
const { spawn } = require('node:child_process');
const path = require('node:path');

const children = new Map();
let stopping = false;

function launch(name, bin, args, env = {}) {
  const p = spawn(bin, args, { stdio: 'inherit', env: { ...process.env, ...env } });
  children.set(name, p);
  p.on('error', (err) => {
    console.error([] failed to start:, err);
    void stop(1);
  });
  p.on('exit', (code) => {
    children.delete(name);
    if (!stopping && code !== 0) {
      console.error([] exited with code ; shutting down.);
      void stop(code || 1);
    }
  });
  return p;
}

async function stop(code = 0) {
  if (stopping) return;
  stopping = true;
  console.log('Shutting down services...');
  for (const [name, p] of children) {
    p.kill('SIGTERM');
  }
  setTimeout(() => process.exit(code), 5000);
}

process.on('SIGINT', () => void stop());
process.on('SIGTERM', () => void stop());

async function main() {
  console.log('=== Starting Yoru on Render (Playback & Web Mode) ===');

  const port = process.env.PORT || '3000';
  const apiPort = process.env.API_PORT || '4000';
  const gatewayPort = process.env.GATEWAY_PORT || '4001';

  // 1. Launch NestJS API on localhost:4000
  console.log(Starting API on 127.0.0.1:...);
  launch('api', process.execPath, ['dist/server/main.js'], {
    PORT: apiPort,
    HOST: '127.0.0.1'
  });

  // 2. Launch Media Gateway on localhost:4001
  console.log(Starting Media Gateway on 127.0.0.1:...);
  launch('gateway', process.execPath, ['dist/server/gateway.js'], {
    GATEWAY_PORT: gatewayPort,
    HOST: '127.0.0.1'
  });

  // 3. Launch Next.js Web on public Render PORT
  console.log(Starting Next.js frontend on port ...);
  const nextBin = path.join(__dirname, '../node_modules/next/dist/bin/next');
  launch('web', process.execPath, [nextBin, 'start', '-p', port, '-H', '0.0.0.0'], {
    API_INTERNAL_URL: http://127.0.0.1:,
    GATEWAY_INTERNAL_URL: http://127.0.0.1:
  });
}

main().catch((err) => {
  console.error('Fatal start error:', err);
  process.exit(1);
});