Skip to content

Commit e4f4f42

Browse files
committed
Update examples in README and put http template first
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 9223d15 commit e4f4f42

File tree

2 files changed

+99
-74
lines changed

2 files changed

+99
-74
lines changed

README.md

+97-72
Original file line numberDiff line numberDiff line change
@@ -29,86 +29,28 @@ Are you referencing pip modules which require a native build toolchain? It's adv
2929

3030
## Downloading the templates
3131

32-
Using template pull:
32+
Using template pull with the repository's URL:
3333

3434
```bash
35-
faas template pull https://github.com/openfaas-incubator/python-flask-template
35+
faas-cli template pull https://github.com/openfaas-incubator/python-flask-template
3636
```
3737

38-
Using template store:
38+
Using the template store:
3939

4040
```bash
41-
faas template store pull python3-flask
42-
```
43-
44-
# Using the python3-flask template
45-
46-
Create a new function
47-
48-
```
49-
export OPENFAAS_PREFIX=alexellis2
50-
export FN="tester"
51-
faas new --lang python3-flask $FN
52-
```
53-
54-
Build, push, and deploy
55-
56-
```
57-
faas up -f $FN.yml
58-
```
59-
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-
def handle(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-
def handle(req):
82-
return "request accepted", 201
83-
```
41+
# Either command downloads both templates
42+
faas-cli template store pull python3-http
8443

85-
## Example of returning a custom HTTP code and content-type
86-
87-
```python
88-
def handle(req):
89-
return "request accepted", 201, {"Content-Type":"binary/octet-stream"}
44+
# Or
45+
faas-cli template store pull python3-flask
9046
```
9147

92-
## Example of accepting raw bytes in the request
93-
94-
Update stack.yml:
48+
Using your `stack.yml` file:
9549

9650
```yaml
97-
environment:
98-
RAW_BODY: True
99-
```
100-
101-
> 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 str(req)
51+
configuration:
52+
templates:
53+
- name: python3-http
11254
```
11355
11456
# Using the python3-http templates
@@ -118,19 +60,19 @@ Create a new function
11860
```
11961
export OPENFAAS_PREFIX=alexellis2
12062
export FN="tester"
121-
faas new --lang python3-http $FN
63+
faas-cli new --lang python3-http $FN
12264
```
12365

12466
Build, push, and deploy
12567

12668
```
127-
faas up -f $FN.yml
69+
faas-cli up -f $FN.yml
12870
```
12971

13072
Test the new function
13173

13274
```
133-
echo -n content | faas invoke $FN
75+
echo -n content | faas-cli invoke $FN
13476
```
13577

13678
## Event and Context Data
@@ -155,6 +97,19 @@ For example, returning a dict object type will automatically attach the header `
15597

15698
## Example usage
15799

100+
### Return a JSON body with a Content-Type
101+
102+
```python
103+
def handle(event, context):
104+
return {
105+
"statusCode": 200,
106+
"body": {"message": "Hello from OpenFaaS!"},
107+
"headers": {
108+
"Content-Type": "application/json"
109+
}
110+
}
111+
```
112+
158113
### Custom status codes and response bodies
159114

160115
Successful response status code and JSON response body
@@ -243,6 +198,76 @@ def handle(event, context):
243198
}
244199
```
245200

201+
# Using the python3-flask template
202+
203+
Create a new function
204+
205+
```
206+
export OPENFAAS_PREFIX=alexellis2
207+
export FN="tester"
208+
faas-cli new --lang python3-flask $FN
209+
```
210+
211+
Build, push, and deploy
212+
213+
```
214+
faas-cli up -f $FN.yml
215+
```
216+
217+
Test the new function
218+
219+
```
220+
echo -n content | faas-cli invoke $FN
221+
```
222+
223+
## Example of returning a string
224+
225+
```python
226+
def handle(req):
227+
"""handle a request to the function
228+
Args:
229+
req (str): request body
230+
"""
231+
232+
return "Hi" + str(req)
233+
```
234+
235+
## Example of returning a custom HTTP code
236+
237+
```python
238+
def handle(req):
239+
return "request accepted", 201
240+
```
241+
242+
## Example of returning a custom HTTP code and content-type
243+
244+
```python
245+
def handle(req):
246+
return "request accepted", 201, {"Content-Type":"binary/octet-stream"}
247+
```
248+
249+
## Example of accepting raw bytes in the request
250+
251+
Update stack.yml:
252+
253+
```yaml
254+
environment:
255+
RAW_BODY: True
256+
```
257+
258+
> Note: the value for `RAW_BODY` is case-sensitive.
259+
260+
```python
261+
def handle(req):
262+
"""handle a request to the function
263+
Args:
264+
req (str): request body
265+
"""
266+
267+
# req is bytes, so an input of "hello" returns i.e. b'hello'
268+
return str(req)
269+
```
270+
246271

247272
## Testing
248273
The `python3` templates will run `pytest` using `tox` during the `faas-cli build`. There are several options for controlling this.

template/python3-http/Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ WORKDIR /home/app/function/
3333
COPY function/requirements.txt .
3434
RUN pip install --user -r requirements.txt
3535

36-
#install function code
36+
# install function code
3737
USER root
3838
COPY function/ .
3939
RUN chown -R app:app ../
@@ -44,7 +44,7 @@ RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping tests" || eval "$TEST_COMMAN
4444

4545
WORKDIR /home/app/
4646

47-
#configure WSGI server and healthcheck
47+
# configure WSGI server and healthcheck
4848
USER app
4949

5050
ENV fprocess="python index.py"

0 commit comments

Comments
 (0)