fix(striker-ui-api): change process ownership after port binding

main
Tsu-ba-me 2 years ago
parent fc73f4ec15
commit 67b292dd95
  1. 23
      striker-ui-api/src/index.ts
  2. 2
      striker-ui-api/src/lib/consts/PROCESS_OWNER.ts
  3. 4
      striker-ui-api/src/lib/consts/SERVER_PORT.ts
  4. 2
      striker-ui-api/src/lib/consts/index.ts

@ -1,7 +1,24 @@
import { getgid, getuid, setgid, setuid } from 'process';
import { PGID, PUID, PORT } from './lib/consts';
import app from './app'; import app from './app';
import { stderr, stdout } from './lib/shell';
stdout(`Starting process with ownership ${getuid()}:${getgid()}`);
app.listen(PORT, () => {
try {
// Group must be set before user to avoid permission error.
setgid(PGID);
setuid(PUID);
stdout(`Process ownership changed to ${getuid()}:${getgid()}.`);
} catch (error) {
stderr(`Failed to change process ownership; CAUSE: ${error}`);
import SERVER_PORT from './lib/consts/SERVER_PORT'; process.exit(1);
}
app.listen(SERVER_PORT, () => { stdout(`Listening on localhost:${PORT}.`);
console.log(`Listening on localhost:${SERVER_PORT}.`);
}); });

@ -0,0 +1,2 @@
export const PUID = process.env.PUID ?? 'striker-ui-api';
export const PGID = process.env.PGID ?? PUID;

@ -1,3 +1 @@
const SERVER_PORT = process.env.SERVER_PORT ?? 8080; export const PORT = process.env.PORT ?? 8080;
export default SERVER_PORT;

@ -0,0 +1,2 @@
export * from './PROCESS_OWNER';
export * from './SERVER_PORT';
Loading…
Cancel
Save