You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Clarifies expectations for usage of the templates in this
repo along with an update to the python3-flask template so that
it provides a better upgrade path.
Examples added for python3-flask to show how to return a HTTP
error or status code.
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
The Python Flask templates that make use of the incubator project [of-watchdog](https://github.com/openfaas-incubator/of-watchdog).
5
5
6
6
Templates available in this repository:
7
+
7
8
- python27-flask
8
9
- python3-flask
9
10
- python3-flask-debian
10
11
- python3-flask-armhf
12
+
11
13
- python3-http
12
14
- python3-http-debian
13
15
- python3-http-armhf
14
16
15
17
Notes:
16
18
- To build and deploy a function for Raspberry Pi or ARMv7 in general, use the language templates ending in *-armhf*
17
19
20
+
## Picking your template
21
+
22
+
The templates named `python*-flask*` are designed as a drop-in replacement for the classic `python3` template, but using the more efficient of-watchdog. The move to use flask as an underlying framework allows for greater control over the HTTP request and response.
23
+
24
+
Those templates named `python*-http*` are designed to offer full control over the HTTP request and response. Flask is used as an underlying framework.
25
+
26
+
The `witness` HTTP server is used along with Flask for all templates.
27
+
28
+
Are you referencing pip modules which require a native build toolchain? It's advisable to use the template with a `-debian` suffix in this case. The Debian images are larger, however they are usually more efficient for use with modules like `numpy` and `pandas`.
> Note: the value for `RAW_BODY` is case-sensitive.
102
+
103
+
```python
104
+
def handle(req):
105
+
"""handle a request to the function
106
+
Args:
107
+
req (str): request body
108
+
"""
109
+
110
+
# req is bytes, so an input of "hello" returns i.e. b'hello'
111
+
return string(req)
35
112
```
36
113
37
114
# Using the python3-http templates
115
+
38
116
Create a new function
117
+
39
118
```
40
-
$ faas new --lang python3-http <fn-name>
119
+
export OPENFAAS_PREFIX=alexellis2
120
+
export FN="tester"
121
+
faas new --lang python3-http $FN
41
122
```
123
+
42
124
Build, push, and deploy
125
+
43
126
```
44
-
$ faas up -f <fn-name>.yml
45
-
```
46
-
Set your OpenFaaS gateway URL. For example:
47
-
```
48
-
$ OPENFAAS_URL=http://127.0.0.1:8080
127
+
faas up -f $FN.yml
49
128
```
129
+
50
130
Test the new function
131
+
51
132
```
52
-
$ curl -i $OPENFAAS_URL/function/<fn-name>
133
+
echo -n content | faas invoke $FN
53
134
```
54
135
55
136
## Event and Context Data
137
+
56
138
The function handler is passed two arguments, *event* and *context*.
57
139
58
140
*event* contains data about the request, including:
@@ -66,12 +148,15 @@ The function handler is passed two arguments, *event* and *context*.
66
148
- hostname
67
149
68
150
## Response Bodies
151
+
69
152
By default, the template will automatically attempt to set the correct Content-Type header for you based on the type of response.
70
153
71
154
For example, returning a dict object type will automatically attach the header `Content-Type: application/json` and returning a string type will automatically attach the `Content-Type: text/html, charset=utf-8` for you.
72
155
73
156
## Example usage
157
+
74
158
### Custom status codes and response bodies
159
+
75
160
Successful response status code and JSON response body
0 commit comments