Skip to content

Instantly share code, notes, and snippets.

@axiaoxin
Last active December 13, 2017 10:36
Show Gist options
  • Save axiaoxin/19cba89779f04525e6b44482f2f8ee80 to your computer and use it in GitHub Desktop.
Save axiaoxin/19cba89779f04525e6b44482f2f8ee80 to your computer and use it in GitHub Desktop.
wrap some json fields for influxdb api response by nginx_lua
upstream influxdb{
server 127.0.0.1:8086;
}
server {
listen 18086;
proxy_pass_request_headers off; # handle gziped capture <https://github.com/openresty/lua-nginx-module/issues/12>
location / {
proxy_pass http://influxdb/query;
}
location /query {
add_header Content-Type 'application/json; charset=utf-8'; # make sure content-type is json
content_by_lua_block {
local method_map = {
GET = ngx.HTTP_GET,
POST = ngx.HTTP_POST,
}
ngx.req.read_body()
local method = method_map[ngx.req.get_method()]
local data = ngx.req.get_body_data()
local args = ngx.req.get_uri_args()
local res = ngx.location.capture("/", {method=method, data=data, args=args})
local cjson = require "cjson"
local new_body = {}
if res.status ~= ngx.HTTP_OK then
new_body["code"] = res.status
else
new_body["code"] = 0
end
new_body["msg"] = ''
new_body["data"] = cjson.decode(res.body)
ngx.say(cjson.encode(new_body))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment