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 ff48aed926c07dad2da85e8ba51f3eebc9ee69ac
parent 21f9c29f3149422ac0d3c860748a06ab79c2a42f
Author: Joris Vink <joris@coders.se>
Date:   Wed, 25 Jan 2017 22:38:06 +0100

initial python example.

don't look too much into this yet.

Diffstat:
examples/python/.gitignore | 6++++++
examples/python/conf/build.conf | 35+++++++++++++++++++++++++++++++++++
examples/python/conf/python.conf | 39+++++++++++++++++++++++++++++++++++++++
examples/python/dh2048.pem | 9+++++++++
examples/python/src/index.py | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
examples/python/src/python.c | 38++++++++++++++++++++++++++++++++++++++
6 files changed, 219 insertions(+), 0 deletions(-)

diff --git a/examples/python/.gitignore b/examples/python/.gitignore @@ -0,0 +1,6 @@ +*.o +.flavor +.objs +python.so +assets.h +cert diff --git a/examples/python/conf/build.conf b/examples/python/conf/build.conf @@ -0,0 +1,35 @@ +# python 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/conf/python.conf b/examples/python/conf/python.conf @@ -0,0 +1,39 @@ +# python configuration + +load ./python.so onload +python_import src/index.py onload + +#bind 127.0.0.1 8888 c_on_connect +bind 127.0.0.1 8888 + +tls_dhparam dh2048.pem + +validator v_id function c_validator +validator v_p_id function python_validator +validator v_auth function python_auth + +authentication auth { + authentication_type request + authentication_validator v_auth +} + +domain * { + certfile cert/server.crt + certkey cert/server.key + + static / page + static /c cpage + static /b minimal + static /json json_parse + static /state state_test + + static /auth page auth + + params get / { + validate id v_p_id + } + + params get /c { + validate id v_id + } +} diff --git a/examples/python/dh2048.pem b/examples/python/dh2048.pem @@ -0,0 +1,8 @@ +-----BEGIN DH PARAMETERS----- +MIIBCAKCAQEAn4f4Qn5SudFjEYPWTbUaOTLUH85YWmmPFW1+b5bRa9ygr+1wfamv +VKVT7jO8c4msSNikUf6eEfoH0H4VTCaj+Habwu+Sj+I416r3mliMD4SjNsUJrBrY +Y0QV3ZUgZz4A8ARk/WwQcRl8+ZXJz34IaLwAcpyNhoV46iHVxW0ty8ND0U4DIku/ +PNayKimu4BXWXk4RfwNVP59t8DQKqjshZ4fDnbotskmSZ+e+FHrd+Kvrq/WButvV +Bzy9fYgnUlJ82g/bziCI83R2xAdtH014fR63MpElkqdNeChb94pPbEdFlNUvYIBN +xx2vTUQMqRbB4UdG2zuzzr5j98HDdblQ+wIBAg== +-----END DH PARAMETERS-----+ \ No newline at end of file diff --git a/examples/python/src/index.py b/examples/python/src/index.py @@ -0,0 +1,92 @@ +# Simplistic kore example + +import kore +import json + +def python_auth(req, data): +# print("python auth called %s" % data) + return kore.RESULT_OK + +def python_validator(req, data): + print("python validator called %s" % data) + return kore.RESULT_OK + +def python_handle(c): + print("python_handle %s" % c) + +def python_on_connect(c): + c.proto = kore.CONN_PROTO_UNKNOWN; + c.state = kore.CONN_STATE_ESTABLISHED; + c.handle = python_handle + print("onconnect %s" % c) + +def onload(action): + kore.log(kore.LOG_INFO, "FOOBAR python onload called with %d" % action) + return kore.RESULT_OK + +def kore_onload(): + print("kore_onload called") + +def kore_preload(): + print("kore_preload called") + +def page(req): +# print("%s path is %s - host is %s" % (req, req.path, req.host)) +# print("connection is %s" % req.connection) + xframe = req.request_header("xframe") +# if xframe != None: +# print("xframe header present %s" % xframe) + if req.method == kore.METHOD_POST: + try: + length, body = req.body_read(1024) + print("POST and got %d bytes! (%s)" % + (length, body.decode("utf-8"))) + except RuntimeError as r: + print("oops runtime error %s" % r) + req.response(500, b'') + except: + print("oops other error") + req.response(500, b'') + else: + req.response_header("content-type", "text/plain") + req.response(200, body) + else: + req.populate_get() + req.response_header("content-type", "text/plain") + req.response(200, "hello 1234".encode("utf-8")) + return kore.RESULT_OK + +def json_parse(req): + if req.method != kore.METHOD_PUT: + req.response(400, b'') + return kore.RESULT_OK + + data = json.loads(req.body) + print("loaded json %s" % data) + if data["hello"] == 123: + print("hello is 123!") + + req.response(200, "ok".encode("utf-8")) + return kore.RESULT_OK + +def state_test(req): + # If we don't have a state this is the first time we're called. + if req.state is None: + print("state_test: first time") + req.state = "hello world" + + # Tell Kore to call us again next event loop. + return kore.RESULT_RETRY + + # We have been called before. + print("state_test: second time, with %s" % req.state) + req.response(200, req.state.encode("utf-8")) + + # We *MUST* reset state back to None before returning RESULT_OK + req.state = None; + + return kore.RESULT_OK + +def minimal(req): + req.response(200, b'') + return kore.RESULT_OK diff --git a/examples/python/src/python.c b/examples/python/src/python.c @@ -0,0 +1,38 @@ +#include <kore/kore.h> +#include <kore/http.h> + +int onload(int); +int cpage(struct http_request *); +void c_on_connect(struct connection *); +int c_validator(struct http_request *, void *); + +int +c_validator(struct http_request *req, void *data) +{ + printf("c_validator called!\n"); + return (KORE_RESULT_OK); +} + +void +c_on_connect(struct connection *c) +{ + printf("c_on_connect!\n"); +} + +int +onload(int action) +{ + printf("C onload called!\n"); + return (KORE_RESULT_OK); +} + +int +cpage(struct http_request *req) +{ + http_populate_get(req); + + //printf("cpage called\n"); + http_response(req, 200, NULL, 0); + + return (KORE_RESULT_OK); +}