diff --git a/striker-ui-api/src/index.ts b/striker-ui-api/src/index.ts index 51c13841..3f49f986 100644 --- a/striker-ui-api/src/index.ts +++ b/striker-ui-api/src/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 { 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, () => { - console.log(`Listening on localhost:${SERVER_PORT}.`); + stdout(`Listening on localhost:${PORT}.`); }); diff --git a/striker-ui-api/src/lib/consts/PROCESS_OWNER.ts b/striker-ui-api/src/lib/consts/PROCESS_OWNER.ts new file mode 100644 index 00000000..6b118789 --- /dev/null +++ b/striker-ui-api/src/lib/consts/PROCESS_OWNER.ts @@ -0,0 +1,2 @@ +export const PUID = process.env.PUID ?? 'striker-ui-api'; +export const PGID = process.env.PGID ?? PUID; diff --git a/striker-ui-api/src/lib/consts/SERVER_PORT.ts b/striker-ui-api/src/lib/consts/SERVER_PORT.ts index db7f5f0e..f668d4c2 100644 --- a/striker-ui-api/src/lib/consts/SERVER_PORT.ts +++ b/striker-ui-api/src/lib/consts/SERVER_PORT.ts @@ -1,3 +1 @@ -const SERVER_PORT = process.env.SERVER_PORT ?? 8080; - -export default SERVER_PORT; +export const PORT = process.env.PORT ?? 8080; diff --git a/striker-ui-api/src/lib/consts/index.ts b/striker-ui-api/src/lib/consts/index.ts new file mode 100644 index 00000000..fa9190c4 --- /dev/null +++ b/striker-ui-api/src/lib/consts/index.ts @@ -0,0 +1,2 @@ +export * from './PROCESS_OWNER'; +export * from './SERVER_PORT';