From 928d96c08013a3851eb3c78bed301e6776b4886e Mon Sep 17 00:00:00 2001 From: Soam Vasani Date: Wed, 18 Jan 2017 15:30:51 -0800 Subject: [PATCH] A trivial guestbook app Functions for GET and POST endpoints. A redis.yaml to deploy redis on kubernetes. And a deploy.sh to deploy the fission functions with routes. --- examples/python/guestbook/add.py | 16 ++++++++++ examples/python/guestbook/deploy.sh | 15 ++++++++++ examples/python/guestbook/get.py | 28 +++++++++++++++++ examples/python/guestbook/redis.yaml | 45 ++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 examples/python/guestbook/add.py create mode 100755 examples/python/guestbook/deploy.sh create mode 100644 examples/python/guestbook/get.py create mode 100644 examples/python/guestbook/redis.yaml diff --git a/examples/python/guestbook/add.py b/examples/python/guestbook/add.py new file mode 100644 index 00000000..5e923457 --- /dev/null +++ b/examples/python/guestbook/add.py @@ -0,0 +1,16 @@ +# +# Handles POST /guestbook -- adds item to guestbook +# + +from flask import request, redirect +import redis + +# Connect to redis. +redisConnection = redis.StrictRedis(host='redis.guestbook', port=6379, db=0) + +def main(): + # Read the item from POST params, add it to redis, and redirect + # back to the list + item = request.form['text'] + redisConnection.rpush('guestbook', item) + return redirect('/guestbook', code=303) diff --git a/examples/python/guestbook/deploy.sh b/examples/python/guestbook/deploy.sh new file mode 100755 index 00000000..812bd153 --- /dev/null +++ b/examples/python/guestbook/deploy.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +set -e + +kubectl create -f redis.yaml + +if [ -z "$FISSION_URL" ] +then + echo "Need $FISSION_URL set to a fission controller address" + exit 1 +fi + +fission env create --name python --image fission/python-env +fission function create --name guestbook-get --env python --code get.py --url /guestbook --method GET +fission function create --name guestbook-add --env python --code add.py --url /guestbook --method POST diff --git a/examples/python/guestbook/get.py b/examples/python/guestbook/get.py new file mode 100644 index 00000000..f4d00d5e --- /dev/null +++ b/examples/python/guestbook/get.py @@ -0,0 +1,28 @@ +# +# Handles GET /guestbook -- returns a list of items in the guestbook +# with a form to add more. +# + +from flask import current_app, escape +import redis + +# Connect to redis. This is run only when this file is loaded; as +# long as the pod is alive, the connection is reused. +redisConnection = redis.StrictRedis(host='redis.guestbook', port=6379, db=0) + +def main(): + messages = redisConnection.lrange('guestbook', 0, -1) + + items = [("
  • %s
  • " % escape(m.decode('utf-8'))) for m in messages] + ul = "" % "\n".join(items) + return """ + +

    Guestbook

    +
    + + +
    +
    + %s + + """ % ul diff --git a/examples/python/guestbook/redis.yaml b/examples/python/guestbook/redis.yaml new file mode 100644 index 00000000..786693e2 --- /dev/null +++ b/examples/python/guestbook/redis.yaml @@ -0,0 +1,45 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: guestbook + labels: + name: guestbook + +--- +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + labels: + run: redis + name: redis + namespace: guestbook +spec: + replicas: 1 + selector: + matchLabels: + run: redis + template: + metadata: + labels: + run: redis + spec: + containers: + - image: redis + name: redis + +--- +apiVersion: v1 +kind: Service +metadata: + labels: + run: redis + name: redis + namespace: guestbook +spec: + selector: + run: redis + type: ClusterIP + ports: + - port: 6379 + protocol: TCP + targetPort: 6379