Skip to content

Instantly share code, notes, and snippets.

@bakins
Last active December 15, 2015 20:59
Show Gist options
  • Save bakins/5322735 to your computer and use it in GitHub Desktop.
Save bakins/5322735 to your computer and use it in GitHub Desktop.
local _M = {}
local cjson = require "cjson"
local mysql = require "resty.mysql"
local math = require "math"
local encode = cjson.encode
local random = math.random
local insert = table.insert
local mysqlconn = {
host = "DBHOSTNAME",
port = 3306,
database = "hello_world",
user = "benchmarkdbuser",
password = "benchmarkdbpass"
}
local function json_handler(ngx)
ngx.header.content_type = 'application/json'
local resp = {message = "Hello, World!"}
ngx.print(encode(resp))
end
local function db_handler(ngx)
ngx.header.content_type = 'application/json'
local db, err = mysql:new()
local ok, err = db:connect(mysqlconn)
local num_queries = tonumber(ngx.req.arg_queries) or 1
local worlds = {}
for i=1, num_queries do
local wid = random(1, 10000)
insert(worlds, db:query('SELECT * FROM World WHERE id = '..wid)[1])
end
ngx.print( encode(worlds) )
local ok, err = db:set_keepalive(0, 256)
end
local handlers = {
["/json"] = json_handler,
["/db"] = db_handler
}
function _M.handler(ngx)
local func = handlers[ngx.var.uri]
return func and func(ngx) or ngx.exit(404)
end
return _M
worker_processes 2;
pid /tmp/nginx.pid;
error_log /dev/null crit;
#error_log /tmp/test.log error;
events {
worker_connections 16384;
}
http {
access_log off;
lua_package_path "CWD/openresty/?.lua;;";
server {
listen 8080;
location / {
content_by_lua 'require("app").handler(ngx)';
}
}
}
@millken
Copy link

millken commented Apr 16, 2013

nice job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment