mdkcontroller 1.5.1 → 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 +99 -4
  2. package/package.json +2 -1
package/main.js CHANGED
@@ -9,9 +9,12 @@ 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
+ import 'dotenv/config';
16
+
17
+ import readline from 'readline';
15
18
 
16
19
  export default async function (appname, cfgHandler = {}) {
17
20
 
@@ -140,19 +143,111 @@ export default async function (appname, cfgHandler = {}) {
140
143
  app.use("/Pages", staticExpress(join(__parentAppPath, "Pages")));
141
144
  app.use("/Cores", staticExpress(join(__rootDirPath, "Cores")));
142
145
  app.use("/api", userRoutes.router);
146
+
147
+
148
+
143
149
  return {
144
150
  server: app,
145
151
  database: db,
146
152
  authen: auth,
147
153
  serverBase: server,
148
154
  startListen:
149
- async function (kport = 8095) {
155
+ async function (kport, adminConsole = false) {
156
+ const PORT = kport || parseInt(process.env.PORT) || 3000;
150
157
  return new Promise(resolve => {
151
- server.listen(kport, () => {
158
+ server.listen(PORT, () => {
152
159
  console.log(
153
- `Server is listening on port ${kport}: http://localhost:${kport}`
160
+ `Server is listening on port ${PORT}: http://localhost:${PORT}`
154
161
  );
155
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
+ }
156
251
  });
157
252
  });
158
253
  }
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "dependencies": {
3
3
  "cookie-parser": "^1.4.6",
4
+ "dotenv": "^17.2.3",
4
5
  "express": "^4.18.2",
5
6
  "js-yaml": "^4.1.0",
6
7
  "jsonwebtoken": "^9.0.2",
@@ -8,7 +9,7 @@
8
9
  "node-cache": "^5.1.2"
9
10
  },
10
11
  "name": "mdkcontroller",
11
- "version": "1.5.1",
12
+ "version": "1.6.1",
12
13
  "keywords": [],
13
14
  "author": "KHANHNBD <khanh272421@gmail.com>",
14
15
  "license": "ISC",