openresty get/set demo

lua_shared_dict dict 10m;

server {
  listen       80;
  #server_name  lederr.cn;
  access_log   logs/80.access.log  main;

  #ssl_certificate      /etc/letsencrypt/live/lederr.cn/fullchain.pem;
  #ssl_certificate_key  /etc/letsencrypt/live/lederr.cn/privkey.pem;


  #ssl_ciphers  HIGH:!aNULL:!MD5;
  #ssl_prefer_server_ciphers  on;


  location / {
  }

  location /set {
    default_type application/json;
    content_by_lua_block {
      local json = require "cjson.safe"
      if ngx.req.get_method() == "GET" then
        ngx.say(json.encode({msg='need post conf data'}));
        return
      end

      ngx.req.read_body()
      local body_str = ngx.req.get_body_data()
            if not body_str then
              ngx.say(json.encode({msg='err:server get_body_data error'}));
        return
            end

            local value, err = json.decode(body_str)
            if not value then
              local value = ngx.req.get_post_args()
              ngx.say(json.encode({msg='err:post data is not json'}));
        return
            else
        local dict = ngx.shared.dict
        dict:set("conf", body_str)
        ngx.say(json.encode({msg='conf set ok', conf=value}));
        return
      end


    }
  }

  location ~* /(get|psdk_param) {
    default_type application/json;
    add_header  DRAWIN 'SDK';
    content_by_lua_block {
      local json = require "cjson.safe"
      local dict = ngx.shared.dict
      local conf, err = json.decode(dict:get("conf"))
            if not conf then
              ngx.say(json.encode({msg=err}));
        return
      end
      ngx.say(json.encode(conf))
    }
  }

  location /favicon.ico {
    return 403;
  }

}

-- add version select 
lua_shared_dict dict 10m;

server {
  listen       80;
  #server_name  lederr.cn;
  access_log   logs/80.access.log  main;

  #ssl_certificate      /etc/letsencrypt/live/lederr.cn/fullchain.pem;
  #ssl_certificate_key  /etc/letsencrypt/live/lederr.cn/privkey.pem;


  #ssl_ciphers  HIGH:!aNULL:!MD5;
  #ssl_prefer_server_ciphers  on;


  location / {
  }

  location /set {
    default_type application/json;
    content_by_lua_block {
      local json = require "cjson.safe"
      if ngx.req.get_method() == "GET" then
        ngx.say(json.encode({msg='need post conf data'}));
        return
      end

      ngx.req.read_body()
      local body_str = ngx.req.get_body_data()
            if not body_str then
              ngx.say(json.encode({msg='err:server get_body_data error'}));
        return
            end

            local value, err = json.decode(body_str)
            if not value then
              local value = ngx.req.get_post_args()
              ngx.say(json.encode({msg='err:post data is not json'}));
        return
            else
        local dict = ngx.shared.dict

        local sdk_ver = ngx.req.get_headers()["version"];
        if sdk_ver == nil or sdk_ver == "" then
            sdk_ver = "default-ver";
        end

        dict:set(sdk_ver, body_str)
        ngx.say(json.encode({msg='conf set ok', sdk_ver=value}));
        return
      end


    }
  }

  location ~* /(get|psdk_param) {
    default_type application/json;
    add_header  DRAWIN 'SDK';
    content_by_lua_block {
      local json = require "cjson.safe"
      local dict = ngx.shared.dict

      local sdk_ver = ngx.var.arg_version;
      if sdk_ver == nil or sdk_ver == "" then
          sdk_ver = "default-ver";
      end

      local conf, err = json.decode(dict:get(sdk_ver))
            if not conf then
              ngx.say(json.encode({msg=err}));
        return
      end
      ngx.say(json.encode(conf))
    }
  }

  location /favicon.ico {
    return 403;
  }

}

你可能感兴趣的:(Linux-系统运维)