Skip to content

Commit

Permalink
feat: add fhttp example command (#4)
Browse files Browse the repository at this point in the history
* initial ASGI implementation

* Example HTTP usage 'fhttp' cmd
  • Loading branch information
lkingland authored Sep 24, 2024
1 parent 7426ea0 commit c13b90c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/fhttp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Function HTTP Test Command

fhttp is a command which illustrates how the func-python library
wraps a function and exposes it as a service. Useful for development.


71 changes: 71 additions & 0 deletions cmd/fhttp/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import argparse
import logging
import func_python.http

logging.basicConfig(level=logging.INFO)

parser = argparse.ArgumentParser(description='Serve a Test Function')
parser.add_argument('--static', action='store_true',
help='Serve the example static handler (default is to '
'instantiate and serve the example class)')
args = parser.parse_args()


async def handle(scope, receive, send):
""" handle is an example of a static handler which can be sent to the
middleware as a funciton. It will be wrapped in a default Funciton
instance before being served as an ASGI application.
"""
logging.info("OK: static")

await send({
'type': 'http.response.start',
'status': 200,
'headers': [[b'content-type', b'text/plain']],
})
await send({
'type': 'http.response.body',
'body': 'OK: static'.encode(),
})


class Function:
""" Function is an example of a functioon instance. The structure
implements the function which will be deployed as a network service.
The class name can be changed. The only required method is "handle".
"""

async def handle(self, scope, receive, send):
""" handle is the only method which must be implemented by the
function instance for it to be served as an ASGI handler.
"""
logging.info("OK: instanced")

await send({
'type': 'http.response.start',
'status': 200,
'headers': [[b'content-type', b'text/plain']],
})
await send({
'type': 'http.response.body',
'body': 'OK: instanced'.encode(),
})


def new():
""" new is the factory function (or constructor) which will create
a new function instance when invoked. This must be named "new", and the
structure returned must include a method named "handle" which implements
the ASGI specification's method signature. The name of the class itself
can be changed.
"""
return Function()


if __name__ == "__main__":
if args.static:
logging.info("Starting static handler")
func_python.http.serve(handle)
else:
logging.info("Starting new instance")
func_python.http.serve(new)

0 comments on commit c13b90c

Please sign in to comment.