Global Lua Modules

Global Lua modules are a powerful mechanism for implementing complex logical functionalities. Lua modules in local configuration use .lua as the file extension instead of .yaml, and must be stored in a dedicated folder named global_lua_modules within the configuration directory.

Directory Structure

The directory structure for Lua modules is as follows:

|-- global_lua_modules/
  |-- hello.lua
  |-- world.lua
  |-- ...

Synchronization Mechanism

If the global_lua_modules directory does not exist, the command-line tool will not check for changes in global Lua modules.

When OpenResty Edge has “Synchronize automatically to other partitions” enabled, the command-line tool will not delete existing global Lua modules in OpenResty Edge even if they are inconsistent with those in the local configuration.

However, when “Synchronize automatically to other partitions” is disabled, the command-line tool will delete Lua modules that exist in OpenResty Edge but not in the local configuration directory.

Usage Example

Update configuration to OpenResty Edge:

edge-config https://192.168.1.1 -s -t 2051e780-1897-4ea0-92b4-2c2f0169aa94 -l global_lua_modules -i /root/oredge-configs

Export configuration from OpenResty Edge:

edge-config -u https://192.168.1.1 -s -t 2051e780-1897-4ea0-92b4-2c2f0169aa94 -E -l global_lua_modules

Both examples use -l global_lua_modules to specify importing/exporting only the Global Lua Modules related configurations.

Configuration Example

Let’s illustrate how to create and use global Lua modules with a simple example:

  1. First, create a Lua module named hello.lua:
local _M = {}

function _M.go()
    ngx.say("hello")
end

return _M
  1. Then, call this hello Lua module in a page rule:
- enable_rule: true
  actions:
    "user-code":
      el: |-
        {
        true =>
            foreign-call(module: "hello", func: "go"),
            done;
        }
  comment: ''

In this example, we defined a simple Lua module hello.lua containing a function named go that outputs “hello”. Subsequently, we called this module and function in the page rule configuration using the expression language.

Using this approach, you can easily encapsulate complex logic in Lua modules and flexibly call them in your configuration when needed.