A Rate-Limiting HTTP Proxy(3)Redis MySQL Logging

A Rate-Limiting HTTP Proxy(3)Redis MySQL Logging

Redis
Start the Redis Service on my Local
>redis-server conf/redis.conf

Connect to the Redis Server
>redis-cli

We use this package to access Redis lua-resty-redis
https://github.com/openresty/lua-resty-redis

Installation - I am using OpenResty bundle, so I can directly use that with
local redis = require “resty.redis”

Redis Commands
https://redis.io/commands

A helper module redis.lua
local redis = require "resty.redis"

local config = {
  host = "127.0.0.1",
  port = 6379,
  -- pass = "1234"  -- redis password
}

local _M = {}

function _M.new(self)
    local red = redis:new()
    red:set_timeout(1000) -- 1 second
    local res = red:connect(config['host'], config['port'])
    if not res then
        return nil
    end
    if config['pass'] ~= nil then
    res = red:auth(config['pass'])
      if not res then
          return nil
      end
    end
    red.close = close
    return red
end

function close(self)
  local sock = self.sock
    if not sock then
        return nil, "not initialized"
    end
    if self.subscribed then
        return nil, "subscribed state"
    end
    return sock:setkeepalive(10000, 50)
end

Load the OpenResty System Module
lua_package_path "/opt/luaweb/lua/?.lua;;";

Use Redis in hello.lua
local req = require "req"
local cjson = require "cjson"
local redis = require "redis"

local args = req.getArgs()

-- convert string to JSON object
local json_str = '{"name": "Carl.Luo", "age": 35}'
local json = cjson.decode(json_str)
local key = args['key']

if key == nil or key == "" then
  key = "foo"
end

local red = redis:new()
local value = red:get(key)
red:close()
ngx.say("key = " .. key .. "Value = " .. value .. "<br />")

MySQL
module lua-resty-mysql
https://github.com/openresty/lua-resty-mysql

Installation
local mysql = require "resty.mysql"

I have a local MySQL database
>mysql -h 127.0.0.1 -u root

res is a array, each item in the array is a table. That is the result from the MySQL driver.
The package we abstract some methods. mysql.lua
local mysql = require "resty.mysql"

local config = {
  host = "127.0.0.1",
  port = 3306,
  database = "mysql",
  user = "root",
  password = ""
}

local _M = {}

function _M.new(self)
  local db, err = mysql:new()
  if not db then
    return nil
  end
  db:set_timeout(1000) -- 1 second
  local ok, err, errno, sqlstate = db:connect(config)
 
  if not ok then
    return nil
  end
  db.close = close
  return db
end

function close(self)
  local sock = self.sock
  if not sock then
    return nil, "not initialized"
  end
  if self.subscribed then
    return nil, "subscribed state"
  end
  return sock:setkeepalive(10000, 50) -- 10 seconds, 50 connections
end 

return _M

Here is How we use that in welcome.lua
local mysql = require "mysql"
local cjson = require "cjson"
local req = require "req"

local args = req.getArgs()
local name = args['name']

if name == nil or name == "" then
  name = "root"
end

name = ngx.quote_sql_str(name) -- pre handle SQL
local db = mysql:new()
local sql = "select User, Host from user where User = " .. name

ngx.say(sql)
ngx.say("<br />")

local res, err, errno, sqlstate = db:query(sql)
db:close()
if not res then
    ngx.say(err)
    return {}
end

ngx.say(cjson.encode(res))
ngx.say("<br />")

Logging
The method ngx.log(logging level, content)
Logging level — ngx.STDERR/EMERG/ALERT/CRIT/ERR/WARN/NOTICE/INFO/DEBUG
ngx.log(ngx.ALERT, 'print to error.log')
ngx.log(ngx.STDERR, 'print to error.log')
ngx.log(ngx.EMERG, 'print to error.log')
ngx.log(ngx.ALERT, 'print to error.log')
ngx.log(ngx.CRIT, 'print to error.log')
ngx.log(ngx.ERR, 'print to error.log')
ngx.log(ngx.WARN, 'print to error.log')
ngx.log(ngx.NOTICE, 'print to error.log')
ngx.log(ngx.INFO, 'print to error.log')
ngx.log(ngx.DEBUG, 'print to error.log’)

Control the global LEVEL in nginx.conf
error_log  logs/error.log  notice;


References:
https://github.com/362228416/openresty-web-dev/tree/master/demo4
https://github.com/362228416/openresty-web-dev/tree/master/demo5
https://github.com/362228416/openresty-web-dev/tree/master/demo6



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326328775&siteId=291194637