91 lines
2.3 KiB
TypeScript
91 lines
2.3 KiB
TypeScript
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
|
|
import {serve} from "@hono/node-server";
|
|
import {OpenAPIHono} from "@hono/zod-openapi";
|
|
import {serveStatic} from "@hono/node-server/serve-static";
|
|
import {logger} from "hono/logger";
|
|
import {cors} from "hono/cors";
|
|
import {log} from "./services/logger/logger.js";
|
|
|
|
// custom routes
|
|
import scalar from "./services/general/route/scalar.js";
|
|
import system from "./services/server/systemServer.js";
|
|
import auth from "./services/auth/authService.js";
|
|
import tcpServer from "./services/tcpServer/tcpServer.js";
|
|
import ocme from "./services/ocme/ocmeService.js";
|
|
|
|
const allowedOrigins = ["http://localhost:3000", "http://localhost:4000", "http://localhost:5173"];
|
|
const app = new OpenAPIHono();
|
|
|
|
// middle ware
|
|
app.use("*", logger());
|
|
app.use(
|
|
"*",
|
|
cors({
|
|
origin: allowedOrigins,
|
|
allowHeaders: ["X-Custom-Header", "Upgrade-Insecure-Requests"],
|
|
allowMethods: ["POST", "GET", "OPTIONS"],
|
|
exposeHeaders: ["Content-Length", "X-Kuma-Revision"],
|
|
maxAge: 600,
|
|
credentials: true,
|
|
})
|
|
);
|
|
|
|
// Middleware to normalize route case
|
|
app.use("*", async (c, next) => {
|
|
const lowercasedUrl = c.req.url.toLowerCase();
|
|
|
|
// If the URL is already lowercase, continue as usual
|
|
if (c.req.url === lowercasedUrl) {
|
|
return next();
|
|
}
|
|
|
|
// Otherwise, re-route internally
|
|
return c.redirect(lowercasedUrl, 308); // 308 preserves the HTTP method
|
|
});
|
|
|
|
app.doc("/api/ref", {
|
|
openapi: "3.0.0",
|
|
info: {
|
|
version: "1.0.0",
|
|
title: "LST API",
|
|
},
|
|
});
|
|
|
|
const routes = [
|
|
scalar,
|
|
auth,
|
|
// apiHits,
|
|
system,
|
|
tcpServer,
|
|
] as const;
|
|
|
|
const appRoutes = routes.forEach((route) => {
|
|
app.route("/api/", route);
|
|
});
|
|
|
|
// 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"}));
|
|
|
|
serve(
|
|
{
|
|
fetch: app.fetch,
|
|
port: Number(process.env.VITE_SERVER_PORT),
|
|
},
|
|
(info) => {
|
|
log.info({username: "LST-SYSTEM"}, `Server is running on http://localhost:${info.port}`);
|
|
}
|
|
);
|
|
|
|
export type AppRoutes = typeof appRoutes;
|