Web
To handle synchronous HTTP requests with the actor model in Ergo Framework, they must be converted into asynchronous messages. For this purpose, two meta-processes have been created: meta.WebServer
and meta.WebHandler
. The first is used to start an HTTP server, and the second transforms synchronous HTTP requests into messages that are sent to a worker process. A worker process can be created using act.WebWorker
, which processes the incoming messages asynchronously.
Creating and Spawning meta.WebHandler
You can create a meta.WebHandler
using the meta.CreateWebHandler
function. This function creates an object that implements the gen.MetaProcessBehavior
and http.Handler
interfaces.
It takes meta.WebHandlerOptions
as an argument:
Process
: The name of the process to which transformed asynchronousmeta.MessageWebRequest
messages are sent. If not specified, these messages are sent to the parent meta-process.RequestTimeout
: The time allowed for the process to handle the message, with a default of 5 seconds.
After successful creation, you need to start the meta-process using SpawnMeta
from the gen.Process
interface.
Once the meta-process is running, it can be used as an HTTP request handler
Creating and Spawning meta.WebServer
To create the meta.WebServer
meta-process, use the meta.CreateWebServer
function with the meta.WebServerOptions
argument. These options allow you to set:
Host
: The interface on which the port will be opened for handling HTTP requests.Port
: The port number.CertManager
: Enables TLS encryption for the HTTP server. You can use the node'sCertManager
to activate the node's certificate by using theCertManager()
method of thegen.Node
interface.Handler
: Specifies the HTTP request handler.
When the meta-process is created, the HTTP server starts. If the server fails to start, meta.CreateWebServer
returns an error. After successful creation, start the meta-process using SpawnMeta(...)
from the gen.Process
interface.
Example:
Example can be found in the repository at https://github.com/ergo-services/examples, specifically in the demo
project.
Last updated