kore

Kore is a web application platform for writing scalable, concurrent web based processes in C or Python.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 0bf36b763a617ec414ae741727419d17510548b9
parent ace8c4e80c3ffce6ce2db9622e5cb6781632ac30
Author: Joris Vink <joris@coders.se>
Date:   Mon,  6 Feb 2017 11:51:49 +0100

add new python-pgsql example

Diffstat:
examples/python-pgsql/.gitignore | 6++++++
examples/python-pgsql/conf/build.conf | 35+++++++++++++++++++++++++++++++++++
examples/python-pgsql/conf/python-pgsql.conf | 16++++++++++++++++
examples/python-pgsql/src/query.py | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 108 insertions(+), 0 deletions(-)

diff --git a/examples/python-pgsql/.gitignore b/examples/python-pgsql/.gitignore @@ -0,0 +1,6 @@ +*.o +.flavor +.objs +python-pgsql.so +assets.h +cert diff --git a/examples/python-pgsql/conf/build.conf b/examples/python-pgsql/conf/build.conf @@ -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. +#} diff --git a/examples/python-pgsql/conf/python-pgsql.conf b/examples/python-pgsql/conf/python-pgsql.conf @@ -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 +} diff --git a/examples/python-pgsql/src/query.py b/examples/python-pgsql/src/query.py @@ -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"))