-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo24.js
More file actions
73 lines (68 loc) · 2.42 KB
/
Copy pathdemo24.js
File metadata and controls
73 lines (68 loc) · 2.42 KB
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
const fs = require('fs')
const mypath = require('path')
const fetch = require("node-fetch")
const relativePath = 'HostJson'
const path = mypath.join(__dirname, relativePath + '.js')
const GOBACKEND = '127.0.0.1:9090'
function getHostConfig(app, httpListen) {
return new Promise(resolve => {
try {
// 从go拿到配置创建域名配置文件,如果有就引用,尽量确保成功,失败了也不要更改内容
var hostJson = {}
try {
hostJson = require('./' + relativePath + '.js')
} catch (error) {
console.log('getHostConfig first error', error)
}
console.log('getHostConfig hostJson1', hostJson)
// 需要从go拿到配置创建域名配置文件 render 重启调用。每次接口触发调用
fetch(`http://${GOBACKEND}/__domain/getHostConfig`, {
headers: {
'Content-Type': 'application/json'
},
dataType: 'json',
method: 'post',
body: JSON.stringify({
token: '216e0c7a7e3dd426f118c74e8ddfd823'
})
}).then((res) => {
res.json().then((result) => {
const { code, msg, data } = result
if (code === '200') {
for (const key in data) {
if (Object.prototype.hasOwnProperty.call(data, key)) {
const element = data[key]
data[key] = JSON.parse(element)
}
}
fs.writeFile(path, `module.exports = ${JSON.stringify(data)}`, function (err) {
if (err) {
throw err
}
delete require.cache[path]
hostJson = require('./' + relativePath + '.js')
console.log('getHostConfig json:', hostJson)
app && httpListen(app)
resolve(hostJson)
})
} else {
console.log('getHostConfig msg', msg, 'result', result)
}
}).catch((err) => {
app && httpListen(app)
console.log('getHostConfig catch', 1, ' hostJson:', hostJson, ' err:', err)
resolve(hostJson)
})
}).catch((err) => {
app && httpListen(app)
console.log('getHostConfig catch', 2, ' hostJson:', hostJson, ' err:', err)
resolve(hostJson)
})
} catch (err) {
app && httpListen(app)
console.log('getHostConfig catch', 3, ' hostJson:', hostJson, ' err:', err)
resolve(hostJson)
}
})
}
module.exports = getHostConfig