From 67ab8a171026ca9bab4230c161b64b3078647314 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Tue, 3 Dec 2024 17:04:05 -0500 Subject: [PATCH] doc: first run of module documenter --- src/backend/src/modules/web/README.md | 59 +++++++++++++++++++ .../src/modules/web/SocketioService.js | 10 ++++ src/backend/src/modules/web/WebModule.js | 10 ++++ 3 files changed, 79 insertions(+) create mode 100644 src/backend/src/modules/web/README.md diff --git a/src/backend/src/modules/web/README.md b/src/backend/src/modules/web/README.md new file mode 100644 index 000000000..22bdb2181 --- /dev/null +++ b/src/backend/src/modules/web/README.md @@ -0,0 +1,59 @@ +# WebModule + +This module initializes a pre-configured web server and socket.io server. +The main service, WebServerService, emits 'install.routes' and provides +the server instance to the callback. + +## Services + +### SocketioService + +SocketioService provides a service for sending messages to clients. +socket.io is used behind the scenes. This service provides a simpler +interface for sending messages to rooms or socket ids. + +#### Listeners + +##### `install.socketio` + +Initializes socket.io + +###### Parameters + +- `server`: The server to attach socket.io to. + +### WebModule + +undefined + +#### Listeners + +### WebServerService + +This class, WebServerService, is responsible for starting and managing the Puter web server. +It initializes the Express app, sets up middlewares, routes, and handles authentication and web sockets. +It also validates the host header and IP addresses to prevent security vulnerabilities. + +#### Listeners + +##### `boot.consolidation` + +This method initializes the backend web server for Puter. It sets up the Express app, configures middleware, and starts the HTTP server. + +##### `boot.activation` + +Starts the web server and listens for incoming connections. +This method sets up the Express app, sets up middleware, and starts the server on the specified port. +It also sets up the Socket.io server for real-time communication. + +##### `start.webserver` + +This method starts the web server by listening on the specified port. It tries multiple ports if the first one is in use. +If the `config.http_port` is set to 'auto', it will try to find an available port in a range of 4100 to 4299. +Once the server is up and running, it emits the 'start.webserver' and 'ready.webserver' events. +If the `config.env` is set to 'dev' and `config.no_browser_launch` is false, it will open the Puter URL in the default browser. + +##### `start.webserver` + + + diff --git a/src/backend/src/modules/web/SocketioService.js b/src/backend/src/modules/web/SocketioService.js index 1f98a10c7..74efd8365 100644 --- a/src/backend/src/modules/web/SocketioService.js +++ b/src/backend/src/modules/web/SocketioService.js @@ -1,10 +1,20 @@ const BaseService = require('../../services/BaseService'); +/** + * SocketioService provides a service for sending messages to clients. + * socket.io is used behind the scenes. This service provides a simpler + * interface for sending messages to rooms or socket ids. + */ class SocketioService extends BaseService { static MODULES = { socketio: require('socket.io'), }; + /** + * Initializes socket.io + * + * @evtparam server The server to attach socket.io to. + */ ['__on_install.socketio'] (_, { server }) { const require = this.require; diff --git a/src/backend/src/modules/web/WebModule.js b/src/backend/src/modules/web/WebModule.js index e7d3b1663..046307f19 100644 --- a/src/backend/src/modules/web/WebModule.js +++ b/src/backend/src/modules/web/WebModule.js @@ -1,7 +1,17 @@ const { AdvancedBase } = require("@heyputer/putility"); +/** + * This module initializes a pre-configured web server and socket.io server. + * The main service, WebServerService, emits 'install.routes' and provides + * the server instance to the callback. + */ class WebModule extends AdvancedBase { async install (context) { + // === LIBS === // + const useapi = context.get('useapi'); + useapi.def('web', require('./lib/__lib__.js'), { assign: true }); + + // === SERVICES === // const services = context.get('services'); const SocketioService = require("./SocketioService");