diff --git a/server/index.ts b/server/index.ts index 5fc338d..129918a 100644 --- a/server/index.ts +++ b/server/index.ts @@ -16,6 +16,7 @@ import ocme from "./services/ocme/ocmeService.js"; import sqlService from "./services/sqlServer/sqlService.js"; import logistics from "./services/logistics/logisticsService.js"; import rfid from "./services/rfid/rfidService.js"; +import printers from "./services/printers/printerService.js"; import {db} from "../database/dbclient.js"; import {settings} from "../database/schema/settings.js"; @@ -83,6 +84,7 @@ const routes = [ sqlService, logistics, rfid, + printers, ] as const; const appRoutes = routes.forEach((route) => { diff --git a/server/services/printers/printerService.ts b/server/services/printers/printerService.ts new file mode 100644 index 0000000..47dfeab --- /dev/null +++ b/server/services/printers/printerService.ts @@ -0,0 +1,14 @@ +import {OpenAPIHono} from "@hono/zod-openapi"; + +import alerts from "./route/printerAlert.js"; +const app = new OpenAPIHono(); +const port = 4000; + +const routes = [alerts] as const; + +// app.route("/server", modules); +const appRoutes = routes.forEach((route) => { + app.route("/printers", route); +}); + +export default app; diff --git a/server/services/printers/route/printerAlert.ts b/server/services/printers/route/printerAlert.ts new file mode 100644 index 0000000..fb07731 --- /dev/null +++ b/server/services/printers/route/printerAlert.ts @@ -0,0 +1,102 @@ +//http://usday1vms006:4000/api/v1/zebra/wrapper1 +import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi"; + +// Define the response schema +const responseSchema = z.object({ + success: z.boolean().openapi({example: true}), + message: z.string().optional(), +}); + +const app = new OpenAPIHono(); + +const ParamsSchema = z.object({ + printer: z + .string() + .min(3) + .openapi({ + param: { + name: "printer", + in: "path", + }, + example: "Line1", + }), +}); + +app.openapi( + createRoute({ + tags: ["printer"], + summary: "Printer Alert", + method: "post", + path: "/{printer}", + request: { + params: ParamsSchema, + }, + responses: { + 200: { + content: { + "application/json": {schema: responseSchema}, + }, + description: "Response message", + }, + 400: { + content: { + "application/json": { + schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}), + }, + }, + description: "Internal Server Error", + }, + 401: { + content: { + "application/json": { + schema: z.object({message: z.string().optional().openapi({example: "Unauthenticated"})}), + }, + }, + description: "Unauthorized", + }, + 500: { + content: { + "application/json": { + schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}), + }, + }, + description: "Internal Server Error", + }, + }, + }), + async (c) => { + const {printer} = c.req.valid("param"); + + const contentType = c.req.header("Content-Type") || ""; + const boundaryMatch = contentType.match(/boundary=(.*)$/); + + if (!boundaryMatch) { + return c.json({message: "No boundary found in Content-Type"}, 400); + } + + const boundary = boundaryMatch[1]; + const body = await c.req.text(); // Get the body of the request + + // Split the body by the boundary (adding extra dashes before the boundary) + const parts = body.split(`--${boundary}`); + console.log(parts); + // Remove the first and last empty parts (they are just before and after the boundaries) + const formDataParts = parts.slice(1, parts.length - 1); + + // console.log(formDataParts); + + // formDataParts.forEach((part, index) => { + // // Split the part into headers and body + // const [headers, data] = part.split("\r\n\r\n"); + + // // Log the part index and data for debugging + // console.log(`Part ${index + 1}:`); + // console.log("Headers:", headers); + // console.log("Data:", decodeURIComponent(data)); + // }); + + return c.json({success: true, message: `New info from ${printer}`}, 200); + } +); + +export default app;