All modules are loaded from here. There is no requirement for nginx.conf additions as a catchall for pinky.dispatch() is handled via /pinky/*
local p = require 'pinky'
local json = require 'cjson'
This covers pulling in pinky core as well as json for output formatting.
Additionally you are required to create an entry function called main that takes the truncated uri as it’s sole argument.
local empty;
local foo;
local bar;
Here we provide declarations of each function as local.
local function pinky_main(uri)
-- This function is the entry point.
-- We are passed the rest of the uri.
local args = p.split(uri,"/")
if #args == 0 then
return json.encode("Hello controller called with no args! (e.g. http://localhost/pinky/hello)"
elseif #args == 1 then
return json.encode("Hello controller called with one arg, foo! (e.g. http://localhost/pinky/hello/foo)"
elseif #args == 2 then
return json.encode("Hello controller called with two arg, foo! (e.g. http://localhost/pinky/hello/foo)"
end
end
We strip out all previous portions of the url including the controller name (e.g. ”hello”)
Each plugin is can enforces its own rest argument format. In this example we have three levels.
Based on each we call a different function with the arguments provided.
function empty()
return "Hello controller called with no args! (e.g. http://localhost/pinky/hello)"
end
function foo()
return "Hello controller called with one arg, foo! (e.g. http://localhost/pinky/hello/foo)"
end
function bar()
return "Hello controller called with two args, foo and bar! (e.g. http://localhost/pinky/hello/foo/bar)"
end
Finally we export which functions are publicly available.
return { pinky_main = pinky_main }
For almost all cases only pinky_main should be exported. Unless there is a need to make the functions available inside other plugins.
You will need to include the following in your nginx.conf
server {
listen 44444;
location ~ /pinky/* {
content_by_lua '
local pinky = require("pinky")
ngx.say(pinky.dispatch(ngx.var.uri))
';
}
}
This function handles executing external commands.
The arguments are:
- command: A String containing the full path to the binary and its arguments.
- Fields: A table of the numeric positions to return.
- Key_field: The position to use as the hash key.
- Sep: Regular expression to split each line of output from command.
- Tokenize: Boolean to return output as a table, or a string.
- true = return table
- nil = return string
Handler for all pinky functions.
Any calls to /pinky/foo will result in foo.lua being loaded if it exists. Then foo.pinky_main() will be called with the uri relative to pinky
Function to verify a fully qualified path/file exists. Returns boolean.
Function to return the files in the directory passed as an argument.
Iterate over a table and
Split a string into a table with pattern.
Handler for all pinky functions.
Any calls to /pinky/foo will result in foo.lua being loaded if it exists. Then foo.pinky_main() will be called with the uri relative to pinky
This function handles executing external commands.
The arguments are:
- command: A String containing the full path to the binary and its arguments.
- Fields: A table of the numeric positions to return.
- Key_field: The position to use as the hash key.
- Sep: Regular expression to split each line of output from command.
- Tokenize: Boolean to return output as a table, or a string.
- true = return table
- nil = return string
Function to verify a fully qualified path/file exists. Returns boolean.
This function takes a list of files and returns the first one that exists. Given platform differences we may have several places we look for something like rvm. With this we return the first one found.
return the home directory of the current user pinky is running as.
resolve the hostname provided to us. We use this to avoid using user input directly in commands.
Return operating system name as seen by uname.
Return full username of the current pinky process.
Return a table of all of the files/directories listed within.
Used in exec_command to return only those columns requested.
Lua lacks a natural string#split command so this provides it.
This function will walk a directory tree returning all files it finds. This is used in items like /proc where we want the full ps tree.
Remove trailing white space.
Debug method used when resolving problems.