-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproperties.controller.js
More file actions
164 lines (140 loc) · 4.95 KB
/
properties.controller.js
File metadata and controls
164 lines (140 loc) · 4.95 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
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
154
155
156
157
158
159
160
161
162
163
164
const propertiesService = require("../services/properties.service");
const configUtils = require("../utils/config.util");
const jsonFilesUtils = require("../utils/files.util");
const { Sema } = require('async-sema');
let togglePropertySema = new Sema(1);
async function updateProperty(req, res) {
togglePropertySema.acquire();
try {
console.log(req.params.property);
await propertiesService.updateProperty(req.params.property, req.params.newvalue);
res.status(200).send("done");
} catch(error) {
console.error(error);
res.status(500).send("error.. " + error.message);
} finally {
togglePropertySema.release();
}
}
let ramAllocationSema = new Sema(1);
async function allocateRam(req, res) {
ramAllocationSema.acquire();
try {
configUtils.updateMemoryAllocated(req.params.mb, true);
res.status(200).send(`Ram allocation updated to ${req.params.mb}M`);
} catch(error) {
console.error(error);
res.status(500).send("error.. " + error.message);
} finally {
ramAllocationSema.release();
}
}
async function serverConfig(req, res) {
try {
const configJSON = configUtils.getConfigJSON();
res.status(200).send(configJSON);
} catch(error) {
console.error(error);
res.status(500).send("error.. " + error.message);
}
}
async function getWhitelist(req, res) {
try {
const whitelistJSON = jsonFilesUtils.getWhitelistJSON();
res.status(200).send(whitelistJSON);
} catch(error) {
console.error(error);
res.status(500).send("error.. " + error.message);
}
}
async function getOps(req, res) {
try {
const opsJSON = jsonFilesUtils.getOpsJSON();
res.status(200).send(opsJSON);
} catch(error) {
console.error(error);
res.status(500).send("error.. " + error.message);
}
}
async function getBannedPlayers(req, res) {
try {
const bannedplayersJSON = jsonFilesUtils.getBannedPlayersJSON();
res.status(200).send(bannedplayersJSON);
} catch(error) {
console.error(error);
res.status(500).send("error.. " + error.message);
}
}
async function modifyOperator(req, res) {
try {
if (req.params.operation != "add" && req.params.operation != "remove") {
res.status(404).send(`Invalid operation ${req.params.operation}`);
return;
}
await jsonFilesUtils.modifyOpsJSON(req.params.playername, req.params.operation === "add");
res.status(200).send(`${req.params.operation === "add" ? "Added" : "Remove"} ${req.params.playername} as an Operator`);
} catch(error) {
console.error(error);
res.status(500).send("error.. " + error.message);
}
}
async function modifyWhitelist(req, res) {
try {
if (req.params.operation != "add" && req.params.operation != "remove") {
res.status(404).send(`Invalid operation ${req.params.operation}`);
return;
}
await jsonFilesUtils.modifyWhitelistJSON(req.params.playername, req.params.operation === "add");
if (req.params.operation === "add")
res.status(200).send(`Added ${req.params.playername} to the Whitelist`);
else
res.status(200).send(`Remove ${req.params.playername} from the Whitelist`);
} catch(error) {
console.error(error);
res.status(500).send("error.. " + error.message);
}
}
async function modifyBanned(req, res) {
try {
if (req.params.operation != "add" && req.params.operation != "remove") {
res.status(404).send(`Invalid operation ${req.params.operation}`);
return;
}
await jsonFilesUtils.modifyBannedPlayersJSON(req.params.playername, req.params.operation === "add");
if (req.params.operation === "add")
res.status(200).send(`Banned ${req.params.playername}`);
else
res.status(200).send(`Pardoned ${req.params.playername}`);
} catch(error) {
console.error(error);
res.status(500).send("error.. " + error.message);
}
}
async function modifyBannedIPs(req, res) {
try {
if (req.params.operation != "add" && req.params.operation != "remove") {
res.status(404).send(`Invalid operation ${req.params.operation}`);
return;
}
await jsonFilesUtils.modifyBannedIPsJSON(req.params.playername, req.params.operation === "add");
if (req.params.operation === "add")
res.status(200).send(`Banned ${req.params.playername}`);
else
res.status(200).send(`Pardoned ${req.params.playername}`);
} catch(error) {
console.error(error);
res.status(500).send("error.. " + error.message);
}
}
module.exports = {
updateProperty,
allocateRam,
serverConfig,
getWhitelist,
getOps,
getBannedPlayers,
modifyOperator,
modifyWhitelist,
modifyBanned,
modifyBannedIPs
}