-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
153 lines (131 loc) · 5.4 KB
/
server.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const express = require("express")
const fs = require("fs/promises")
const path = require("path")
const cors = require("cors")
const bodyParser = require("body-parser")
const app = express()
const port = 30000
// 设置当前工作目录为与src目录同级的目录
const currentDirectory = path.join(__dirname, "etc/openGemini.conf")
app.use(
cors({
origin: [
"http://localhost:3333",
"http://219.216.65.83:3333",
"http://169.254.95.120:3333",
"http://172.17.0.1:3333",
"http://172.18.0.1:3333"
]
})
)
app.use(bodyParser.json()) //使用 body-parser 解析请求体
app.post("/start-server", async (req, res) => {
try {
// 启动服务器的代码
const { spawn } = require("child_process")
const serverProcess = spawn("node", ["server.js"])
serverProcess.stdout.on("data", (data) => {
console.log(`Server stdout: ${data}`)
})
serverProcess.stderr.on("data", (data) => {
console.error(`Server stderr: ${data}`)
})
serverProcess.on("close", (code) => {
console.log(`Server process exited with code ${code}`)
})
res.status(200).json({ success: true, message: "Server started successfully." })
} catch (error) {
res.status(500).json({ success: false, error: error.message })
}
})
app.get("/get-config-file-content", async (req, res) => {
try {
// 在这里编写代码以读取配置文件的内容
const configFileContent = await fs.readFile(currentDirectory, "utf-8")
res.send(configFileContent)
} catch (error) {
res.status(500).json({ success: false, error: error.message })
}
})
app.post("/save-config-file-content", async (req, res) => {
try {
const {
metaJoinInCommon1,
metaJoinInCommon2,
metaJoinInCommon3,
bindAddressInMeta,
httpBindAddressInMeta,
rpcBindAddressInMeta,
dirInMeta,
bindAddressInHttp,
storeIngestAddrInData,
storeSelectAddrInData,
storeDataDirInData,
storeWalDirInData,
storeMetaDirInData,
bindAddressInGossip,
storeBindPortInGossip,
metaBindPortInGossip,
membersInGossip1,
membersInGossip2,
membersInGossip3
} = req.body
//读配置文件内容
const contentInCommon = await fs.readFile(currentDirectory, "utf-8")
const updatedConfigInCommon = contentInCommon.replace(
/(\[common\][\s\S]*?meta-join\s*=\s*\[")([^"]*)(",\s*")([^"]*)(",\s*")([^"]*)("\])/,
(match, group1, group2, group3, group4, group5, group6, group7) =>
`${group1}${metaJoinInCommon1}${group3}${metaJoinInCommon2}${group5}${metaJoinInCommon3}${group7}`
)
//将更新后的配置写回文件
await fs.writeFile(currentDirectory, updatedConfigInCommon, "utf-8")
const contentInMeta = await fs.readFile(currentDirectory, "utf-8")
const updatedConfigInMeta = contentInMeta.replace(
/(\[meta\][\s\S]*?bind-address\s*=\s*")([^"]*)("\s*http-bind-address\s*=\s*")([^"]*)("\s*rpc-bind-address\s*=\s*")([^"]*)("\s*dir\s*=\s*")([^"]*)(")/,
(match, group1, group2, group3, group4, group5, group6, group7, group8, group9) =>
`${group1}${bindAddressInMeta}${group3}${httpBindAddressInMeta}${group5}${rpcBindAddressInMeta}${group7}${dirInMeta}${group9}`
)
await fs.writeFile(currentDirectory, updatedConfigInMeta, "utf-8")
const contentInHttp = await fs.readFile(currentDirectory, "utf-8")
const updatedConfigInHttp = contentInHttp.replace(
/(\[http\][\s\S]*?bind-address\s*=\s*")([^"]*)(")/,
(match, group1, group2, group3) => `${group1}${bindAddressInHttp}${group3}`
)
await fs.writeFile(currentDirectory, updatedConfigInHttp, "utf-8")
const contentInData = await fs.readFile(currentDirectory, "utf-8")
const updatedConfigInData = contentInData.replace(
/(\[data\][\s\S]*?store-ingest-addr\s*=\s*")([^"]*)("\s*store-select-addr\s*=\s*")([^"]*)("\s*store-data-dir\s*=\s*")([^"]*)("\s*store-wal-dir\s*=\s*")([^"]*)("\s*store-meta-dir\s*=\s*")([^"]*)(")/,
(match, group1, group2, group3, group4, group5, group6, group7, group8, group9, group10, group11) =>
`${group1}${storeIngestAddrInData}${group3}${storeSelectAddrInData}${group5}${storeDataDirInData}${group7}${storeWalDirInData}${group9}${storeMetaDirInData}${group11}`
)
await fs.writeFile(currentDirectory, updatedConfigInData, "utf-8")
const contentInGossip = await fs.readFile(currentDirectory, "utf-8")
const updatedConfigInGossip = contentInGossip.replace(
/(\[gossip\][\s\S]*?bind-address\s*=\s*")([^"]*)("\s*store-bind-port\s*=\s*)(\S*)(\s*meta-bind-port\s*=\s*)(\S*)([\s\S]*?members\s*=\s*\[")([^"]*)(",\s*")([^"]*)(",\s*")([^"]*)("\])/,
(
match,
group1,
group2,
group3,
group4,
group5,
group6,
group7,
group8,
group9,
group10,
group11,
group12,
group13
) =>
`${group1}${bindAddressInGossip}${group3}${storeBindPortInGossip}${group5}${metaBindPortInGossip}${group7}${membersInGossip1}${group9}${membersInGossip2}${group11}${membersInGossip3}${group13}`
)
await fs.writeFile(currentDirectory, updatedConfigInGossip, "utf-8")
res.status(200).json({ success: true, message: "Configuration file([meta]) saved successfully" })
} catch (error) {
res.status(500).json({ success: false, error: error.message })
}
})
app.listen(port, () => {
console.log(`Server is listening at http://localhost:${port}`)
})