-
Notifications
You must be signed in to change notification settings - Fork 152
/
packageListServer.js
78 lines (69 loc) · 2.43 KB
/
packageListServer.js
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
73
74
75
76
77
78
const axios = require('axios')
const fs = require('fs')
const YAML = require('yamljs')
const lists = require('./.plugins.json')
const http = require('http')
const PORT = process.env.PORT || 4000
const localhost = 'http://ide.test'
console.log(`lisitening on PORT ${PORT}`)
const commonHeader = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': 'http://ide.test:8060',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Headers': 'Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, X-Space-Key'
}
let getPluginsPorts = lists
try {
const taskConfig = fs.readFileSync('./task.yaml', 'utf-8')
const taskConfigJson = YAML.parse(taskConfig)
getPluginsPorts = getPluginsPorts.concat(taskConfigJson.apps
.filter(task => task.name && task.name.split('-')[0] === 'plugin')
.map(task => task.env ? task.env.PORT || 4000 : 4000))
console.log(`find ${getPluginsPorts.length} ports`, getPluginsPorts.join())
} catch (e) {
console.log('e')
}
http.createServer((req, res) => {
if (req.url === '/packages/' && getPluginsPorts) {
const listsPromises = getPluginsPorts
.map(port => axios.get(String(port).includes('http') ? port : `${localhost}:${port}/packages`)
.then(res => Object.assign(res.data[0], { TARGET: String(port).includes('http') ? port : `${localhost}:${port}` }))
.catch(e => console.log(e))
)
Promise.all(listsPromises)
.then((values) => {
const result = values.filter(v => v)
res.writeHead(200, commonHeader)
res.write(JSON.stringify(result))
res.end()
})
.catch((e) => {
res.writeHead(500, commonHeader)
res.write(JSON.stringify({ error: e }))
res.end()
})
} else if (req.url.startsWith('/packages/??')) {
const pluginsString = req.url.substring(12)
const pluginScripts = pluginsString.split(',')
const listsPromises = getPluginsPorts
.map((port, idx) => axios.get(String(port).includes('http') ? port : `${localhost}:${port}/packages/${pluginScripts[idx]}`)
.then((res) => res.data)
.catch(e => console.log(e))
)
Promise.all(listsPromises)
.then((values) => {
const result = values.join('')
res.writeHead(200, commonHeader)
res.write(result)
res.end()
})
.catch((e) => {
res.writeHead(500, commonHeader)
res.write(JSON.stringify({ error: e }))
res.end()
})
} else {
res.write('it works')
res.end()
}
}).listen(PORT)