209 lines
5.4 KiB
TypeScript
209 lines
5.4 KiB
TypeScript
import fs from 'fs';
|
|
import axios from "axios";
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Routes
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
| This file is dedicated for defining HTTP routes. A single file is enough
|
|
| for majority of projects, however you can define routes in different
|
|
| files and just make sure to import them inside this file. For example
|
|
|
|
|
| Define routes in following two files
|
|
| ├── start/routes/cart.ts
|
|
| ├── start/routes/customer.ts
|
|
|
|
|
| and then import them inside `start/routes.ts` as follows
|
|
|
|
|
| import './routes/cart'
|
|
| import './routes/customer'
|
|
|
|
|
*/
|
|
|
|
import Route from "@ioc:Adonis/Core/Route";
|
|
import { runtimeCheckLogs } from "App/utils/runtimeCheckLogs";
|
|
import Env from "@ioc:Adonis/Core/Env";
|
|
import { sendMessToZulip } from "App/utils/sendMessToZulip";
|
|
import moment from "moment";
|
|
import Product from "App/Models/Product";
|
|
import { sendDeviceInfora } from "App/utils/sendDeviceInfor";
|
|
import InfoDevice from "App/Models/InfoDevice";
|
|
import LogReport from "App/Models/LogReport";
|
|
import Cache from "@ioc:Kaperskyguru/Adonis-Cache";
|
|
const util = require('util');
|
|
const exec = util.promisify(require('child_process').exec);
|
|
|
|
runtimeCheckLogs(Env.get("FOLDER_LOGS"));
|
|
|
|
|
|
Route.post("/api/getIndexSerialNumber", "ErpsController.getIndexSerialNumber")
|
|
.middleware("checkToken").middleware(
|
|
"writeLog"
|
|
);
|
|
// .middleware("writeLog");
|
|
|
|
Route.post("/api/getParagraph", "ErpsController.getParagraph")
|
|
.middleware("checkToken")
|
|
.middleware("writeLog")
|
|
|
|
|
|
//Users
|
|
Route.post("/api/account/createUser", "UsersController.create").middleware(
|
|
"writeLog"
|
|
);
|
|
|
|
Route.post("/api/account/checkLogin", "UsersController.checkLogin").middleware(
|
|
"writeLog"
|
|
);
|
|
|
|
//Log
|
|
Route.get("/api/log/showLog/:name?", "LogsController.showLog").middleware(
|
|
"writeLog"
|
|
);
|
|
|
|
Route.get("/api/getAllLogDetect", "LogsController.getAllLogDetect")
|
|
|
|
//Key-Value
|
|
Route.post("/api/getKeyValue", "ValuesController.getKeyValue")
|
|
|
|
Route.post("/api/deleteValue", "ValuesController.destroy").middleware(
|
|
"writeLog"
|
|
);
|
|
|
|
Route.post("/api/editValue", "ValuesController.edit").middleware(
|
|
"writeLog"
|
|
);
|
|
|
|
Route.post("/api/addValue", "ValuesController.create").middleware("writeLog");
|
|
|
|
Route.post("/api/backupProduct", async ({ request, response }) => {
|
|
try {
|
|
const date = moment(Date.now()).format("YYYYMMDD");
|
|
const res = await axios.post(
|
|
"https://logs.danielvu.com/api/getIndexSerialNumber",
|
|
{ from: date, to: date },
|
|
{
|
|
headers: {
|
|
Authorization: request.headers().authorization?.replace(/"/g, ""),
|
|
},
|
|
}
|
|
);
|
|
|
|
res.data.map((obj, index) => {
|
|
res.data[index] = {
|
|
PID: res.data[index].PID,
|
|
SN: res.data[index].SN,
|
|
VID: res.data[index].VID,
|
|
line: res.data[index].line.join(","),
|
|
file: res.data[index].fileName,
|
|
warehouse: res.data[index].warehouse,
|
|
};
|
|
});
|
|
const addProduct = await Product.createMany(res.data);
|
|
// console.log(addProduct)
|
|
response.status(200).send("Add " + res.data.length + " success!");
|
|
await sendMessToZulip(
|
|
"stream",
|
|
"networkToolBot",
|
|
"Log service",
|
|
"Backup product " +
|
|
date +
|
|
" success with " +
|
|
res.data.length +
|
|
" products"
|
|
);
|
|
} catch (error) {
|
|
response.status(500).send(error);
|
|
await sendMessToZulip(
|
|
"stream",
|
|
"networkToolBot",
|
|
"Log service",
|
|
"Backup product fail. Please check!"
|
|
);
|
|
}
|
|
}).middleware("writeLog");
|
|
|
|
Route.post("/api/sendMailInforDevice", async () => {
|
|
try {
|
|
sendDeviceInfora()
|
|
} catch (error) {
|
|
console.log(error)
|
|
}
|
|
}).middleware("checkToken").middleware(
|
|
"writeLog"
|
|
);
|
|
|
|
Route.post("/api/private-log/getFileOnFolder", async ({ request, response }) => {
|
|
try {
|
|
let result = [];
|
|
let path = request.all().folerPath;
|
|
return new Promise((resolve, reject) => {
|
|
fs.readdir(path, (err, entries) => {
|
|
if (err) {
|
|
reject(err);
|
|
return;
|
|
}
|
|
|
|
entries.forEach((entry) => {
|
|
const entryPath = path+"/"+entry;
|
|
|
|
fs.stat(entryPath, (statErr, stats) => {
|
|
if (statErr) {
|
|
reject(statErr);
|
|
return;
|
|
}
|
|
|
|
const type = stats.isFile() ? "file" : "directory";
|
|
result.push({ name: entryPath, type: type });
|
|
|
|
if (result.length === entries.length) {
|
|
resolve(result);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
})
|
|
|
|
Route.post("/api/private-log/readFile", async ({ request, response }) => {
|
|
try {
|
|
let result = [];
|
|
let path = request.all().filePath;
|
|
return await fs.readFileSync(path, "utf8");
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
})
|
|
|
|
Route.post("/api/find-value", async ({ request, response }) => {
|
|
try {
|
|
const { value } = request.all();
|
|
|
|
const { stdout, stderr } = await exec(`grep -nr "${value}" /home/logs`);
|
|
|
|
|
|
response.status(200).send(JSON.stringify(stdout));
|
|
} catch (error) {
|
|
console.error(error);
|
|
response.status(500).send(`Error: ${error.message}`);
|
|
}
|
|
});
|
|
|
|
Route.post("/api/test", async () => {
|
|
try {
|
|
const logs = await Cache.get('logs')
|
|
|
|
if(logs){
|
|
return {type: "cache", data: logs}
|
|
}else{
|
|
let data = await LogReport.all();
|
|
Cache.set('logs', data, 120)
|
|
return {type: "no cache", data: data}
|
|
}
|
|
} catch (error) {
|
|
console.log(error)
|
|
}
|
|
}) |