forked from knative-extensions/func-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
90 lines (75 loc) · 2.73 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import argparse
import logging
from func_python.http import serve
# Set the default logging level to INFO
logging.basicConfig(level=logging.INFO)
# Allow this test to be either instanced (default) or --static
# to test the two different primary method signatures supported in the
# final Function.
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()
# Example static handler.
# Enable with --static
# Must be named exactly "handle"
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(),
})
# Example instanced handler
# This is the default expected by this test.
# The class can be named anything. See "new" below.
class MyFunction:
async def __call__(self, scope, receive, send):
logging.info("OK")
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 alive(self):
logging.info("liveness checked")
return True, "I'm alive!"
def ready(self):
logging.info("readiness checked")
return True, "I'm ready!"
def stop(self):
logging.info("Cleanup on stop")
# Add cleanup logic here if necessary
# Funciton instance constructor
# expected to be named exactly "new"
# Must return a callable which conforms to the ASGI spec.
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 MyFunction()
# Run the example.
# Start either the static or instanced handler depending on flag --static
if __name__ == "__main__":
if args.static:
logging.info("Starting static handler")
serve(handle)
else:
logging.info("Starting new instance")
serve(new)