Git Product home page Git Product logo

pyfun's Introduction

Function framework for Python

This framework provides a mechanism to write Function-as-a-Service style code in Python for handling CloudEvents delivered via HTTP.

This framework is primarily intended to work with Knative, but also works to provide a generic server for handling CloudEvents over HTTP (e.g. from Kubernetes or on a local machine).

Usage:

import logging

from pyfun_events import Handle,Get

counter = 0


# @Handle assumes json body. For string or other body conversion, try:
# @Handle(str)
@Handle
def DoEvent(data: str, context: dict):
    logging.info(data)
    counter = counter + 1


@Handle(path="/secret")
def DoOther(data: str, context: dict):
    if data.get("handshake") == "backwards":
        counter = 0
        return "It's gone, man"


@Get
def Info():
    return "Got {0}".format(counter)
    
    
@Get("/dance")
def Party():
    return "<BLINK>Like it's 1999</BLINK>"

Running manually

Copy packaging/config.py and packaging/requirements.txt into your application directory alongside your other code. You can then start the Flask webserver running your function with:

FLASK_APP=config
flask run

Running on Knative

There is a supplied build template in packaging/build-template.yaml, which you can apply to your cluster with:

kubectl apply -f packaging/build-template.yaml

or

kubectl apply -f https://github.com/evankanderson/pyfun/tree/master/packaging/build-template.yaml

Then update your Knative Service like so:

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: message-dumper
spec:
  runLatest:
    configuration:
+     build:
+       source:
+          git:
+            url: YOUR_REPO_URL
+            revision: HEAD
+       template:
+         name: pyfn
+         arguments:
+         - name: IMAGE
+           value: YOUR_DOCKER_IMAGE
+       serviceAccountName: builder
  revisionTemplate:
    spec:
      container:
        image: YOUR_DOCKER_IMAGE

pyfun's People

Contributors

evankanderson avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.