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`.
# Using the python27-flask/python3-flask templates
44
+
# Using the python3-flask template
45
+
24
46
Create a new function
47
+
25
48
```
26
-
$ faas new --lang python27-flask <fn-name>
49
+
export OPENFAAS_PREFIX=alexellis2
50
+
export FN="tester"
51
+
faas new --lang python3-flask $FN
27
52
```
53
+
28
54
Build, push, and deploy
55
+
29
56
```
30
-
$ faas up -f <fn-name>.yml
57
+
faas up -f $FN.yml
31
58
```
59
+
32
60
Test the new function
61
+
62
+
```
63
+
echo -n content | faas invoke $FN
64
+
```
65
+
66
+
## Example of returning a string
67
+
68
+
```python
69
+
defhandle(req):
70
+
"""handle a request to the function
71
+
Args:
72
+
req (str): request body
73
+
"""
74
+
75
+
return"Hi"+str(req)
76
+
```
77
+
78
+
## Example of returning a custom HTTP code
79
+
80
+
```python
81
+
defhandle(req):
82
+
"""handle a request to the function
83
+
Args:
84
+
req (str): request body
85
+
"""
86
+
87
+
return201, "request accepted"
88
+
```
89
+
90
+
## Example of accepting raw bytes in the request
91
+
92
+
Update stack.yml:
93
+
94
+
```yaml
95
+
environment:
96
+
RAW_BODY: True
33
97
```
34
-
$ echo -n content | faas invoke <fn-name>
98
+
99
+
> Note: the value for `RAW_BODY` is case-sensitive.
100
+
101
+
```python
102
+
def handle(req):
103
+
"""handle a request to the function
104
+
Args:
105
+
req (str): request body
106
+
"""
107
+
108
+
# req is bytes, so an input of "hello" returns i.e. b'hello'
109
+
return string(req)
35
110
```
36
111
37
112
# Using the python3-http templates
113
+
38
114
Create a new function
115
+
39
116
```
40
-
$ faas new --lang python3-http <fn-name>
117
+
export OPENFAAS_PREFIX=alexellis2
118
+
export FN="tester"
119
+
faas new --lang python3-http $FN
41
120
```
121
+
42
122
Build, push, and deploy
123
+
43
124
```
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
125
+
faas up -f $FN.yml
49
126
```
127
+
50
128
Test the new function
129
+
51
130
```
52
-
$ curl -i $OPENFAAS_URL/function/<fn-name>
131
+
echo -n content | faas invoke $FN
53
132
```
54
133
55
134
## Event and Context Data
135
+
56
136
The function handler is passed two arguments, *event* and *context*.
57
137
58
138
*event* contains data about the request, including:
@@ -66,12 +146,15 @@ The function handler is passed two arguments, *event* and *context*.
66
146
- hostname
67
147
68
148
## Response Bodies
149
+
69
150
By default, the template will automatically attempt to set the correct Content-Type header for you based on the type of response.
70
151
71
152
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
153
73
154
## Example usage
155
+
74
156
### Custom status codes and response bodies
157
+
75
158
Successful response status code and JSON response body
0 commit comments