Skip to content

Commit

Permalink
Update examples in README and put http template first
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Oct 3, 2022
1 parent 9223d15 commit e4f4f42
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 74 deletions.
169 changes: 97 additions & 72 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,86 +29,28 @@ Are you referencing pip modules which require a native build toolchain? It's adv

## Downloading the templates

Using template pull:
Using template pull with the repository's URL:

```bash
faas template pull https://github.com/openfaas-incubator/python-flask-template
faas-cli template pull https://github.com/openfaas-incubator/python-flask-template
```

Using template store:
Using the template store:

```bash
faas template store pull python3-flask
```

# Using the python3-flask template

Create a new function

```
export OPENFAAS_PREFIX=alexellis2
export FN="tester"
faas new --lang python3-flask $FN
```

Build, push, and deploy

```
faas up -f $FN.yml
```

Test the new function

```
echo -n content | faas invoke $FN
```

## Example of returning a string

```python
def handle(req):
"""handle a request to the function
Args:
req (str): request body
"""

return "Hi" + str(req)
```

## Example of returning a custom HTTP code

```python
def handle(req):
return "request accepted", 201
```
# Either command downloads both templates
faas-cli template store pull python3-http

## Example of returning a custom HTTP code and content-type

```python
def handle(req):
return "request accepted", 201, {"Content-Type":"binary/octet-stream"}
# Or
faas-cli template store pull python3-flask
```

## Example of accepting raw bytes in the request

Update stack.yml:
Using your `stack.yml` file:

```yaml
environment:
RAW_BODY: True
```

> Note: the value for `RAW_BODY` is case-sensitive.
```python
def handle(req):
"""handle a request to the function
Args:
req (str): request body
"""

# req is bytes, so an input of "hello" returns i.e. b'hello'
return str(req)
configuration:
templates:
- name: python3-http
```

# Using the python3-http templates
Expand All @@ -118,19 +60,19 @@ Create a new function
```
export OPENFAAS_PREFIX=alexellis2
export FN="tester"
faas new --lang python3-http $FN
faas-cli new --lang python3-http $FN
```

Build, push, and deploy

```
faas up -f $FN.yml
faas-cli up -f $FN.yml
```

Test the new function

```
echo -n content | faas invoke $FN
echo -n content | faas-cli invoke $FN
```

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

## Example usage

### Return a JSON body with a Content-Type

```python
def handle(event, context):
return {
"statusCode": 200,
"body": {"message": "Hello from OpenFaaS!"},
"headers": {
"Content-Type": "application/json"
}
}
```

### Custom status codes and response bodies

Successful response status code and JSON response body
Expand Down Expand Up @@ -243,6 +198,76 @@ def handle(event, context):
}
```

# Using the python3-flask template

Create a new function

```
export OPENFAAS_PREFIX=alexellis2
export FN="tester"
faas-cli new --lang python3-flask $FN
```

Build, push, and deploy

```
faas-cli up -f $FN.yml
```

Test the new function

```
echo -n content | faas-cli invoke $FN
```

## Example of returning a string

```python
def handle(req):
"""handle a request to the function
Args:
req (str): request body
"""

return "Hi" + str(req)
```

## Example of returning a custom HTTP code

```python
def handle(req):
return "request accepted", 201
```

## Example of returning a custom HTTP code and content-type

```python
def handle(req):
return "request accepted", 201, {"Content-Type":"binary/octet-stream"}
```

## Example of accepting raw bytes in the request

Update stack.yml:

```yaml
environment:
RAW_BODY: True
```

> Note: the value for `RAW_BODY` is case-sensitive.
```python
def handle(req):
"""handle a request to the function
Args:
req (str): request body
"""

# req is bytes, so an input of "hello" returns i.e. b'hello'
return str(req)
```


## Testing
The `python3` templates will run `pytest` using `tox` during the `faas-cli build`. There are several options for controlling this.
Expand Down
4 changes: 2 additions & 2 deletions template/python3-http/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ WORKDIR /home/app/function/
COPY function/requirements.txt .
RUN pip install --user -r requirements.txt

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

WORKDIR /home/app/

#configure WSGI server and healthcheck
# configure WSGI server and healthcheck
USER app

ENV fprocess="python index.py"
Expand Down

0 comments on commit e4f4f42

Please sign in to comment.