mdkcontroller 1.5.2 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/main.js +98 -4
  2. package/package.json +1 -1
package/main.js CHANGED
@@ -9,11 +9,13 @@ import cookieParser from "cookie-parser";
9
9
  import dbInstant from "./dk_modules/dkdb.js";
10
10
  import userInstant from "./dk_modules/users.js";
11
11
  import authInstant from "./dk_modules/authorization.js";
12
-
12
+ import { getNumber as Sequense } from "./dk_modules/autoSequence.js";
13
13
  import { fileURLToPath } from "url";
14
14
  import path from 'path';
15
15
  import 'dotenv/config';
16
16
 
17
+ import readline from 'readline';
18
+
17
19
  export default async function (appname, cfgHandler = {}) {
18
20
 
19
21
  const __filename = fileURLToPath(import.meta.url);
@@ -141,19 +143,111 @@ export default async function (appname, cfgHandler = {}) {
141
143
  app.use("/Pages", staticExpress(join(__parentAppPath, "Pages")));
142
144
  app.use("/Cores", staticExpress(join(__rootDirPath, "Cores")));
143
145
  app.use("/api", userRoutes.router);
146
+
147
+
148
+
144
149
  return {
145
150
  server: app,
146
151
  database: db,
147
152
  authen: auth,
148
153
  serverBase: server,
149
154
  startListen:
150
- async function (kport = 8095) {
155
+ async function (kport, adminConsole = false) {
156
+ const PORT = kport || parseInt(process.env.PORT) || 3000;
151
157
  return new Promise(resolve => {
152
- server.listen(kport, () => {
158
+ server.listen(PORT, () => {
153
159
  console.log(
154
- `Server is listening on port ${kport}: http://localhost:${kport}`
160
+ `Server is listening on port ${PORT}: http://localhost:${PORT}`
155
161
  );
156
162
  resolve();
163
+ if (adminConsole) {
164
+ console.log("Chế độ quản trị viên đã được kích hoạt. Nhập 'help' để xem các lệnh hỗ trợ.");
165
+ const userTable = db.data.users || [];
166
+
167
+ const rl = readline.createInterface({
168
+ input: process.stdin,
169
+ output: process.stdout
170
+ });
171
+ function ask(question) {
172
+ return new Promise(resolve => {
173
+ rl.question(question, answer => resolve(answer));
174
+ });
175
+ }
176
+ (async () => {
177
+ let keepGoing = true;
178
+
179
+
180
+ while (keepGoing) {
181
+ const input = await ask('Common: ');
182
+
183
+ if (input === 'exit') {
184
+ keepGoing = false;
185
+ } else {
186
+ if (input == "help") {
187
+ console.log(`Các lệnh hỗ trợ:
188
+ help: hiển thị danh sách lệnh
189
+ exit: thoát chờ lệnh
190
+ ua: thêm người dùng mới
191
+ ud: xóa người dùng
192
+ ul: danh sách người dùng`);
193
+ }
194
+
195
+ if (input.startsWith("ua")) {
196
+ const parts = input.split(" ");
197
+ if (parts.length != 3) {
198
+ console.log("Cú pháp: ua <username> <password>");
199
+ continue;
200
+ }
201
+ const username = parts[1];
202
+ const password = parts[2];
203
+ const existingUser = userTable.find(user => user.username === username);
204
+ if (existingUser) {
205
+ existingUser.password = global.auth.encrypt(password);
206
+ existingUser.signcode = global.generateRandomString(100);
207
+ db.write();
208
+ console.log(`Người dùng ${username} đã được cập nhật.`);
209
+ } else {
210
+ const newUser = {
211
+ id: Sequense('users'),
212
+ username: username.toLowerCase(),
213
+ email: username + "@patsoft.com.vn",
214
+ password: global.auth.encrypt(password),
215
+ active: true,
216
+ signcode: global.generateRandomString(100),
217
+ }
218
+ userTable.push(newUser);
219
+ db.write();
220
+ console.log(`Người dùng ${username} đã được thêm.`);
221
+ }
222
+ }
223
+ if (input.startsWith("ud")) {
224
+ const parts = input.split(" ");
225
+ if (parts.length != 2) {
226
+ console.log("Cú pháp: ud <username>");
227
+ continue;
228
+ }
229
+ const username = parts[1];
230
+ const userIndex = userTable.findIndex(user => user.username === username);
231
+ if (userIndex !== -1) {
232
+ userTable.splice(userIndex, 1);
233
+ db.write();
234
+ console.log(`Người dùng ${username} đã được xóa.`);
235
+ } else {
236
+ console.log(`Người dùng ${username} không tồn tại.`);
237
+ }
238
+ }
239
+ if (input.startsWith("ul")) {
240
+ console.log("Danh sách người dùng:");
241
+ userTable.forEach(user => {
242
+ console.log(`- ${user.username}`);
243
+ });
244
+ }
245
+ }
246
+ }
247
+
248
+ rl.close();
249
+ })();
250
+ }
157
251
  });
158
252
  });
159
253
  }
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "node-cache": "^5.1.2"
10
10
  },
11
11
  "name": "mdkcontroller",
12
- "version": "1.5.2",
12
+ "version": "1.6.1",
13
13
  "keywords": [],
14
14
  "author": "KHANHNBD <khanh272421@gmail.com>",
15
15
  "license": "ISC",