Search code examples
apacherestcentosrhelmod-lua

Determine variable from uri pattern (Apache)


Actually we are using an Apache webserver to host our REST-API. Scripts are written in Lua and mapped with mod-lua.

For instance an actual snippet from the httpd.conf:

[...]
LuaMapHandler /restapi/(v\d{3})/(\w+) /opt/http/htdocs/restscripts/$1/$2.lua
[...]

So the Request GET https://xxx/restapi/v001/articles is mapped to the script /opt/http/htdocs/restscripts/v001/articles.lua.

Okay, I hope you could actually follow me. Now we have to extend our API with modules, but the request uri has to be the same. Is there an opportunity to determine the module name and write it into a variable?


Example - script paths:

/opt/http/htdocs/restscripts/module1/v001/articles.lua
/opt/http/htdocs/restscripts/module2/v001/orders.lua
/opt/http/htdocs/restscripts/module2/v001/payments.lua
/opt/http/htdocs/restscripts/module3/v001/employees.lua

The lua-scripts are unique.

Example - requests:

GET https://xxx/restapi/v001/articles
GET https://xxx/restapi/v001/orders
GET https://xxx/restapi/v001/payments
GET https://xxx/restapi/v001/employees

The httpd.conf should be sth like this:

[...]
$MODULE=getModule
LuaMapHandler /restapi/(v\d{3})/(\w+) /opt/http/htdocs/restscripts/$MODULE/$1/$2.lua
[...]

I already read following question, but these variables aren't dependending on the uri.

Please tell me, if you need any further information. Thanks


Solution

  • Solved it by specifying the module in the uri and evaluating it within the lua script.