Skip to content

Instantly share code, notes, and snippets.

@agaffney
Created September 11, 2015 21:55
Show Gist options
  • Save agaffney/a21a80e7297788fc9978 to your computer and use it in GitHub Desktop.
Save agaffney/a21a80e7297788fc9978 to your computer and use it in GitHub Desktop.
nginx+lua magic to allow "docker seach" to work with a V2.1 docker registry
location /v1/search {
content_by_lua '
local cjson = require "cjson"
local args = ngx.req.get_uri_args()
local query = args["q"]
-- Create structure for V1 search endpoint output
local output = { ["num_results"] = 0, ["query"] = query, ["results"] = {} }
-- Fetch data from V2 catalog endpoint
local res = ngx.location.capture("/v2/_catalog")
-- Parse JSON and iterate over it
local data = cjson.new().decode(res.body)
for k, repo in pairs(data["repositories"]) do
if not (string.match(repo, query) == nil) then
output["num_results"] = output["num_results"] + 1
table.insert(output["results"], { ["name"] = repo })
end
end
-- If there are no results, add a dummy entry. This is necessary because
-- cjson treats an empty table as a JSON object rather than an array,
-- which causes the Docker client to barf on the output
if output["num_results"] == 0 then
table.insert(output["results"], { ["name"] = "dummy", ["description"] = "dummy entry because Lua" })
output["num_results"] = 1
end
-- Encode the output structure as JSON and send to client
ngx.print(cjson.new().encode(output))
';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment