Skip to content

Scripting

Louis requested to merge Scripting into master

Created by: Commander-lol

This PR adds in scripting support, allowing you to handle certain requests with dyon rhai lua scripts, as defined in the .swerve/config.yml file.

Script files should live inside the .swerve folder of a project so as to avoid cluttering the project that swerve is hosting. Currently there are no plans to support loading scripts from outside of the .swerve folder, but cross-project scripts could be handled quite easily by symlinking from a common script folder.

Scripts are specified as a handler for a route entry in the config.yml file, in the format

routes:
  - route: /path/@param
    script: my_script.lua

Any routing params will match that section of the URL, and will be available inside the script as a property on the params table (e.g. the previous example would expose params.param inside the lua script).

An empty response object can be created with the empty_response() global, or a fully fleshed out response can be created with the response(status_code, content_type, body_string) global.

A response object exposes the following methods:

  • response:set_status(status_code)
  • response:set_header(header, value)
  • response:set_body(body_string)
  • response:unset_body()

A script must return a response object


A response handler type in the config.yml (check the example's /users route) is currently supported, but will not register a handler as there is currently no implementation for it


Closes #1 (closed)

Merge request reports

Loading