Script Configuration
Egern allows users to flexibly control network request/response handling and scheduled tasks by writing JavaScript scripts. Through these scripts, users can customize the handling of network requests and responses as needed or set up scheduled tasks to execute certain operations.
Configuration Details
The scriptings
configuration type includes three types of scripts: HTTP request scripts (http_request
), HTTP response scripts (http_response
), and scheduled task scripts (schedule
). Each type of script shares some common configuration fields and also has some specific configuration fields.
The following table lists all the configuration fields:
Field Name | Type | Required | Description | Applicable Script Types |
---|---|---|---|---|
name | string | Yes | The name of the script. | All |
match | string (RegEx) | No | The URL regex pattern where the script should be applied. | http_request , http_response |
script_url | string | Yes | The URL of the script, can be a local path or a remote link. | All |
update_interval | integer | No | When the script is a remote link, this sets the interval (in seconds) for updating the script. Default is 86400 (one day). | All |
timeout | integer | No | The timeout for script execution in seconds. Default is 30. | All |
max_size | integer | No | Sets the maximum message body size in bytes. If the message body exceeds this value, the script will not be invoked. | http_request , http_response |
debug | boolean | No | Whether to enable debugging. If enabled, Egern will print more debug information. | All |
body_required | boolean | No | Specifies whether the message body is required. If set to true , the script will only be invoked if the message body exists. | http_request , http_response |
cron | string | No | Sets the execution time of the scheduled task using a Cron expression. | schedule |
Configuration Example
Below is an example of a scriptings
type configuration:
scriptings:
- http_request:
name: "Example Request Script"
match: "example\\.com"
script_url: "scripts/request.js"
update_interval: 86400
timeout: 30
max_size: 131072
debug: false
body_required: true
- http_response:
name: "Example Response Script"
match: "example\\.com"
script_url: "scripts/response.js"
update_interval: 86400
timeout: 30
max_size: 131072
debug: false
body_required: true
- schedule:
name: "Example Scheduled Task"
cron: "0 0 * * *"
script_url: "scripts/task.js"
update_interval: 86400
timeout: 30
debug: false
In this configuration, we have three scripts: one to handle all HTTP requests matching example.com
, one to handle all HTTP responses matching example.com
, and one scheduled task that runs daily at midnight. Each script has its own URL, and Egern will update these scripts daily from their URLs.