refactor(server): removed the websocket wrapper going wiht normal ws
This commit is contained in:
2
database/migrations/0025_amusing_sugar_man.sql
Normal file
2
database/migrations/0025_amusing_sugar_man.sql
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
DROP TABLE "apiHits" CASCADE;--> statement-breakpoint
|
||||||
|
DROP TABLE "eom" CASCADE;
|
||||||
1056
database/migrations/meta/0025_snapshot.json
Normal file
1056
database/migrations/meta/0025_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -176,6 +176,13 @@
|
|||||||
"when": 1742408812383,
|
"when": 1742408812383,
|
||||||
"tag": "0024_curved_venom",
|
"tag": "0024_curved_venom",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 25,
|
||||||
|
"version": "7",
|
||||||
|
"when": 1742655504936,
|
||||||
|
"tag": "0025_amusing_sugar_man",
|
||||||
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
192
server/index.ts
192
server/index.ts
@@ -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 { 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";
|
||||||
|
|
||||||
// custom routes
|
// custom routes
|
||||||
import scalar from "./services/general/route/scalar.js";
|
import scalar from "./services/general/route/scalar.js";
|
||||||
@@ -19,11 +19,9 @@ 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 loggerService from "./services/logger/loggerService.js";
|
||||||
import ocpService from "./services/ocp/ocpService.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 } from "drizzle-orm";
|
||||||
import {createNodeWebSocket} from "@hono/node-ws";
|
|
||||||
import {streamSSE} from "hono/streaming";
|
|
||||||
|
|
||||||
// create the main prodlogin here
|
// create the main prodlogin here
|
||||||
const username = "lst_user";
|
const username = "lst_user";
|
||||||
@@ -31,97 +29,66 @@ const password = "Alpla$$Prod";
|
|||||||
export const lstAuth = btoa(`${username}:${password}`);
|
export const lstAuth = btoa(`${username}:${password}`);
|
||||||
|
|
||||||
// checking to make sure we have the settings intialized
|
// checking to make sure we have the settings intialized
|
||||||
const serverIntialized = await db.select({count: count()}).from(settings);
|
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 app = new OpenAPIHono({strict: false});
|
const app = new OpenAPIHono({ strict: false });
|
||||||
export const {injectWebSocket, upgradeWebSocket} = createNodeWebSocket({app});
|
|
||||||
|
|
||||||
// middle ware
|
// middle ware
|
||||||
app.use("*", logger());
|
app.use("*", logger());
|
||||||
app.use(
|
app.use(
|
||||||
"*",
|
"*",
|
||||||
cors({
|
cors({
|
||||||
origin: "*", // Allow all origins
|
origin: "*", // Allow all origins
|
||||||
allowHeaders: ["Content-Type", "Authorization", "X-Requested-With"],
|
allowHeaders: ["Content-Type", "Authorization", "X-Requested-With"],
|
||||||
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"],
|
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
|
credentials: true, // Allow credentials if needed
|
||||||
maxAge: 600,
|
maxAge: 600,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
// 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);
|
//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();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, re-route internally
|
// Otherwise, re-route internally
|
||||||
return c.redirect(lowercasedUrl, 308); // 308 preserves the HTTP method
|
return c.redirect(lowercasedUrl, 308); // 308 preserves the HTTP method
|
||||||
});
|
});
|
||||||
|
|
||||||
app.doc("/api/ref", {
|
app.doc("/api/ref", {
|
||||||
openapi: "3.0.0",
|
openapi: "3.0.0",
|
||||||
info: {
|
info: {
|
||||||
version: "2.0.0",
|
version: "2.0.0",
|
||||||
title: "LST API",
|
title: "LST API",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get(
|
|
||||||
"/api/log",
|
|
||||||
// upgradeWebSocket(() => {
|
|
||||||
// return {
|
|
||||||
// onMessage(event, ws) {
|
|
||||||
// console.log(`Message from client: ${event.data}`);
|
|
||||||
// ws.send("Hello from server!");
|
|
||||||
// },
|
|
||||||
// onClose: () => {
|
|
||||||
// console.log("Connection closed");
|
|
||||||
// },
|
|
||||||
// };
|
|
||||||
// })
|
|
||||||
async (c) => {
|
|
||||||
let id = 0;
|
|
||||||
let running = true;
|
|
||||||
return streamSSE(c, async (stream) => {
|
|
||||||
while (running) {
|
|
||||||
const message = `It is ${new Date().toISOString()}`;
|
|
||||||
await stream.writeSSE({
|
|
||||||
data: message,
|
|
||||||
event: "time-update",
|
|
||||||
id: String(id++),
|
|
||||||
});
|
|
||||||
await stream.sleep(1000);
|
|
||||||
if (id === 5) {
|
|
||||||
running = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const routes = [
|
const routes = [
|
||||||
scalar,
|
scalar,
|
||||||
auth,
|
auth,
|
||||||
// apiHits,
|
// apiHits,
|
||||||
system,
|
system,
|
||||||
tcpServer,
|
tcpServer,
|
||||||
sqlService,
|
sqlService,
|
||||||
logistics,
|
logistics,
|
||||||
rfid,
|
rfid,
|
||||||
printers,
|
printers,
|
||||||
loggerService,
|
loggerService,
|
||||||
ocpService,
|
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);
|
app.route("/ocme/", ocme);
|
||||||
@@ -154,48 +121,55 @@ app.route("/ocme/", ocme);
|
|||||||
//---------------------------------------------------\\
|
//---------------------------------------------------\\
|
||||||
|
|
||||||
// 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));
|
||||||
|
|
||||||
// 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" }));
|
||||||
|
|
||||||
// Handle app exit signals
|
// Handle app exit signals
|
||||||
process.on("SIGINT", async () => {
|
process.on("SIGINT", async () => {
|
||||||
console.log("\nGracefully shutting down...");
|
console.log("\nGracefully shutting down...");
|
||||||
//await closePool();
|
//await closePool();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on("SIGTERM", async () => {
|
process.on("SIGTERM", async () => {
|
||||||
console.log("Received termination signal, closing database...");
|
console.log("Received termination signal, closing database...");
|
||||||
//await closePool();
|
//await closePool();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on("uncaughtException", async (err) => {
|
process.on("uncaughtException", async (err) => {
|
||||||
console.log("Uncaught Exception:", err);
|
console.log("Uncaught Exception:", err);
|
||||||
//await closePool();
|
//await closePool();
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on("beforeExit", async () => {
|
process.on("beforeExit", async () => {
|
||||||
console.log("Process is about to exit...");
|
console.log("Process is about to exit...");
|
||||||
//await closePool();
|
//await closePool();
|
||||||
});
|
});
|
||||||
|
|
||||||
const port = process.env.NODE_ENV === "development" ? process.env.VITE_SERVER_PORT : process.env.PROD_PORT;
|
const port =
|
||||||
injectWebSocket(
|
process.env.NODE_ENV === "development"
|
||||||
serve(
|
? process.env.VITE_SERVER_PORT
|
||||||
{
|
: process.env.PROD_PORT;
|
||||||
fetch: app.fetch,
|
|
||||||
port: Number(port),
|
serve(
|
||||||
hostname: "0.0.0.0",
|
{
|
||||||
},
|
fetch: app.fetch,
|
||||||
(info) => {
|
port: Number(port),
|
||||||
createLog("info", "LST", "server", `Server is running on http://${info.address}:${info.port}`);
|
hostname: "0.0.0.0",
|
||||||
}
|
},
|
||||||
)
|
(info) => {
|
||||||
|
createLog(
|
||||||
|
"info",
|
||||||
|
"LST",
|
||||||
|
"server",
|
||||||
|
`Server is running on http://${info.address}:${info.port}`
|
||||||
|
);
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
export type AppRoutes = typeof appRoutes;
|
export type AppRoutes = typeof appRoutes;
|
||||||
|
|||||||
Reference in New Issue
Block a user