Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
86 lines (74 sloc) 2.65 KB
function identity(str)
return str
end
local base1 = [[
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>isucon 2</title>
<link type="text/css" rel="stylesheet" href="/css/ui-lightness/jquery-ui-1.8.24.custom.css">
<link type="text/css" rel="stylesheet" href="/css/isucon2.css">
<script type="text/javascript" src="/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="/js/jquery-ui-1.8.24.custom.min.js"></script>
<script type="text/javascript" src="/js/isucon2.js"></script>
</head>
<body>
<header>
<a href="/">
<img src="/images/isucon_title.jpg">
</a>
</header>
<div id="sidebar">
</div>
<div id="content">
]]
local base2 = [[
</div>
</body>
</html>
]]
local memcached = require "resty.memcached"
local memc, err = memcached:new{ key_transform = { identity, identity }}
if not memc then
ngx.log(ngx.ERR,"failed to instantiate memc: ", err)
ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR
return
end
memc:set_timeout(1000) -- 0.1 sec
local ok, err = memc:connect("127.0.0.1", <MEMCACHED_PORT>)
if not ok then
ngx.log(ngx.ERR, "failed to connect: ", err)
ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR
return
end
ngx.req.read_body()
local args = ngx.req.get_post_args()
local variation_id = args["variation_id"]
local member_id = args["member_id"]
local max_id, flags, err = memc:get("max_id:"..variation_id)
local rid, flags, err = memc:incr("vari_id:"..variation_id, 1)
-- ngx.log(ngx.ERR,variation_id, ":",rid,":",max_id)
if tonumber(rid) > tonumber(max_id) then
-- ngx.log(ngx.ERR, "soldout:",variation_id, ":",rid,":",max_id)
ngx.say(base1,'<span class="result" data-result="failure">売り切れました。</span>',base2)
return
end
local rid = variation_id * 100000 + rid
local seat_id, flags, err = memc:get("rid:"..rid)
-- ngx.log(ngx.ERR, "sold:",variation_id, ":",rid,":",max_id,":",seat_id)
memc:set_keepalive(0, 100)
ngx.say(base1,"<h2>予約完了</h2>")
ngx.say('会員ID:<span class="member_id">',member_id,'</span>で<span class="result" data-result="success">&quot;<span class="seat">', seat_id, '</span>&quot;の席を購入しました。</span>',base2)
local mysql = require "resty.mysql"
local db, err = mysql:new()
local ok, err, errno, sqlstate = db:connect{
host = "<DATABASE>",
port = 3306,
database = "<dbname>",
user = "isucon2app",
password = "isunageruna",
max_packet_size = 1024 * 1024 }
local bytesend, err = db:send_query("INSERT INTO order_request (id,member_id) VALUES ('"..rid.."','"..member_id.."')");
-- ngx.log(ngx.ERR, "sold:",variation_id, ":",rid,":",max_id,":",err)
local ok, err = db:set_keepalive(0, 100)