Skip to content

Commit 9584001

Browse files
authored
Merge pull request #324 from hwong0305/refactorStatus
Combining API
2 parents e0fa1d5 + c0f2a21 commit 9584001

File tree

6 files changed

+53
-82
lines changed

6 files changed

+53
-82
lines changed

src/api/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ import express from 'express'
22
import adminRouter from './admin'
33
import mappingRouter from './mapping'
44
import sshKeyRouter from './sshKeys'
5-
import statusRouter from './serverStatus'
65
import { getAvailableDomains } from '../lib/data'
76

87
const apiRouter = express.Router()
98

109
apiRouter.use('/admin', adminRouter.app)
1110
apiRouter.use('/mappings', mappingRouter)
1211
apiRouter.use('/sshKeys', sshKeyRouter)
13-
apiRouter.use('/statuses', statusRouter)
1412

1513
apiRouter.get('/availableDomains', (req, res) => {
1614
const domains = getAvailableDomains()

src/api/mapping.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,30 @@ mappingRouter.post('/', async (req, res) => {
8686
})
8787
})
8888

89-
mappingRouter.get('/', (req, res) => {
89+
mappingRouter.get('/', async (req, res) => {
9090
const domains = getMappings()
91-
res.json(domains)
91+
if (isProduction()) {
92+
const data = await exec('su - myproxy -c "pm2 jlist"')
93+
94+
const statusData = JSON.parse(data.stdout).reduce(
95+
(statusObj, el) => ({
96+
...statusObj,
97+
[el.name]: el.pm2_env.status
98+
}),
99+
{}
100+
)
101+
const fullDomainStatusMapping = domains.map(el => {
102+
if (statusData[el.fullDomain]) {
103+
return { ...el, status: statusData[el.fullDomain] }
104+
} else {
105+
return { ...el, status: 'not started' }
106+
}
107+
})
108+
109+
res.json(fullDomainStatusMapping)
110+
} else {
111+
res.json(domains.map(el => ({ ...el, status: 'not started' })))
112+
}
92113
})
93114

94115
mappingRouter.delete('/:id', async (req, res) => {

src/api/serverStatus.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/public/client.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,19 @@ class MappingItem {
101101
}
102102
}
103103

104-
Promise.all([
105-
fetch('/api/mappings').then(r => r.json()),
106-
fetch('/api/statuses').then(r => r.json())
107-
]).then(([mappings, statuses]: [Mapping[], Status[]]) => {
108-
domainList.innerHTML = ''
109-
mappings
110-
.reverse()
111-
.filter(e => e.domain && e.port && e.id && e.gitLink && e.fullDomain)
112-
.forEach(e => {
113-
e.status = statuses[e.fullDomain] || 'not started'
114-
new MappingItem(e)
115-
})
116-
})
104+
fetch('/api/mappings')
105+
.then(r => r.json())
106+
.then(mappings => {
107+
domainList.innerHTML = ''
108+
mappings
109+
.reverse()
110+
.filter(
111+
e => e.domain && e.port && e.id && e.gitLink && e.fullDomain && e.status
112+
)
113+
.forEach(e => {
114+
new MappingItem(e)
115+
})
116+
})
117117

118118
create.onclick = (): void => {
119119
const subDomain = helper.getElement('.subDomain') as HTMLInputElement

src/tests/integration/mapping.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,21 @@ describe('/api', () => {
147147
await mappingAdapter(`/${match1.id}`, 'DELETE')
148148
await mappingAdapter(`/${match2.id}`, 'DELETE')
149149
})
150+
151+
it('checks status is returned when querying mappings', async () => {
152+
const subDomain = uuidv4()
153+
const domain = 'VinDiesel'
154+
const port = '3533'
155+
await mappingAdapter('/', 'POST', {
156+
domain,
157+
subDomain,
158+
port
159+
})
160+
161+
const getResponse = await mappingAdapter('/', 'GET')
162+
const getMappings = await getResponse.json()
163+
164+
expect(getMappings[0].status).toEqual('not started')
165+
await mappingAdapter(`/${getMappings[0].id}`, 'DELETE')
166+
})
150167
})

src/tests/integration/status.test.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)