mirror of
https://github.com/jorisvink/kore
synced 2025-03-09 12:39:01 -04:00
add new python-pgsql example
This commit is contained in:
parent
ace8c4e80c
commit
0bf36b763a
6
examples/python-pgsql/.gitignore
vendored
Normal file
6
examples/python-pgsql/.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
*.o
|
||||
.flavor
|
||||
.objs
|
||||
python-pgsql.so
|
||||
assets.h
|
||||
cert
|
35
examples/python-pgsql/conf/build.conf
Normal file
35
examples/python-pgsql/conf/build.conf
Normal file
@ -0,0 +1,35 @@
|
||||
# python-pgsql build config
|
||||
# You can switch flavors using: kore flavor [newflavor]
|
||||
|
||||
# Set to yes if you wish to produce a single binary instead
|
||||
# of a dynamic library. If you set this to yes you must also
|
||||
# set kore_source together with kore_flavor and update ldflags
|
||||
# to include the appropriate libraries you will be linking with.
|
||||
#single_binary=no
|
||||
#kore_source=/home/joris/src/kore
|
||||
#kore_flavor=
|
||||
|
||||
# The flags below are shared between flavors
|
||||
cflags=-Wall -Wmissing-declarations -Wshadow
|
||||
cflags=-Wstrict-prototypes -Wmissing-prototypes
|
||||
cflags=-Wpointer-arith -Wcast-qual -Wsign-compare
|
||||
|
||||
cxxflags=-Wall -Wmissing-declarations -Wshadow
|
||||
cxxflags=-Wpointer-arith -Wcast-qual -Wsign-compare
|
||||
|
||||
# Mime types for assets served via the builtin asset_serve_*
|
||||
#mime_add=txt:text/plain; charset=utf-8
|
||||
#mime_add=png:image/png
|
||||
#mime_add=html:text/html; charset=utf-8
|
||||
|
||||
dev {
|
||||
# These flags are added to the shared ones when
|
||||
# you build the "dev" flavor.
|
||||
cflags=-g
|
||||
cxxflags=-g
|
||||
}
|
||||
|
||||
#prod {
|
||||
# You can specify additional flags here which are only
|
||||
# included if you build with the "prod" flavor.
|
||||
#}
|
16
examples/python-pgsql/conf/python-pgsql.conf
Normal file
16
examples/python-pgsql/conf/python-pgsql.conf
Normal file
@ -0,0 +1,16 @@
|
||||
# python-pgsql configuration
|
||||
|
||||
bind 127.0.0.1 8888
|
||||
|
||||
python_import src/query.py
|
||||
|
||||
tls_dhparam dh2048.pem
|
||||
|
||||
domain * {
|
||||
certfile cert/server.crt
|
||||
certkey cert/server.key
|
||||
|
||||
static / query
|
||||
static /hello hello
|
||||
static /slow slow
|
||||
}
|
51
examples/python-pgsql/src/query.py
Normal file
51
examples/python-pgsql/src/query.py
Normal file
@ -0,0 +1,51 @@
|
||||
#
|
||||
# Copyright (c) 2017 Joris Vink <joris@coders.se>
|
||||
#
|
||||
# Permission to use, copy, modify, and distribute this software for any
|
||||
# purpose with or without fee is hereby granted, provided that the above
|
||||
# copyright notice and this permission notice appear in all copies.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
#
|
||||
|
||||
# Asynchronous postgresql queries with Python.
|
||||
|
||||
import json
|
||||
import kore
|
||||
|
||||
# Register the path to our database when the worker starts.
|
||||
def kore_worker_configure():
|
||||
kore.register_database("db", "host=/tmp dbname=kore")
|
||||
|
||||
# A handler that returns 200 OK with hello as body.
|
||||
def hello(req):
|
||||
req.response(200, b'hello\n')
|
||||
|
||||
#
|
||||
# The query handler that fires of the query and returns a coroutine.
|
||||
#
|
||||
# Kore will resume this handler when the query returns a result or
|
||||
# is succesfull.
|
||||
#
|
||||
# The req.pgsql() method can throw exceptions, most notably a
|
||||
# GeneratorExit in case the client connection went away before
|
||||
# the query was able to be completed.
|
||||
#
|
||||
# In this example we're not doing any exception handling.
|
||||
#
|
||||
async def query(req):
|
||||
result = await req.pgsql("db", "SELECT * FROM coders")
|
||||
req.response(200, json.dumps(result).encode("utf-8"))
|
||||
|
||||
#
|
||||
# A slow query that returns after 10 seconds.
|
||||
#
|
||||
async def slow(req):
|
||||
result = await req.pgsql("db", "SELECT * FROM pg_sleep(10)")
|
||||
req.response(200, json.dumps(result).encode("utf-8"))
|
Loading…
x
Reference in New Issue
Block a user