kore-doc

The kore documentation found under https://docs.kore.io/
Commits | Files | Refs | README | git clone https://git.kore.io/kore-doc.git

commit 4b0bbc779034717b61e50cb05d3d1e30f4839468
parent 72d37cf77640035e31f6591cee5c0f80f1a737fd
Author: Joris Vink <joris@coders.se>
Date:   Mon, 31 Aug 2020 14:58:59 +0200

add full blown routing example

Diffstat:
api/python.md | 76++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
1 file changed, 52 insertions(+), 24 deletions(-)

diff --git a/api/python.md b/api/python.md @@ -310,37 +310,65 @@ Nothing ### Example ```python -async def validate_user(req, data): - return True +import kore +import json -async def validate_auth(req, data): - return True +class RouteExample: + def configure(self, args): + kore.config.workers = 1 + kore.config.deployment = "dev" -def route_gethash(req, hash): - req.response(200, hash.encode()) + kore.server("default", ip="127.0.0.1", port="8888", tls=False) + dom = kore.domain("*", attach="default") + + # Simple GET route. + dom.route("/", self.index, methods=["get"]) + + # Example post route with some arguments (which are validated + # automatically and removed if verification fails). + dom.route("/update", self.update, methods=["post"], + post={ + "number" : "^[0-9]{4}$", + "user" : "^[a-z]{4,12}$" + } + ) + + # Authenticate via header or cookie. + dom.route("/secret", self.secret, methods=["get"], + auth={ + "type" : "header", # header or cookie + "value" : "x-header", # header or cookie name + "redirect" : "/", # redirect location upon failure, + # if not set, 403 is returned. + "verify" : self.verify # The callback to verify + } + ) + + def verify(self, req, data): + kore.log(kore.LOG_INFO, "verify called with %s" % data) + return True -dom = kore.domain("kore.io", attach="server", acme=True) + def index(self, req): + req.response(200, b'this is the index') -dom.route("/", route_index, methods=["get"]) + def secret(self, req): + req.response(200, b'this is the secret') -dom.route("/update", route_update, methods=["post"], - post={ - "id" : "^[0-9]+$", - "user" : validate_user - } -) + def update(self, req): + req.populate_post() -dom.route("^/([a-f0-9]{32})$", route_gethash, methods=["get"]) + payload = {} + nr = req.argument("number") + user = req.argument("user") -dom.route("/secret", route_secret, methods=["get"], - auth={ - "type" : "header", # header or cookie are both supported. - "value" : "x-header", # header name or cookie name. - "redirect" : "/", # Redirect to / on auth failure, if not set - # auth failure will result in a 403. - "verify" : validate_auth # Callback to verify the authenticator. - } -) + if nr: + payload.update({"number": nr}) + if user: + payload.update({"user": user}) + + req.response(200, json.dumps(payload).encode()) + +koreapp = RouteExample() ``` ---