Created
September 11, 2015 21:55
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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