rework base example a bit.

This commit is contained in:
Joris Vink 2017-02-06 12:21:40 +01:00
parent 01e3ef6cd3
commit 21bf7f9583
4 changed files with 9 additions and 41 deletions

View File

@ -32,7 +32,6 @@ domain * {
static /c cpage
static /b minimal
static /json json_parse
static /state state_test
static /ws ws_connect
static /upload upload

View File

@ -97,7 +97,6 @@ def page(req):
kore.log(kore.LOG_INFO, "got id of %s" % id)
req.response_header("content-type", "text/plain")
req.response(200, "hello 1234".encode("utf-8"))
return kore.RESULT_OK
#
# Handler that parses the incoming body as JSON and dumps out some things.
@ -105,42 +104,16 @@ def page(req):
def json_parse(req):
if req.method != kore.METHOD_PUT:
req.response(400, b'')
return kore.RESULT_OK
else:
data = json.loads(req.body)
kore.log(kore.LOG_INFO, "loaded json %s" % data)
if data["hello"] == 123:
kore.log(kore.LOG_INFO, "hello is 123!")
data = json.loads(req.body)
kore.log(kore.LOG_INFO, "loaded json %s" % data)
if data["hello"] == 123:
kore.log(kore.LOG_INFO, "hello is 123!")
req.response(200, "ok".encode("utf-8"))
return kore.RESULT_OK
#
# Handler that stores some python state in req.state that it reuses
# once the handler is called again by the event loop (after having
# returned RESULT_RETRY to the event loop).
#
def state_test(req):
# If we don't have a state this is the first time we're called.
if req.state is None:
kore.log(kore.LOG_INFO, "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.
kore.log(kore.LOG_INFO, "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
req.response(200, "ok".encode("utf-8"))
#
# Small handler, returns 200 OK.
#
def minimal(req):
req.response(200, b'')
return kore.RESULT_OK

View File

@ -27,7 +27,7 @@ def upload(req):
if req.method is not kore.METHOD_POST:
req.response_header("allow", "post")
req.response(400, b'')
return kore.RESULT_OK
return
# Ask kore to parse incoming multipart data.
req.populate_multi()
@ -36,7 +36,7 @@ def upload(req):
file = req.file_lookup("file")
if not file:
req.response(400, b'')
return kore.RESULT_OK
return
kore.log(kore.LOG_INFO,
"%s (%s, filename=%s)" % (file, file.name, file.filename))
@ -45,7 +45,7 @@ def upload(req):
f = open(file.filename, "wb")
if not f:
req.response(500, b'')
return kore.RESULT_OK
return
# Read all data from incoming file and write it to the output file.
len = True
@ -56,5 +56,3 @@ def upload(req):
f.close()
req.response(200, b'')
return kore.RESULT_OK

View File

@ -62,5 +62,3 @@ def ws_connect(req):
req.websocket_handshake("onconnect", "onmessage", "ondisconnect")
except:
req.response(500, b'')
return kore.RESULT_OK