feat(server): ocpService and loggerService added
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
|
||||||
import {serve} from "@hono/node-server";
|
import {serve} from "@hono/node-server";
|
||||||
import {OpenAPIHono} from "@hono/zod-openapi";
|
import {OpenAPIHono} from "@hono/zod-openapi";
|
||||||
|
import {proxy} from "hono/proxy";
|
||||||
import {serveStatic} from "@hono/node-server/serve-static";
|
import {serveStatic} from "@hono/node-server/serve-static";
|
||||||
import {logger} from "hono/logger";
|
import {logger} from "hono/logger";
|
||||||
import {cors} from "hono/cors";
|
import {cors} from "hono/cors";
|
||||||
import {createLog} from "./services/logger/logger.js";
|
import {createLog} from "./services/logger/logger.js";
|
||||||
import {closePool} from "./services/sqlServer/prodSqlServer.js";
|
|
||||||
|
|
||||||
// custom routes
|
// custom routes
|
||||||
import scalar from "./services/general/route/scalar.js";
|
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 logistics from "./services/logistics/logisticsService.js";
|
||||||
import rfid from "./services/rfid/rfidService.js";
|
import rfid from "./services/rfid/rfidService.js";
|
||||||
import printers from "./services/printers/printerService.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 {db} from "../database/dbclient.js";
|
||||||
import {settings} from "../database/schema/settings.js";
|
import {settings} from "../database/schema/settings.js";
|
||||||
import {count, eq} from "drizzle-orm";
|
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;
|
export const installed = serverIntialized[0].count === 0 && process.env.NODE_ENV !== "development" ? false : true;
|
||||||
createLog("info", "LST", "server", `Server is installed: ${installed}`);
|
createLog("info", "LST", "server", `Server is installed: ${installed}`);
|
||||||
|
|
||||||
const allowedOrigins = [
|
const app = new OpenAPIHono({strict: false});
|
||||||
"http://localhost:3000",
|
|
||||||
"http://localhost:4000",
|
|
||||||
"http://localhost:5173",
|
|
||||||
`http://usmcd1vms006:4000`,
|
|
||||||
];
|
|
||||||
const app = new OpenAPIHono();
|
|
||||||
|
|
||||||
// middle ware
|
// middle ware
|
||||||
app.use("*", logger());
|
app.use("*", logger());
|
||||||
app.use(
|
app.use(
|
||||||
"*",
|
"*",
|
||||||
cors({
|
cors({
|
||||||
origin: allowedOrigins,
|
origin: "*", // Allow all origins
|
||||||
allowHeaders: ["X-Custom-Header", "Upgrade-Insecure-Requests"],
|
allowHeaders: ["Content-Type", "Authorization", "X-Requested-With"],
|
||||||
allowMethods: ["POST", "GET", "OPTIONS"],
|
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"],
|
||||||
exposeHeaders: ["Content-Length", "X-Kuma-Revision"],
|
exposeHeaders: ["Content-Length", "X-Kuma-Revision"],
|
||||||
|
credentials: true, // Allow credentials if needed
|
||||||
maxAge: 600,
|
maxAge: 600,
|
||||||
credentials: true,
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
// Middleware to normalize route case
|
// Middleware to normalize route case
|
||||||
app.use("*", async (c, next) => {
|
app.use("*", async (c, next) => {
|
||||||
const lowercasedUrl = c.req.url.toLowerCase();
|
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 the URL is already lowercase, continue as usual
|
||||||
if (c.req.url === lowercasedUrl) {
|
if (c.req.url === lowercasedUrl) {
|
||||||
return next();
|
return next();
|
||||||
@@ -85,22 +80,46 @@ const routes = [
|
|||||||
logistics,
|
logistics,
|
||||||
rfid,
|
rfid,
|
||||||
printers,
|
printers,
|
||||||
|
loggerService,
|
||||||
|
ocpService,
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
const appRoutes = routes.forEach((route) => {
|
const appRoutes = routes.forEach((route) => {
|
||||||
app.route("/api/", 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
|
// the catch all api route
|
||||||
app.all("/api/*", (c) => c.json({error: "API route not found"}, 404));
|
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
|
// front end static files
|
||||||
app.use("/*", serveStatic({root: "./frontend/dist"}));
|
app.use("/*", serveStatic({root: "./frontend/dist"}));
|
||||||
app.use("*", serveStatic({path: "./frontend/dist/index.html"}));
|
app.use("*", serveStatic({path: "./frontend/dist/index.html"}));
|
||||||
|
|||||||
Reference in New Issue
Block a user