From f00170258015265d75c3be928e30145515ac34f0 Mon Sep 17 00:00:00 2001 From: Sui Gn Date: Fri, 15 Mar 2024 01:06:09 -0500 Subject: [PATCH] - - --- docs/Gateway.html | 4 +- docs/index.js.html | 4 +- docs/module-index.html | 3 +- docs/src_Gateway.js.html | 23 +- docs/src_routes_defaultRoutes.js.html | 204 ++++++++++++++++++ docs/tests_test.js.html | 9 +- index.js | 4 +- jsdoc.json | 2 +- package.json | 2 +- src/Gateway.js | 23 +- src/ejsApp/views/index.ejs | 13 ++ .../defaultRoutes.js} | 12 +- tests/test.js | 9 +- 13 files changed, 284 insertions(+), 28 deletions(-) create mode 100644 docs/src_routes_defaultRoutes.js.html rename src/{handlers/defaultHandler.js => routes/defaultRoutes.js} (55%) diff --git a/docs/Gateway.html b/docs/Gateway.html index 23305b2..db26ce3 100644 --- a/docs/Gateway.html +++ b/docs/Gateway.html @@ -587,7 +587,7 @@

View Source - src/gateway.js, line 52 + src/gateway.js, line 65

@@ -1093,7 +1093,7 @@

View Source - src/gateway.js, line 52 + src/gateway.js, line 65

diff --git a/docs/index.js.html b/docs/index.js.html index ca3e2a9..bfc8647 100644 --- a/docs/index.js.html +++ b/docs/index.js.html @@ -132,8 +132,8 @@

index.js

/**
  * Entry point of the application.
- * It exports all NetGet tools as default NetGet
- * Gateway is a customizable gateway server.
+ * default exports NetGet
+ * 
  * @module index
  */
 
diff --git a/docs/module-index.html b/docs/module-index.html
index ecfe880..fbd6afd 100644
--- a/docs/module-index.html
+++ b/docs/module-index.html
@@ -137,8 +137,7 @@ 

index

Entry point of the application. -It exports all NetGet tools as default NetGet -Gateway is a customizable gateway server.
+default exports NetGet diff --git a/docs/src_Gateway.js.html b/docs/src_Gateway.js.html index 0e57d77..e0c79b9 100644 --- a/docs/src_Gateway.js.html +++ b/docs/src_Gateway.js.html @@ -133,7 +133,7 @@

src/gateway.js

import express from 'express';
 import path from 'path';
 import morgan from 'morgan';
-import defaultHandler from './handlers/defaultHandler.js';
+import defaultRoutes from './handlers/defaultRoutes.js';
 import { fileURLToPath } from 'url';
 
 // Determine the base directory for static file serving and view engine setup
@@ -151,7 +151,7 @@ 

src/gateway.js

*/ constructor({ port = 3432, handlers = {} } = {}) { this.port = port; - this.handlers = handlers; + this.routes = routes; this.app = express(); this.initialize().catch(err => console.error('Initialization error:', err)); } @@ -165,11 +165,17 @@

src/gateway.js

this.app.use(express.static(path.join(baseDir, 'ejsApp', 'public'))); this.app.set('view engine', 'ejs'); this.app.set('views', path.join(baseDir, 'ejsApp', 'views')); - morgan.token('host', (req) => req.hostname || req.headers['host'] || '-'); this.app.use(morgan(':method :url :status :res[content-length] - :response-time ms - Host: :host')); - // This middleware checks the request's hostname and uses the corresponding handler or the default one + /* This middleware checks the request's ::::::::hostname::::::and uses the corresponding handler or the default one. + + Hard-coded and Dynamic Domain Handling in the Gateway , + Define two methods. + 1. Static object this.handlers for domains with predefined handlers. + 2. Fetch Handler configurations dynamically from a PostgreSQL database v.path.mlisa.me, + for domains that require dynamic content serving, such as browser.pixelgrid.me/html.js. + this.app.use((req, res) => { // Check if handlers object is empty (no handlers defined at all) const noHandlersDefined = Object.keys(this.handlers).length === 0; @@ -177,7 +183,14 @@

src/gateway.js

