Comparing version 2.3.33 to 2.3.34
@@ -8,5 +8,3 @@ <img src="https://suign.github.io/assets/imgs/netget.png" alt="netget.me" width="244" height="203"> | ||
### [Project Status : Experimental and Under Development, Subject to Major Changes] | ||
The module is in active development, and as such, it is subject to significant changes as we refine our approach and methodologies to best support our goals. | ||
visit: https://neurons.me to learn more. | ||
@@ -17,3 +15,2 @@ | ||
# Unleash the Cyberspace Within | ||
**NetGet** streamlines the orchestration of digital domains with the simplicity of a pedal's press, enabling seamless symphonies across networked realms. This **modular gateway framework,** designed for Node.js applications, acts as a dynamic conduit, **directing internet traffic to local services** with unparalleled ease. Inspired by the vast digital landscapes of cyberpunk lore, NetGet empowers developers to manage and expand their digital footprints without the complexities traditionally associated with network configurations. | ||
@@ -24,3 +21,2 @@ | ||
## Installation | ||
Install NetGet via npm: | ||
@@ -33,3 +29,2 @@ | ||
## Usage | ||
Import NetGet in your Node.js application: | ||
@@ -53,3 +48,2 @@ | ||
## Configuration | ||
NetGet relies on a `domains.json` file for routing configuration, structured as follows: | ||
@@ -68,27 +62,27 @@ | ||
Each domain key maps to a handler module that exports a function to handle requests for that domain. | ||
By default, NetGet searches for a `domains.json` configuration in the `./config` directory, streamlining the setup process. | ||
**NetGet** is particularly useful in environments where multiple services or applications must be accessible through a single entry point, commonly known as a **reverse proxy setup.** | ||
Scenario: Local and Remote NetGet Interaction | ||
Local NetGet Setup: On your local machine, NetGet operates within your Node.js environment, managing local traffic and processing requests according to your configured rules. It doesn't directly face the internet and instead communicates with an external NetGet instance that does. | ||
**NetGet** is particularly useful in environments where multiple services or applications must be accessible through a single entry point, commonly known as a **reverse proxy setup.** | ||
Remote NetGet as a Reverse Proxy: The main.netget.me hosted on Nginx acts as your gateway to the internet. It receives internet traffic and forwards it to the appropriate local NetGet instance based on the domain routing configured. | ||
Handshaker Mechanism: For machines not directly accessible via a public IP, the remote NetGet instance (main.netget.me) can act as a handshaker. Your local NetGet instance communicates with this handshaker, which then relays traffic between the internet and your local network. | ||
### Scalable Web Services | ||
In a microservices architecture, **NetGet can route requests to different services** within your infrastructure, making it an ideal solution for developers looking to scale their applications horizontally. Each service can have its own domain, and **NetGet** will ensure that requests are forwarded to the correct service. | ||
### Personal Hosting Solutions | ||
For personal web hosting, **NetGet** provides an **easy-to-set-up gateway** for routing traffic to various self-hosted applications. Users with several web applications running on a home server can use **NetGet** to manage access to these applications through different domains. | ||
### Secure Access Control | ||
Combined with authentication layers, NetGet can control access to various parts of a web infrastructure, ensuring that only authorized users can access specific services. | ||
### Simplified Configuration | ||
With NetGet, the complexity of setting up a domain routing system is abstracted away. Users can define their routing logic in a simple JSON configuration file, making the management of domain routes straightforward and maintainable. | ||
### Dynamic Load Balancing | ||
NetGet can be extended to include load balancing capabilities, distributing incoming requests across multiple instances of a service to balance the load and improve performance. | ||
@@ -95,0 +89,0 @@ |
{ | ||
"name": "netget", | ||
"version": "2.3.33", | ||
"version": "2.3.34", | ||
"description": "Rette Adepto/ Recibido Directamente.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -1,79 +0,59 @@ | ||
/* | ||
🅜🅞🅝🅐🅓🅛🅘🅢🅐 | ||
🅃🄷🄴🄶🄰🅃🄴🅆🄰🅈 | ||
ⓝⓔⓤⓡⓞⓝⓢ.ⓜⓔ | ||
🄽🄴🅃🄶🄴🅃 | ||
🆂🆄🅸🅶🅽 | ||
*/ | ||
import express from 'express'; | ||
import path from 'path'; | ||
import fs from 'fs/promises'; | ||
import morgan from 'morgan'; | ||
import defaultHandler from './handlers/defaultHandler.js'; | ||
import { fileURLToPath } from 'url'; | ||
import morgan from 'morgan'; | ||
import initializeRoutes from './routes/routes.js'; | ||
import { loadDomainConfig } from './config/domainConfigUtils.js'; | ||
// Calculate the equivalent of __dirname in ES Module scope | ||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
// Determine the base directory for static file serving and view engine setup | ||
const baseDir = path.dirname(fileURLToPath(import.meta.url)); | ||
/** | ||
* Class representing a customizable gateway server using Express.js. | ||
* Represents a customizable gateway server. | ||
*/ | ||
class Gateway { | ||
/** | ||
* Creates a Gateway instance with provided configuration. | ||
* @param {Object} config Configuration for the Gateway. | ||
* @param {number} [config.port=3432] Port on which the server will listen. | ||
* @param {string} [config.domainsConfigPath='./config/domains.json'] Path to the domain configuration file. | ||
*/ | ||
constructor({ port = 3432, domainsConfigPath = './config/domains.json' } = {}) { | ||
this.port = port; | ||
// Adjust the path to the domains configuration file | ||
this.domainsConfigPath = path.resolve(__dirname, domainsConfigPath); | ||
this.app = express(); | ||
this.initialize().catch(err => console.error('Initialization error:', err)); | ||
} | ||
/** | ||
* Initializes the server setup including static file serving, view engine setup, and domain routing. | ||
*/ | ||
async initialize() { | ||
const baseDir = path.dirname(fileURLToPath(import.meta.url)); // Calculate the base directory for the gateway, assuming the gateway class is in 'src' | ||
this.app.use(express.urlencoded({ extended: true })); // Parse URL-encoded bodies (as sent by HTML forms) | ||
this.app.use(express.json()); // Parse JSON bodies (as sent by API clients) | ||
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')); | ||
// Use morgan for logging | ||
this.app.use(morgan('dev')); | ||
// Load the domain configuration | ||
await this.loadDomainConfig(); | ||
// Initialize and use the routes with the provided domainsConfigPath | ||
// This should come after loading your domain configuration and before starting the server | ||
const router = initializeRoutes(this.domainsConfigPath); | ||
this.app.use(router); | ||
} | ||
/** | ||
* Loads the domain configuration from a specified JSON file. | ||
*/ | ||
async loadDomainConfig() { | ||
try { | ||
// Pass the resolved path to the utility function | ||
const config = await loadDomainConfig(this.domainsConfigPath); | ||
console.log('Loaded Domain Configuration:', config); | ||
} catch (err) { | ||
console.error('Failed to load domain configuration:', err); | ||
throw new Error('Failed to initialize domain configuration'); | ||
} | ||
} | ||
/** | ||
* Initializes a new instance of the Gateway class. | ||
* @param {Object} config - The configuration object for the gateway. | ||
* @param {number} [config.port=3432] - The port number on which the gateway will listen. | ||
* @param {Object} [config.handlers={}] - An object mapping domains to their respective request handlers. | ||
*/ | ||
constructor({ port = 3432, handlers = {} } = {}) { | ||
this.port = port; | ||
this.handlers = handlers; | ||
this.app = express(); | ||
this.initialize().catch(err => console.error('Initialization error:', err)); | ||
} | ||
/** | ||
* Starts listening on the configured port. | ||
*/ | ||
listen() { | ||
this.app.listen(this.port, () => { | ||
console.log(`Gateway listening at http://localhost:${this.port}`); | ||
}); | ||
} | ||
/** | ||
* Initializes the express application with middleware, static file serving, and view engine setup. | ||
*/ | ||
async initialize() { | ||
this.app.use(express.urlencoded({ extended: true })); | ||
this.app.use(express.json()); | ||
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.app.use((req, res) => { | ||
// Check if handlers object is empty (no handlers defined at all) | ||
const noHandlersDefined = Object.keys(this.handlers).length === 0; | ||
const handler = this.handlers[req.hostname] || ((req, res) => defaultHandler(req, res, noHandlersDefined)); | ||
handler(req, res); | ||
}); | ||
} | ||
/** | ||
* Starts the gateway, making it listen on the configured port. | ||
*/ | ||
listen() { | ||
this.app.listen(this.port, () => { | ||
console.log(`Gateway listening at http://localhost:${this.port}`); | ||
}); | ||
} | ||
} | ||
export default Gateway; | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2
1642178
549