Add base HTTP server that responds to IO pings.
This commit is contained in:
121
src/main/http-server/http-server.ts
Normal file
121
src/main/http-server/http-server.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import cors from "cors";
|
||||
import { app } from "electron";
|
||||
import log from "electron-log/main";
|
||||
import express from "express";
|
||||
|
||||
export default class LocalServer {
|
||||
private app: express.Application;
|
||||
private server: any;
|
||||
private PORT = 1337;
|
||||
|
||||
constructor() {
|
||||
this.app = express();
|
||||
this.configureMiddleware();
|
||||
this.configureRoutes();
|
||||
}
|
||||
|
||||
private configureMiddleware(): void {
|
||||
const allowedOrigins = [
|
||||
"http://localhost",
|
||||
"https://localhost",
|
||||
"http://localhost:3000",
|
||||
"https://localhost:3000",
|
||||
"https://test.imex.online",
|
||||
"https://imex.online",
|
||||
];
|
||||
|
||||
this.app.use(
|
||||
cors({
|
||||
origin: (origin, callback) => {
|
||||
// Allow requests with no origin (like mobile apps, curl requests)
|
||||
if (!origin) return callback(null, true);
|
||||
|
||||
if (allowedOrigins.indexOf(origin) !== -1) {
|
||||
return callback(null, true);
|
||||
} else {
|
||||
return callback(null, false);
|
||||
}
|
||||
},
|
||||
credentials: true,
|
||||
}),
|
||||
);
|
||||
|
||||
// Parse JSON bodies
|
||||
this.app.use(express.json());
|
||||
this.app.use(express.urlencoded());
|
||||
|
||||
//Add logger Middleware
|
||||
this.app.use((req, res, next) => {
|
||||
const startTime = Date.now();
|
||||
const requestId = Math.random().toString(36).substring(2, 15);
|
||||
|
||||
// Log request details
|
||||
log.info(
|
||||
`[HTTP Server] [${requestId}] Request: ${req.method} ${req.url}`,
|
||||
);
|
||||
log.info(
|
||||
`[HTTP Server] [${requestId}] Headers: ${JSON.stringify(req.headers)}`,
|
||||
);
|
||||
|
||||
// Log request body if it exists
|
||||
if (req.body && Object.keys(req.body).length > 0) {
|
||||
log.info(
|
||||
`[HTTP Server] [${requestId}] Body: ${JSON.stringify(req.body)}`,
|
||||
);
|
||||
}
|
||||
|
||||
// Capture the original methods
|
||||
const originalSend = res.send;
|
||||
const originalJson = res.json;
|
||||
|
||||
// Override send method to log response
|
||||
res.send = function (body): express.Response {
|
||||
log.info(`[HTTP Server] [${requestId}] Response body: ${body}`);
|
||||
log.info(
|
||||
`[HTTP Server] [${requestId}] Response time: ${Date.now() - startTime}ms`,
|
||||
);
|
||||
return originalSend.call(this, body);
|
||||
};
|
||||
|
||||
// Override json method to log response
|
||||
res.json = function (body): express.Response {
|
||||
log.info(
|
||||
`[HTTP Server] [${requestId}] Response body: ${JSON.stringify(body)}`,
|
||||
);
|
||||
log.info(
|
||||
`[HTTP Server] [${requestId}] Response time: ${Date.now() - startTime}ms`,
|
||||
);
|
||||
return originalJson.call(this, body);
|
||||
};
|
||||
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
private configureRoutes(): void {
|
||||
// Basic health check endpoint
|
||||
this.app.get("/health", (req, res) => {
|
||||
res.status(200).json({ status: "ok" });
|
||||
});
|
||||
this.app.post("/ping", (req, res) => {
|
||||
res.status(200).json({
|
||||
appVer: app.getVersion(),
|
||||
qbPath: app.getPath("userData"), //TODO: Resolve to actual QB file path.
|
||||
});
|
||||
});
|
||||
// Add more routes as needed
|
||||
}
|
||||
|
||||
public start(): void {
|
||||
this.server = this.app.listen(this.PORT, () => {
|
||||
log.info(`[HTTP Server] Local HTTP server running on port ${this.PORT}`);
|
||||
});
|
||||
}
|
||||
|
||||
public stop(): void {
|
||||
if (this.server) {
|
||||
this.server.close();
|
||||
log.info("[HTTP Server] Local HTTP server stopped");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user