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 c10813dc442cd4105d433a64f0053f8dbc2a68d2
parent 8e858983bfed32d33d7bb952bdee999cb700e273
Author: Joris Vink <joris@coders.se>
Date:   Wed,  4 Sep 2019 20:07:04 +0200

turn python-pgsql into a real kore python app

Diffstat:
examples/python-pgsql/__init__.py | 7+++++++
examples/python-pgsql/app.py | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
examples/python-pgsql/conf/build.conf | 35-----------------------------------
examples/python-pgsql/conf/python-pgsql.conf | 16----------------
examples/python-pgsql/kore.conf | 14++++++++++++++
examples/python-pgsql/src/query.py | 51---------------------------------------------------
6 files changed, 75 insertions(+), 102 deletions(-)

diff --git a/examples/python-pgsql/__init__.py b/examples/python-pgsql/__init__.py @@ -0,0 +1,7 @@ +from .app import koreapp + +def kore_parent_configure(args): + koreapp.configure(args) + +def kore_worker_configure(): + return diff --git a/examples/python-pgsql/app.py b/examples/python-pgsql/app.py @@ -0,0 +1,54 @@ +# +# Copyright (c) 2017-2018 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 + +class KoreApp: + def configure(self, args): + # Register the path to our database when Kore starts. + kore.dbsetup("db", "host=/tmp dbname=test") + + # A handler that returns 200 OK with hello as body. + def hello(self, 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 kore.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(self, req): + result = await kore.dbquery("db", "SELECT * FROM coders") + req.response(200, json.dumps(result).encode("utf-8")) + + # + # A slow query that returns after 10 seconds. + # + async def slow(self, req): + result = await kore.dbquery("db", "SELECT * FROM pg_sleep(10)") + req.response(200, json.dumps(result).encode("utf-8")) + +koreapp = KoreApp() diff --git a/examples/python-pgsql/conf/build.conf b/examples/python-pgsql/conf/build.conf @@ -1,35 +0,0 @@ -# python-pgsql build config -# You can switch flavors using: kodev 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 @@ -1,16 +0,0 @@ -# python-pgsql configuration - -bind 127.0.0.1 8888 - -python_import src/query.py - -tls_dhparam dh2048.pem - -domain * { - certfile cert/server.pem - certkey cert/key.pem - - static / query - static /hello hello - static /slow slow -} diff --git a/examples/python-pgsql/kore.conf b/examples/python-pgsql/kore.conf @@ -0,0 +1,14 @@ +# sql configuration + +bind 127.0.0.1 8888 + +tls_dhparam dh2048.pem + +domain * { + certfile cert/server.pem + certkey cert/key.pem + + static / koreapp.query + static /hello koreapp.hello + static /slow koreapp.slow +} diff --git a/examples/python-pgsql/src/query.py b/examples/python-pgsql/src/query.py @@ -1,51 +0,0 @@ -# -# Copyright (c) 2017-2018 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 Kore starts. -def kore_parent_configure(args): - kore.dbsetup("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 kore.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 kore.dbquery("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 kore.dbquery("db", "SELECT * FROM pg_sleep(10)") - req.response(200, json.dumps(result).encode("utf-8"))