diff --git a/src/api/index.ts b/src/api/index.ts index 960264fd..c416260a 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -2,7 +2,6 @@ import express from 'express' import adminRouter from './admin' import mappingRouter from './mapping' import sshKeyRouter from './sshKeys' -import statusRouter from './serverStatus' import { getAvailableDomains } from '../lib/data' const apiRouter = express.Router() @@ -10,7 +9,6 @@ const apiRouter = express.Router() apiRouter.use('/admin', adminRouter.app) apiRouter.use('/mappings', mappingRouter) apiRouter.use('/sshKeys', sshKeyRouter) -apiRouter.use('/statuses', statusRouter) apiRouter.get('/availableDomains', (req, res) => { const domains = getAvailableDomains() diff --git a/src/api/mapping.ts b/src/api/mapping.ts index 07f99fa6..2e20a5e1 100644 --- a/src/api/mapping.ts +++ b/src/api/mapping.ts @@ -86,9 +86,30 @@ mappingRouter.post('/', async (req, res) => { }) }) -mappingRouter.get('/', (req, res) => { +mappingRouter.get('/', async (req, res) => { const domains = getMappings() - res.json(domains) + if (isProduction()) { + const data = await exec('su - myproxy -c "pm2 jlist"') + + const statusData = JSON.parse(data.stdout).reduce( + (statusObj, el) => ({ + ...statusObj, + [el.name]: el.pm2_env.status + }), + {} + ) + const fullDomainStatusMapping = domains.map(el => { + if (statusData[el.fullDomain]) { + return { ...el, status: statusData[el.fullDomain] } + } else { + return { ...el, status: 'not started' } + } + }) + + res.json(fullDomainStatusMapping) + } else { + res.json(domains.map(el => ({ ...el, status: 'not started' }))) + } }) mappingRouter.delete('/:id', async (req, res) => { diff --git a/src/api/serverStatus.ts b/src/api/serverStatus.ts deleted file mode 100644 index 31b348a2..00000000 --- a/src/api/serverStatus.ts +++ /dev/null @@ -1,33 +0,0 @@ -import cp from 'child_process' -import express from 'express' -import environment from '../helpers/environment' -import util from 'util' - -const statusRouter = express.Router() -const exec = util.promisify(cp.exec) -const { isProduction } = environment - -statusRouter.get('/', async (req, res) => { - try { - if (isProduction()) { - const data = await exec('su - myproxy -c "pm2 jlist"') - // Formats data into an Object that contains the each domain as the - // the key and each status as the value. - res.json( - JSON.parse(data.stdout).reduce( - (statusObj, el) => ({ - ...statusObj, - [el.name]: el.pm2_env.status - }), - {} - ) - ) - } else { - res.json({}) - } - } catch (err) { - res.status(500).send({ err }) - } -}) - -export default statusRouter diff --git a/src/public/client.ts b/src/public/client.ts index 2ba8429b..00190f7b 100644 --- a/src/public/client.ts +++ b/src/public/client.ts @@ -101,19 +101,19 @@ class MappingItem { } } -Promise.all([ - fetch('/api/mappings').then(r => r.json()), - fetch('/api/statuses').then(r => r.json()) -]).then(([mappings, statuses]: [Mapping[], Status[]]) => { - domainList.innerHTML = '' - mappings - .reverse() - .filter(e => e.domain && e.port && e.id && e.gitLink && e.fullDomain) - .forEach(e => { - e.status = statuses[e.fullDomain] || 'not started' - new MappingItem(e) - }) -}) +fetch('/api/mappings') + .then(r => r.json()) + .then(mappings => { + domainList.innerHTML = '' + mappings + .reverse() + .filter( + e => e.domain && e.port && e.id && e.gitLink && e.fullDomain && e.status + ) + .forEach(e => { + new MappingItem(e) + }) + }) create.onclick = (): void => { const subDomain = helper.getElement('.subDomain') as HTMLInputElement diff --git a/src/tests/integration/mapping.test.ts b/src/tests/integration/mapping.test.ts index 13f67d12..58ad0dc2 100644 --- a/src/tests/integration/mapping.test.ts +++ b/src/tests/integration/mapping.test.ts @@ -147,4 +147,21 @@ describe('/api', () => { await mappingAdapter(`/${match1.id}`, 'DELETE') await mappingAdapter(`/${match2.id}`, 'DELETE') }) + + it('checks status is returned when querying mappings', async () => { + const subDomain = uuidv4() + const domain = 'VinDiesel' + const port = '3533' + await mappingAdapter('/', 'POST', { + domain, + subDomain, + port + }) + + const getResponse = await mappingAdapter('/', 'GET') + const getMappings = await getResponse.json() + + expect(getMappings[0].status).toEqual('not started') + await mappingAdapter(`/${getMappings[0].id}`, 'DELETE') + }) }) diff --git a/src/tests/integration/status.test.ts b/src/tests/integration/status.test.ts deleted file mode 100644 index 04b085ee..00000000 --- a/src/tests/integration/status.test.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { startAppServer } from '../../server/server' -import fetch from 'node-fetch' - -const TEST_PORT = process.env.PORT || 5002 -const ADMIN = process.env.ADMIN || '123' - -const reqHeaders = { - authorization: ADMIN, - 'Content-Type': 'application/json' -} - -describe('api/status', () => { - let server - - beforeAll(async () => { - server = await startAppServer(TEST_PORT, ADMIN) - }) - - afterAll(() => { - server.close() - }) - - it('Server should return empty array when no domains are specified', async () => { - const res = await fetch(`http://localhost:${TEST_PORT}/api/statuses`, { - headers: reqHeaders - }) - - const data = await res.json() - expect(data).toStrictEqual({}) - expect(res.status).toEqual(200) - }) -})