Skip to content

Instantly share code, notes, and snippets.

@moonbingbing
Created November 2, 2012 03:04
Show Gist options
  • Save moonbingbing/3998467 to your computer and use it in GitHub Desktop.
Save moonbingbing/3998467 to your computer and use it in GitHub Desktop.
query postgres in openresty
worker_processes 1;
events {
worker_connections 64;
}
http {
send_timeout 1s;
server_tokens off;
client_body_buffer_size 128k;
upstream database {
postgres_server localhost:5432 dbname=test
user=adminpassword=test123;
postgres_keepalive max=80 mode=single overflow=ignore;
}
lua_package_path "$prefix/?.luac;$prefix/?.lua;;";
include mime.types;
log_format access '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent $request_time $request_body';
include ngx_php_win.conf;
include ngx_lua_win.conf;
}
server {
listen 80;
access_log off;#/var/log/nginx/access.log access;
gzip on;
gzip_types text/plain application/x-javascript text/css application/xml;
location /postgres {
internal;
default_type text/html;
set_by_lua $query_sql 'return ngx.unescape_uri(ngx.var.arg_sql)';
postgres_pass database;
rds_json on;
postgres_query $query_sql;
}
location /hello {
access_log off;
default_type text/html;
content_by_lua '
ngx.say("<p>hello, world</p>")
';
}
}
function query_from_db(sql)
local res = ngx.location.capture('/postgres',
{ args = {sql = sql } }
)
local status = res.status
local body = json.decode(res.body)
if status == 200 then
status = true
else
status = false
end
return status, body
end
function client_heartbeat(mid)
common.query_from_db("BEGIN")
local sql = [[UPDATE clients SET update_time = now() WHERE midn = ']] .. mid .. [[']]
common.query_from_db(sql)
common.query_from_db("COMMIT")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment