Initial commit
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
! /servers/gateway/functions.js – Central Gateway Server Functions
|
||||
? This modules exports the functionality for the express gateway server
|
||||
*/
|
||||
|
||||
// % import nodejs dependencies
|
||||
const WebSocket = require('ws');
|
||||
|
||||
// % core variables
|
||||
const ip = process.env.INTERNAL_HOST_IP;
|
||||
const port = process.env.INTERNAL_GATEWAY_PORT;
|
||||
|
||||
// % create a reference for the websocket client
|
||||
let wsClient = null;
|
||||
|
||||
// % create an event emitter for the gateway
|
||||
const EventEmitter = require('events');
|
||||
const gatewayEvents = new EventEmitter();
|
||||
|
||||
// $ helper: send messages through websocket
|
||||
function sendToWs(payload) {
|
||||
if (wsClient && wsClient.readyState === WebSocket.OPEN) {
|
||||
wsClient.send(JSON.stringify(payload));
|
||||
} else {
|
||||
console.error('WebSocket not connected.');
|
||||
}
|
||||
}
|
||||
|
||||
// $ server start function
|
||||
async function boot(server) {
|
||||
// ~ start express server
|
||||
server.listen(port, ip, () => {
|
||||
console.info(`Express server is now running on http://${ip}:${port}`);
|
||||
});
|
||||
|
||||
// ~ connect to the websocket server
|
||||
const wsUrl = `ws://${process.env.INTERNAL_HOST_IP}:${process.env.INTERNAL_WEBSOCKET_PORT}?key=${process.env.INTERNAL_SECRET}&codename=gateway`;
|
||||
wsClient = new WebSocket(wsUrl);
|
||||
|
||||
wsClient.on('open', () => {
|
||||
console.info('Connected to central websocket server');
|
||||
});
|
||||
|
||||
wsClient.on('message', (data) => {
|
||||
const msg = JSON.parse(data);
|
||||
gatewayEvents.emit('ws_message', msg);
|
||||
});
|
||||
|
||||
wsClient.on('error', (err) => {
|
||||
console.error('WebSocket error:', err.message);
|
||||
});
|
||||
|
||||
// ~ return running express server reference
|
||||
return server;
|
||||
};
|
||||
|
||||
// $ server shutdown function
|
||||
async function shutdown(server) {
|
||||
// ~ close websocket server
|
||||
if (wsClient) {
|
||||
wsClient.close();
|
||||
wsClient = null;
|
||||
}
|
||||
|
||||
// ~ shutdown running express server
|
||||
return new Promise((resolve) => {
|
||||
server.close(() => {
|
||||
console.info('Express server shut down.');
|
||||
resolve(server);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// $ server restart function
|
||||
async function restart(server) {
|
||||
// ~ shutdown the running express server
|
||||
await shutdown(server);
|
||||
|
||||
// ~ restart the express server
|
||||
await boot(server);
|
||||
|
||||
// ~ return the server reference
|
||||
return server;
|
||||
};
|
||||
|
||||
// $ server status function
|
||||
async function status(server) {
|
||||
// ~ get and return server listening status
|
||||
return server.listening ? 'online' : 'offline';
|
||||
};
|
||||
|
||||
// § export server functions module
|
||||
module.exports = { boot, shutdown, restart, status, sendToWs, gatewayEvents };
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
! /servers/gateway/routers/index.js – Central Gateway Routing Aggregator
|
||||
? This module dynamically collects and registeres all modular ".router.js" files within the "routers" directory
|
||||
*/
|
||||
|
||||
// % import nodejs packages
|
||||
const router = require('express').Router();
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
// % import external dependencies
|
||||
const rootDir = path.resolve(__dirname, '..', '..', '..');
|
||||
const { dirs } = require(path.resolve(rootDir, 'configuration', 'mapping.js'));
|
||||
|
||||
// % read router files recursively
|
||||
const routerRoot = path.resolve(dirs.gatewayDir, 'routers');
|
||||
const routerFiles = fs.readdirSync(routerRoot)
|
||||
.filter(file => /\.router\.js$/.test(file))
|
||||
.sort((a, b) => {
|
||||
if (a === 'root.router.js') return 1;
|
||||
if (b === 'root.router.js') return -1;
|
||||
return a.localeCompare(b);
|
||||
});
|
||||
|
||||
// $ configure used routers
|
||||
routerFiles.forEach(file => {
|
||||
const routerPath = path.resolve(routerRoot, file);
|
||||
router.use(require(routerPath));
|
||||
});
|
||||
|
||||
// § export router module
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
! /servers/gateway/routers/template.router.js –
|
||||
*/
|
||||
|
||||
// % import nodejs dependencies
|
||||
const router = require('express').Router();
|
||||
|
||||
// % import external dependencies
|
||||
const rootDir = path.resolve(__dirname, '..', '..', '..');
|
||||
const { dirs } = require(path.resolve(rootDir, 'configuration', 'mapping.js'));
|
||||
const { gatewayEvents, sendToWs } = path.resolve(dirs.gateway, 'functions.js');
|
||||
|
||||
// $ template router with websocket integration
|
||||
router.post('/test', (req, res) => {
|
||||
const correlationId = '';
|
||||
|
||||
sendToWs({
|
||||
type: 'actionName',
|
||||
correlationId,
|
||||
data: req.body
|
||||
});
|
||||
|
||||
// ~ wait for callback through central event bus
|
||||
gatewayEvents.once('ws_message', (msg) => {
|
||||
if (msg.correlationId === correlationId) res.json(msg.data);
|
||||
});
|
||||
});
|
||||
|
||||
// § export routers
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
! /servers/gateway/server.js – Central Gateway Server Manager
|
||||
? This module orchestrates the express lifecycle, handles initialization, middleware configs and router integrations
|
||||
*/
|
||||
|
||||
// % import nodejs packages
|
||||
const express = require('express');
|
||||
const http = require('http');
|
||||
const path = require('path');
|
||||
|
||||
// % import external dependencies
|
||||
const rootDir = path.resolve(__dirname, '..', '..');
|
||||
const { dirs } = require(path.resolve(rootDir, 'configuration', 'mapping.js'));
|
||||
|
||||
// % set up express server
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
|
||||
// $ server setup
|
||||
app.use(express.json()); // add json support for express
|
||||
|
||||
// $ router setup
|
||||
const router = require(path.resolve(dirs.gatewayDir, 'routers'));
|
||||
app.use(router);
|
||||
|
||||
// $ server functions
|
||||
const {
|
||||
boot, shutdown, restart, status
|
||||
} = require(path.resolve(dirs.gatewayDir, 'functions.js'));
|
||||
|
||||
// $ export server module
|
||||
module.exports = { boot, shutdown, restart, status, server };
|
||||
Reference in New Issue
Block a user