Created
May 11, 2017 17:17
-
-
Save qaisjp/2f921c5f074dce60bea45f5e2e6f9e79 to your computer and use it in GitHub Desktop.
nginx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lua_shared_dict calum_scores 12k; | |
server { | |
server_name calum.hgs.club; | |
listen 80; | |
default_type text; | |
add_header 'Access-Control-Allow-Origin' '*'; | |
location / { | |
return 444; | |
} | |
location /get { | |
content_by_lua ' | |
local dict = ngx.shared.calum_scores | |
local score, name = tostring(dict:get("score")), tostring(dict:get("name")) | |
ngx.say(score .. " " .. name) | |
'; | |
} | |
location /set { | |
content_by_lua ' | |
local args = ngx.req.get_uri_args() | |
local dict = ngx.shared.calum_scores | |
local success, err = false, "bad data provided" | |
local score = tonumber(args.score) | |
local name = tostring(args.name) | |
if args.score and score and args.name and (score >= 0) and (score <= 100) and (#name <= 20) and (#name > 0) then | |
local forcible | |
success, err, forcible = dict:set("score", score) | |
if forcible then | |
dict:flush_expired() | |
end | |
if success then | |
success, err, forcible = dict:set("name", name) | |
if forcible then | |
dict:flush_expired() | |
end | |
end | |
end | |
if not success then | |
dict:set("name", nil) | |
dict:set("score", nil) | |
ngx.say("error: " .. tostring(err)) | |
return | |
end | |
ngx.say("highscore set to " .. score) | |
ngx.say("\\nname set to " .. name) | |
'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment