Skip to content

Instantly share code, notes, and snippets.

@qaisjp
Created May 11, 2017 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qaisjp/2f921c5f074dce60bea45f5e2e6f9e79 to your computer and use it in GitHub Desktop.
Save qaisjp/2f921c5f074dce60bea45f5e2e6f9e79 to your computer and use it in GitHub Desktop.
nginx
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