From 2d3f30887744bddfe22dc764188cf727a5e476df Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Wed, 19 Mar 2025 17:13:52 -0500 Subject: [PATCH] feat(server): ocpService and loggerService added --- server/index.ts | 61 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/server/index.ts b/server/index.ts index 7d2f0e8..d42fdef 100644 --- a/server/index.ts +++ b/server/index.ts @@ -1,11 +1,11 @@ process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; import {serve} from "@hono/node-server"; import {OpenAPIHono} from "@hono/zod-openapi"; +import {proxy} from "hono/proxy"; import {serveStatic} from "@hono/node-server/serve-static"; import {logger} from "hono/logger"; import {cors} from "hono/cors"; import {createLog} from "./services/logger/logger.js"; -import {closePool} from "./services/sqlServer/prodSqlServer.js"; // custom routes import scalar from "./services/general/route/scalar.js"; @@ -17,7 +17,8 @@ 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 loggerService from "./services/logger/loggerService.js"; +import ocpService from "./services/ocp/ocpService.js"; import {db} from "../database/dbclient.js"; import {settings} from "../database/schema/settings.js"; import {count, eq} from "drizzle-orm"; @@ -32,32 +33,26 @@ const serverIntialized = await db.select({count: count()}).from(settings); export const installed = serverIntialized[0].count === 0 && process.env.NODE_ENV !== "development" ? false : true; createLog("info", "LST", "server", `Server is installed: ${installed}`); -const allowedOrigins = [ - "http://localhost:3000", - "http://localhost:4000", - "http://localhost:5173", - `http://usmcd1vms006:4000`, -]; -const app = new OpenAPIHono(); +const app = new OpenAPIHono({strict: false}); // middle ware app.use("*", logger()); app.use( "*", cors({ - origin: allowedOrigins, - allowHeaders: ["X-Custom-Header", "Upgrade-Insecure-Requests"], - allowMethods: ["POST", "GET", "OPTIONS"], + origin: "*", // Allow all origins + allowHeaders: ["Content-Type", "Authorization", "X-Requested-With"], + allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"], exposeHeaders: ["Content-Length", "X-Kuma-Revision"], + credentials: true, // Allow credentials if needed maxAge: 600, - credentials: true, }) ); // Middleware to normalize route case app.use("*", async (c, next) => { const lowercasedUrl = c.req.url.toLowerCase(); - + //console.log("Incoming Request:", c.req.url, c.req.method); // If the URL is already lowercase, continue as usual if (c.req.url === lowercasedUrl) { return next(); @@ -85,22 +80,46 @@ const routes = [ logistics, rfid, printers, + loggerService, + ocpService, ] as const; const appRoutes = routes.forEach((route) => { app.route("/api/", route); }); +app.route("/ocme/*", ocme); + +//--------------- lst v1 proxy ----------------------\\ +app.all("/api/v1/*", (c) => { + const path = c.req.path.replace("/api/v1/", ""); // Extract the subpath + const query = c.req.query() ? "?" + new URLSearchParams(c.req.query()).toString() : ""; // Get query params + return proxy(`http://localhost:4900/${path}${query}`, { + headers: { + ...c.req.header(), + "X-Forwarded-For": "127.0.0.1", + "X-Forwarded-Host": c.req.header("host"), + }, + }); +}); + +app.all("/system/*", (c) => { + const path = c.req.path.replace("/system/", ""); // Extract the subpath + const query = c.req.query() ? "?" + new URLSearchParams(c.req.query()).toString() : ""; // Get query params + return proxy(`http://localhost:4200/${path}${query}`, { + headers: { + ...c.req.header(), + "X-Forwarded-For": "127.0.0.1", + "X-Forwarded-Host": c.req.header("host"), + }, + }); +}); + +//---------------------------------------------------\\ + // the catch all api route app.all("/api/*", (c) => c.json({error: "API route not found"}, 404)); -app.route("/ocme/", ocme); - -// async (c) => { -// //return ocmeService(c); -// c.json({error: "Ocme route not found"}, 404); -// }); - // front end static files app.use("/*", serveStatic({root: "./frontend/dist"})); app.use("*", serveStatic({path: "./frontend/dist/index.html"}));