Browse Source

Simplify app.lua

Mikael Lindberg 11 years ago
parent
commit
3ded2f4200
1 changed files with 19 additions and 36 deletions
  1. 19 36
      openresty/app.lua

+ 19 - 36
openresty/app.lua

@@ -1,44 +1,27 @@
-local _M = {}
-
-local cjson = require "cjson"
 local mysql = require "resty.mysql"
 local math = require "math"
 
-local encode = cjson.encode
+local encode = require("cjson").encode
 local random = math.random
 local insert = table.insert
 
 
-function _M.handler(ngx)
-    ngx.header.content_type = 'application/json'
-    
-    if ngx.var.uri == '/json' then
-        local resp = {message = "Hello, World!"}
-        ngx.print( encode(resp) )
-    elseif ngx.var.uri == '/db' then
-
-        local mysqlconn = {
-            host = "DBHOSTNAME",
-            port = 3306,
-            database = "hello_world",
-            user = "benchmarkdbuser",
-            password = "benchmarkdbpass"
-        }
-
-        local db, err = mysql:new()
-        local ok, err = db:connect(mysqlconn)
-        local num_queries = tonumber(ngx.var.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)
-    elseif ngx.var.uri == '/plaintext' then
-        ngx.header.content_type = 'text/plain'
-        ngx.print('Hello, World!')
-    end
+local mysqlconn = {
+	host = "127.0.0.1",
+	port = 3306,
+	database = "hello_world",
+	user = "benchmarkdbuser",
+	password = "benchmarkdbpass"
+}
+local db = mysql:new()
+return function()
+	db:connect(mysqlconn)
+	local num_queries = tonumber(ngx.var.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) )
+	db:set_keepalive(0, 256)
 end
-
-return _M