Skip to content

Commit

Permalink
Merge pull request #5175 from whalecold/refactor
Browse files Browse the repository at this point in the history
Migrate the backends handle logic to function
  • Loading branch information
k8s-ci-robot authored Mar 11, 2020
2 parents d868a4e + f2e5d6f commit e152ee3
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions rootfs/etc/nginx/lua/configuration.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,31 @@ local function handle_certs()
end
end


local function handle_backends()
if ngx.var.request_method == "GET" then
ngx.status = ngx.HTTP_OK
ngx.print(_M.get_backends_data())
return
end

local backends = fetch_request_body()
if not backends then
ngx.log(ngx.ERR, "dynamic-configuration: unable to read valid request body")
ngx.status = ngx.HTTP_BAD_REQUEST
return
end

local success, err = configuration_data:set("backends", backends)
if not success then
ngx.log(ngx.ERR, "dynamic-configuration: error updating configuration: " .. tostring(err))
ngx.status = ngx.HTTP_BAD_REQUEST
return
end

ngx.status = ngx.HTTP_CREATED
end

function _M.call()
if ngx.var.request_method ~= "POST" and ngx.var.request_method ~= "GET" then
ngx.status = ngx.HTTP_BAD_REQUEST
Expand All @@ -177,33 +202,13 @@ function _M.call()
return
end

if ngx.var.request_uri ~= "/configuration/backends" then
ngx.status = ngx.HTTP_NOT_FOUND
ngx.print("Not found!")
return
end

if ngx.var.request_method == "GET" then
ngx.status = ngx.HTTP_OK
ngx.print(_M.get_backends_data())
if ngx.var.request_uri == "/configuration/backends" then
handle_backends()
return
end

local backends = fetch_request_body()
if not backends then
ngx.log(ngx.ERR, "dynamic-configuration: unable to read valid request body")
ngx.status = ngx.HTTP_BAD_REQUEST
return
end

local success, err = configuration_data:set("backends", backends)
if not success then
ngx.log(ngx.ERR, "dynamic-configuration: error updating configuration: " .. tostring(err))
ngx.status = ngx.HTTP_BAD_REQUEST
return
end

ngx.status = ngx.HTTP_CREATED
ngx.status = ngx.HTTP_NOT_FOUND
ngx.print("Not found!")
end

if _TEST then
Expand Down

0 comments on commit e152ee3

Please sign in to comment.