handler(req, res); }); } - +*/ + this.app.use((req, res) => { + // Check if handlers object is empty (no handlers defined at all) + const noRoutesDefined = Object.keys(this.handlers).length === 0; + const routes = this.routes[req.hostname] || ((req, res) => defaultRoutes(req, res, noHRoutesDefined)); + handler(req, res); + }); + } /** * Starts the gateway, making it listen on the configured port. */ diff --git a/docs/src_routes_defaultRoutes.js.html b/docs/src_routes_defaultRoutes.js.html new file mode 100644 index 0000000..335f08e --- /dev/null +++ b/docs/src_routes_defaultRoutes.js.html @@ -0,0 +1,204 @@ + + + + + + + ... + + + NetGet src/routes/defaultRoutes.js + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+
+
+

Source

+

src/routes/defaultRoutes.js

+
+ + + + + +
+
+
// ./src/handlers/defaultHandler.js
+/**
+ * Renders a default response when no specific handler is found for a request.
+ * This function serves as a fallback handler to provide informative feedback to the user or administrator.
+ * It indicates whether no handlers are defined at all or if a specific handler for the requested domain is missing.
+ * 
+ * @param {Object} req - The HTTP request object provided by Express.js.
+ * @param {Object} res - The HTTP response object provided by Express.js.
+ * @param {boolean} [noRoutessDefined=false] - Indicates whether no handlers have been defined in the gateway.
+ */
+export default function defaultRoutes(req, res, noRoutesDefined = false) {
+  let message = " ";
+  if (noRoutesDefined) {
+    message += " No Routes have been defined.<br/> To configure your Domain Routes, please refer to Docs: <br/>  https://netget.me ";
+  } else {
+    message += ` No Route found for ${req.hostname}.<br/> If you are the administrator, please define a route for this domain. <br/>  https://netget.me `;
+  }
+
+  const showDomainListLink = !noRoutesDefined;
+
+  res.render('index', {
+      title: "Gateway Initiated!",
+      message: message,
+      showDomainListLink: showDomainListLink
+  });
+}
+
+
+
+ + + + +
+ +
+
+

+ +

+ Witness our Seal. +
+neurons.me +

+
+
+ +
+
+
+
+ + + + + + + + + diff --git a/docs/tests_test.js.html b/docs/tests_test.js.html index dcc094a..0db79e7 100644 --- a/docs/tests_test.js.html +++ b/docs/tests_test.js.html @@ -153,7 +153,14 @@

tests/test.js

*/ const testGateway = new Gateway({ port: 3000, - domainsConfigPath: './config/domains.json' + routes: { + 'example.com': (req, res) => { + res.send('Hello from example.com!'); + }, + 'another.com': (req, res) => { + res.send('Hello from another.com!'); + } + } }); testGateway.listen();
diff --git a/index.js b/index.js index a4e32da..fe9e4b7 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ /** * Entry point of the application. - * It exports all NetGet tools as default NetGet - * Gateway is a customizable gateway server. + * default exports NetGet + * * @module index */ diff --git a/jsdoc.json b/jsdoc.json index ecea75a..01ea60a 100644 --- a/jsdoc.json +++ b/jsdoc.json @@ -1,6 +1,6 @@ { "source": { - "include": ["./src/","./src/gateway.js", "./src/netget.js", "./src/handlers/defaultHandler.js", "./src/routes/routes.js", "./index.js", "./tests/"] + "include": ["./src/","./src/gateway.js", "./src/netget.js", "./src/routes/defaultRoutes.js", "./src/routes/routes.js", "./index.js", "./tests/"] }, "opts": { "destination": "./docs", diff --git a/package.json b/package.json index 3c8d654..671cef6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "netget", - "version": "2.3.34", + "version": "2.3.35", "description": "Rette Adepto/ Recibido Directamente.", "type": "module", "main": "index.js", diff --git a/src/Gateway.js b/src/Gateway.js index a635cce..f75f17c 100644 --- a/src/Gateway.js +++ b/src/Gateway.js @@ -1,7 +1,7 @@ import express from 'express'; import path from 'path'; import morgan from 'morgan'; -import defaultHandler from './handlers/defaultHandler.js'; +import defaultRoutes from './handlers/defaultRoutes.js'; import { fileURLToPath } from 'url'; // Determine the base directory for static file serving and view engine setup @@ -19,7 +19,7 @@ class Gateway { */ constructor({ port = 3432, handlers = {} } = {}) { this.port = port; - this.handlers = handlers; + this.routes = routes; this.app = express(); this.initialize().catch(err => console.error('Initialization error:', err)); } @@ -33,11 +33,17 @@ class Gateway { this.app.use(express.static(path.join(baseDir, 'ejsApp', 'public'))); this.app.set('view engine', 'ejs'); this.app.set('views', path.join(baseDir, 'ejsApp', 'views')); - morgan.token('host', (req) => req.hostname || req.headers['host'] || '-'); this.app.use(morgan(':method :url :status :res[content-length] - :response-time ms - Host: :host')); - // This middleware checks the request's hostname and uses the corresponding handler or the default one + /* This middleware checks the request's ::::::::hostname::::::and uses the corresponding handler or the default one. + + Hard-coded and Dynamic Domain Handling in the Gateway , + Define two methods. + 1. Static object this.handlers for domains with predefined handlers. + 2. Fetch Handler configurations dynamically from a PostgreSQL database v.path.mlisa.me, + for domains that require dynamic content serving, such as browser.pixelgrid.me/html.js. + this.app.use((req, res) => { // Check if handlers object is empty (no handlers defined at all) const noHandlersDefined = Object.keys(this.handlers).length === 0; @@ -45,7 +51,14 @@ class Gateway { handler(req, res); }); } - +*/ + this.app.use((req, res) => { + // Check if handlers object is empty (no handlers defined at all) + const noRoutesDefined = Object.keys(this.handlers).length === 0; + const routes = this.routes[req.hostname] || ((req, res) => defaultRoutes(req, res, noHRoutesDefined)); + handler(req, res); + }); + } /** * Starts the gateway, making it listen on the configured port. */ diff --git a/src/ejsApp/views/index.ejs b/src/ejsApp/views/index.ejs index e9c02bd..34d30f2 100644 --- a/src/ejsApp/views/index.ejs +++ b/src/ejsApp/views/index.ejs @@ -98,6 +98,19 @@ <% if (showDomainListLink) { %> View Domain List <% } %> + +
+ + diff --git a/src/handlers/defaultHandler.js b/src/routes/defaultRoutes.js similarity index 55% rename from src/handlers/defaultHandler.js rename to src/routes/defaultRoutes.js index ece00e9..ee2061d 100644 --- a/src/handlers/defaultHandler.js +++ b/src/routes/defaultRoutes.js @@ -6,17 +6,17 @@ * * @param {Object} req - The HTTP request object provided by Express.js. * @param {Object} res - The HTTP response object provided by Express.js. - * @param {boolean} [noHandlersDefined=false] - Indicates whether no handlers have been defined in the gateway. + * @param {boolean} [noRoutessDefined=false] - Indicates whether no handlers have been defined in the gateway. */ -export default function defaultHandler(req, res, noHandlersDefined = false) { +export default function defaultRoutes(req, res, noRoutesDefined = false) { let message = " "; - if (noHandlersDefined) { - message += " No handlers have been defined.
To configure your domain handlers, please refer to Docs:
https://netget.me "; + if (noRoutesDefined) { + message += " No Routes have been defined.
To configure your Domain Routes, please refer to Docs:
https://netget.me "; } else { - message += ` No specific handler found for ${req.hostname}.
If you are the administrator, please define a handler for this domain.`; + message += ` No Route found for ${req.hostname}.
If you are the administrator, please define a route for this domain.
https://netget.me `; } - const showDomainListLink = !noHandlersDefined; + const showDomainListLink = !noRoutesDefined; res.render('index', { title: "Gateway Initiated!", diff --git a/tests/test.js b/tests/test.js index 85aa09c..55bb080 100644 --- a/tests/test.js +++ b/tests/test.js @@ -21,7 +21,14 @@ import Gateway from '../src/gateway.js'; */ const testGateway = new Gateway({ port: 3000, - domainsConfigPath: './config/domains.json' + routes: { + 'example.com': (req, res) => { + res.send('Hello from example.com!'); + }, + 'another.com': (req, res) => { + res.send('Hello from another.com!'); + } + } }); testGateway.listen(); \ No newline at end of file