-
Notifications
You must be signed in to change notification settings - Fork 7
/
ApacheHandlerUsingConfigFromFile.lua
120 lines (105 loc) · 5.33 KB
/
ApacheHandlerUsingConfigFromFile.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
----------------------------------------------------------------------------------------------------
-- ApacheHandlerUsingConfigFromFile.lua
----------------------------------------------------------------------------------------------------
-- HANDLER: ApacheHandlerUsingConfigFromFile
--
-- DESCRIPTION:
-- This Apache httpd Lua handler verifies that HTTP requests are allowed to be passed to the
-- backend. If a request does not contain proof that a queue traversal has already taken place
-- a redirect to the queue is issued.
-- This handler in an Apache httpd configuration file. The following environment variables
-- must be set in order for the handler to be able to handle requests:
--... * QUEUEIT_CUSTOMER_ID: The Queue-it customer id
--... * QUEUEIT_SECRET_KEY: The Queue-it secret key to access the queue API
--... * QUEUEIT_INT_CONF_FILE: The local JSON file containing the integration configuration
-- * QUEUEIT_ERROR_CODE: (optional) The response code to use instead of declining to act
-- if request handling fails
-- Note that the integration configuration is read on every request. The JSON file containing
-- The integration configuration should, for performance reasons, be available locally.
--
-- USAGE:
-- Add the following configuration to httpd.conf (or apache2.conf):
-- LoadModule lua_module modules/mod_lua.so
-- [...]
-- SetEnv QUEUEIT_CUSTOMER_ID "{CUSTOMER_ID}"
-- SetEnv QUEUEIT_SECRET_KEY "{SECRET_KEY}"
-- SetEnv QUEUEIT_INT_CONF_FILE "{APP_FOLDER}/integration_config.json"
-- SetEnv QUEUEIT_ERROR_CODE "400"
-- LuaMapHandler "{URI_PATTERN}" "{APP_FOLDER}/Handlers/ApacheHandlerUsingConfigFromFile.lua"
-- LuaPackagePath "{APP_FOLDER}/SDK/?.lua"
-- LuaPackagePath "{APP_FOLDER}/Helpers/?/?.lua"
-- LuaPackagePath "{APP_FOLDER}/Handlers/?.lua"
--
-- AUTHOR: Simon Studer ([email protected]), Frederik Williams ([email protected])
--
-- LICENSE: Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
--
----------------------------------------------------------------------------------------------------
local DEBUG_TAG = "ApacheHandlerUsingConfigFromFile.lua"
local kuHandler = require("KnownUserApacheHandler")
local file = require("file")
local function initRequiredHelpers(r)
local iHelpers = require("KnownUserImplementationHelpers")
iHelpers.request.getAbsoluteUri = function()
local fullUrl = string.format("%s://%s%s",
r.is_https and "https" or "http",
r.hostname,
r.unparsed_uri)
r:debug(string.format("[%s] Rebuilt request URL as: %s", DEBUG_TAG, fullUrl))
return fullUrl
end
end
function handle(r)
-- default error behaviour
local errorResult = apache2.DECLINED
-- catch errors if any occur
local success, result = pcall(function()
-- get configuration from environment variables
local customerId = r.subprocess_env["QUEUEIT_CUSTOMER_ID"]
local secretKey = r.subprocess_env["QUEUEIT_SECRET_KEY"]
local intConfFile = r.subprocess_env["QUEUEIT_INT_CONF_FILE"]
local errorCode = r.subprocess_env["QUEUEIT_ERROR_CODE"]
if customerId ~= nil then
r:debug(string.format("[%s] Environment variable QUEUEIT_CUSTOMER_ID: %s", DEBUG_TAG, customerId))
end
if secretKey ~= nil then
r:debug(string.format("[%s] Environment variable QUEUEIT_SECRET_KEY: %s", DEBUG_TAG, secretKey))
end
if intConfFile ~= nil then
r:debug(string.format("[%s] Environment variable QUEUEIT_INT_CONF_FILE: %s", DEBUG_TAG, intConfFile))
end
if errorCode ~= nil then
r:debug(string.format("[%s] Environment variable QUEUEIT_ERROR_CODE: %s", DEBUG_TAG, errorCode))
end
assert(customerId ~= nil, "customerId invalid")
assert(secretKey ~= nil, "secretKey invalid")
assert(intConfFile ~= nil, "config invalid")
-- check if valid value
if (errorCode ~= nil) then
errorCode = tonumber(errorCode)
if (errorCode == nil) then
r:warn(string.format(
"[%s] Value of QUEUEIT_ERROR_CODE is not a valid HTTP status code: %s",
DEBUG_TAG, r.subprocess_env["QUEUEIT_ERROR_CODE"]))
elseif (errorCode >= 100) and (errorCode < 600) then
errorResult = errorCode
end
end
r:debug(string.format("[%s] Value of variable errorCode: %s", DEBUG_TAG, errorCode))
-- initialize helper functions
initRequiredHelpers(r)
-- read integration configuration from file
local intConfJson = file.readAll(intConfFile)
r:debug(string.format("[%s] Content of file %s: %s", DEBUG_TAG, intConfFile, intConfJson))
-- return the result from handling the request by the SDK
return kuHandler.handleByIntegrationConfig(customerId, secretKey, intConfJson, r)
end)
-- check if error occurred during request handling
if (success) then
r:debug(string.format("[%s] Request handling successful: result => %s", DEBUG_TAG, result))
return result
else
r:err(string.format("[%s] Request handling not successful (denying access): error => %s", DEBUG_TAG, result))
return errorResult
end
end