turn python-pgsql into a real kore python app

This commit is contained in:
Joris Vink 2019-09-04 20:07:04 +02:00
parent 8e858983bf
commit c10813dc44
6 changed files with 75 additions and 102 deletions

View File

@ -0,0 +1,7 @@
from .app import koreapp
def kore_parent_configure(args):
koreapp.configure(args)
def kore_worker_configure():
return

View File

@ -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()

View File

@ -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.
#}

View File

@ -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
}

View File

@ -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
}

View File

@ -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"))