feat(server): ocpService and loggerService added
This commit is contained in:
@@ -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"}));
|
||||
|
||||
Reference in New Issue
Block a user