Skip to content

Commit

Permalink
Merge pull request #3 from Kong/feat/update-to-new-db-and-pdk
Browse files Browse the repository at this point in the history
feat(azure-functions) update to new db & pdk
  • Loading branch information
kikito authored Oct 17, 2018
2 parents 0ad5eaa + 36084ce commit 4fdd5d5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 70 deletions.
67 changes: 11 additions & 56 deletions kong/plugins/azure-functions/handler.lua
Original file line number Diff line number Diff line change
@@ -1,54 +1,21 @@
local BasePlugin = require "kong.plugins.base_plugin"
local singletons = require "kong.singletons"
local responses = require "kong.tools.responses"
local constants = require "kong.constants"
local meta = require "kong.meta"
local http = require "resty.http"


local pairs = pairs
local type = type
local ngx = ngx
local get_body_data = ngx.req.get_body_data
local get_uri_args = ngx.req.get_uri_args
local get_headers = ngx.req.get_headers
local read_body = ngx.req.read_body
local get_method = ngx.req.get_method
local ngx_log = ngx.log
local var = ngx.var


local server_header = meta._SERVER_TOKENS
local conf_cache = setmetatable({}, { __mode = "k" })


local function send(status, content, headers)
ngx.status = status

if type(headers) == "table" then
for k, v in pairs(headers) do
ngx.header[k] = v
end
if kong.configuration.enabled_headers[constants.HEADERS.VIA] then
headers = kong.table.merge(headers) -- create a copy of headers
headers[constants.HEADERS.VIA] = server_header
end

if not ngx.header["Content-Length"] then
ngx.header["Content-Length"] = #content
end

if singletons.configuration.enabled_headers[constants.HEADERS.VIA] then
ngx.header[constants.HEADERS.VIA] = server_header
end

ngx.print(content)

return ngx.exit(status)
end


local function flush(ctx)
ctx = ctx or ngx.ctx
local response = ctx.delayed_response
return send(response.status_code, response.content, response.headers)
return kong.response.exit(status, content, headers)
end


Expand Down Expand Up @@ -97,15 +64,15 @@ function azure:access(config)

local ok, err = client:connect(config.host, config.port)
if not ok then
ngx_log(ngx.ERR, "[azure-functions] could not connect to Azure service: ", err)
return responses.send_HTTP_INTERNAL_SERVER_ERROR(err)
kong.log.err("could not connect to Azure service: ", err)
return kong.response.exit(500, { message = "An unexpected error ocurred" })
end

if config.https then
local ok, err = client:ssl_handshake(false, config.host, config.https_verify)
if not ok then
ngx_log(ngx.ERR, "[azure-functions] could not perform SSL handshake : ", err)
return responses.send_HTTP_INTERNAL_SERVER_ERROR(err)
kong.log.err("could not perform SSL handshake : ", err)
return kong.response.exit(500, { message = "An unexpected error ocurred" })
end
end

Expand Down Expand Up @@ -144,7 +111,8 @@ function azure:access(config)
}

if not res then
return responses.send_HTTP_INTERNAL_SERVER_ERROR(err)
kong.log.err(err)
return kong.response.exit(500, { message = "An unexpected error occurred" })
end

local response_headers = res.headers
Expand All @@ -153,20 +121,7 @@ function azure:access(config)

ok, err = client:set_keepalive(config.keepalive)
if not ok then
ngx_log(ngx.ERR, "[azure-functions] could not keepalive connection: ", err)
end

local ctx = ngx.ctx
if ctx.delay_response and not ctx.delayed_response then
ctx.delayed_response = {
status_code = response_status,
content = response_content,
headers = response_headers,
}

ctx.delayed_response_callback = flush

return
kong.log.err("could not keepalive connection: ", err)
end

return send(response_status, response_content, response_headers)
Expand Down
34 changes: 20 additions & 14 deletions kong/plugins/azure-functions/schema.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
return {
name = "azure-functions",
fields = {
-- connection basics
timeout = { type = "number", default = 600000},
keepalive = { type = "number", default = 60000 },
https = { type = "boolean", default = true },
https_verify = { type = "boolean", default = false },
-- authorization
apikey = { type = "string" },
clientid = { type = "string" },
-- target/location
appname = { type = "string", required = true },
hostdomain = { type = "string", required = true, default = "azurewebsites.net" },
routeprefix = { type = "string", default = "api" },
functionname = { type = "string", required = true },
}
{ config = {
type = "record",
fields = {
-- connection basics
{ timeout = { type = "number", default = 600000}, },
{ keepalive = { type = "number", default = 60000 }, },
{ https = { type = "boolean", default = true }, },
{ https_verify = { type = "boolean", default = false }, },
-- authorization
{ apikey = { type = "string" }, },
{ clientid = { type = "string" }, },
-- target/location
{ appname = { type = "string", required = true }, },
{ hostdomain = { type = "string", required = true, default = "azurewebsites.net" }, },
{ routeprefix = { type = "string", default = "api" }, },
{ functionname = { type = "string", required = true }, },
},
}, },
},
}

0 comments on commit 4fdd5d5

Please sign in to comment.