feat(tcp crud): tcp server start, stop, restart endpoints + status check
All checks were successful
Build and Push LST Docker Image / docker (push) Successful in 1m30s
All checks were successful
Build and Push LST Docker Image / docker (push) Successful in 1m30s
This commit is contained in:
14
backend/tcpServer/tcp.routes.ts
Normal file
14
backend/tcpServer/tcp.routes.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import type { Express } from "express";
|
||||
import { requireAuth } from "../middleware/auth.middleware.js";
|
||||
import restart from "./tcpRestart.route.js";
|
||||
import start from "./tcpStart.route.js";
|
||||
import stop from "./tcpStop.route.js";
|
||||
|
||||
export const setupTCPRoutes = (baseUrl: string, app: Express) => {
|
||||
//stats will be like this as we dont need to change this
|
||||
app.use(`${baseUrl}/api/tcp/start`, requireAuth, start);
|
||||
app.use(`${baseUrl}/api/tcp/stop`, requireAuth, stop);
|
||||
app.use(`${baseUrl}/api/tcp/restart`, requireAuth, restart);
|
||||
|
||||
// all other system should be under /api/system/*
|
||||
};
|
||||
@@ -3,13 +3,14 @@ import { eq } from "drizzle-orm";
|
||||
import { db } from "../db/db.controller.js";
|
||||
import { printerData } from "../db/schema/printers.schema.js";
|
||||
import { createLogger } from "../logger/logger.controller.js";
|
||||
import { delay } from "../utils/delay.utils.js";
|
||||
import { returnFunc } from "../utils/returnHelper.utils.js";
|
||||
import { tryCatch } from "../utils/trycatch.utils.js";
|
||||
import { type PrinterData, printerListen } from "./tcp.printerListener.js";
|
||||
|
||||
let tcpServer: net.Server;
|
||||
const tcpSockets: Set<net.Socket> = new Set();
|
||||
//let isServerRunning = false;
|
||||
export let isServerRunning = false;
|
||||
|
||||
const port = parseInt(process.env.TCP_PORT ?? "2222", 10);
|
||||
|
||||
@@ -39,9 +40,8 @@ const parseTcpAlert = (input: string) => {
|
||||
name,
|
||||
};
|
||||
};
|
||||
|
||||
export const startTCPServer = () => {
|
||||
const log = createLogger({ module: "tcp", submodule: "create_server" });
|
||||
const log = createLogger({ module: "tcp", submodule: "create_server" });
|
||||
export const startTCPServer = async () => {
|
||||
tcpServer = net.createServer(async (socket) => {
|
||||
tcpSockets.add(socket);
|
||||
socket.on("data", async (data: Buffer) => {
|
||||
@@ -103,7 +103,7 @@ export const startTCPServer = () => {
|
||||
log.info({}, `TCP Server listening on port ${port}`);
|
||||
});
|
||||
|
||||
//isServerRunning = true;
|
||||
isServerRunning = true;
|
||||
return returnFunc({
|
||||
success: true,
|
||||
level: "info",
|
||||
@@ -115,3 +115,66 @@ export const startTCPServer = () => {
|
||||
room: "",
|
||||
});
|
||||
};
|
||||
|
||||
export const stopTCPServer = async () => {
|
||||
if (!isServerRunning)
|
||||
return { success: false, message: "Server is not running" };
|
||||
for (const socket of tcpSockets) {
|
||||
socket.destroy();
|
||||
}
|
||||
tcpSockets.clear();
|
||||
tcpServer.close(() => {
|
||||
log.info({}, "TCP Server stopped");
|
||||
});
|
||||
isServerRunning = false;
|
||||
return returnFunc({
|
||||
success: true,
|
||||
level: "info",
|
||||
module: "tcp",
|
||||
subModule: "create_server",
|
||||
message: "TCP server stopped.",
|
||||
data: [],
|
||||
notify: false,
|
||||
room: "",
|
||||
});
|
||||
};
|
||||
|
||||
export const restartTCPServer = async () => {
|
||||
if (!isServerRunning) {
|
||||
startTCPServer();
|
||||
return returnFunc({
|
||||
success: false,
|
||||
level: "warn",
|
||||
module: "tcp",
|
||||
subModule: "create_server",
|
||||
message: "Server is not running will try to start it",
|
||||
data: [],
|
||||
notify: false,
|
||||
room: "",
|
||||
});
|
||||
} else {
|
||||
for (const socket of tcpSockets) {
|
||||
socket.destroy();
|
||||
}
|
||||
tcpSockets.clear();
|
||||
tcpServer.close(() => {
|
||||
log.info({}, "TCP Server stopped");
|
||||
});
|
||||
isServerRunning = false;
|
||||
|
||||
await delay(1500);
|
||||
|
||||
startTCPServer();
|
||||
}
|
||||
|
||||
return returnFunc({
|
||||
success: true,
|
||||
level: "info",
|
||||
module: "tcp",
|
||||
subModule: "create_server",
|
||||
message: "TCP server has been restarted.",
|
||||
data: [],
|
||||
notify: false,
|
||||
room: "",
|
||||
});
|
||||
};
|
||||
|
||||
19
backend/tcpServer/tcpRestart.route.ts
Normal file
19
backend/tcpServer/tcpRestart.route.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Router } from "express";
|
||||
import { apiReturn } from "../utils/returnHelper.utils.js";
|
||||
import { restartTCPServer } from "./tcp.server.js";
|
||||
|
||||
const r = Router();
|
||||
|
||||
r.post("/restart", async (_, res) => {
|
||||
const connect = await restartTCPServer();
|
||||
apiReturn(res, {
|
||||
success: connect.success,
|
||||
level: connect.success ? "info" : "error",
|
||||
module: "tcp",
|
||||
subModule: "post",
|
||||
message: "TCP Server has been restarted",
|
||||
data: connect.data,
|
||||
status: connect.success ? 200 : 400,
|
||||
});
|
||||
});
|
||||
export default r;
|
||||
20
backend/tcpServer/tcpStart.route.ts
Normal file
20
backend/tcpServer/tcpStart.route.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Router } from "express";
|
||||
import { apiReturn } from "../utils/returnHelper.utils.js";
|
||||
import { startTCPServer } from "./tcp.server.js";
|
||||
|
||||
const r = Router();
|
||||
|
||||
r.post("/start", async (_, res) => {
|
||||
const connect = await startTCPServer();
|
||||
apiReturn(res, {
|
||||
success: connect.success,
|
||||
level: connect.success ? "info" : "error",
|
||||
module: "routes",
|
||||
subModule: "prodSql",
|
||||
message: connect.message,
|
||||
data: connect.data,
|
||||
status: connect.success ? 200 : 400,
|
||||
});
|
||||
});
|
||||
|
||||
export default r;
|
||||
20
backend/tcpServer/tcpStop.route.ts
Normal file
20
backend/tcpServer/tcpStop.route.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Router } from "express";
|
||||
import { apiReturn } from "../utils/returnHelper.utils.js";
|
||||
import { stopTCPServer } from "./tcp.server.js";
|
||||
|
||||
const r = Router();
|
||||
|
||||
r.post("/stop", async (_, res) => {
|
||||
const connect = await stopTCPServer();
|
||||
apiReturn(res, {
|
||||
success: connect.success,
|
||||
level: connect.success ? "info" : "error",
|
||||
module: "routes",
|
||||
subModule: "prodSql",
|
||||
message: connect.message,
|
||||
data: [],
|
||||
status: connect.success ? 200 : 400,
|
||||
});
|
||||
});
|
||||
|
||||
export default r;
|
||||
Reference in New Issue
Block a user