Skip to content

Commit d091c01

Browse files
committed
Sync examples
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
1 parent 90a1fa9 commit d091c01

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

README.md

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ Learn more about the tiers at [https://www.openfaas.com/pricing/](https://www.op
5454
5555
### Code samples
5656

57-
You can generate new functions using the `faas-cli` and built-in templates or use any binary for Windows or Linux in a container.
57+
You can scaffold a new function using the `faas-cli new` command passing in the name of the function and the language template you want to use i.e. `faas-cli new --lang node20 stripe-webhooks`.
5858

5959
Official templates exist for many popular languages and are easily extensible with Dockerfiles.
6060

61-
* Node.js (`node12`) example:
61+
Learn about [OpenFaaS templates in the docs](https://docs.openfaas.com/languages/overview/)
62+
63+
* Node.js (`node20`) example:
6264

6365
```js
6466
"use strict"
@@ -76,39 +78,40 @@ Official templates exist for many popular languages and are easily extensible wi
7678
```
7779
*handler.js*
7880

79-
* Python 3 example:
81+
* Python 3 example (`python3-http`):
8082

8183
```python
82-
import requests
83-
84-
def handle(req):
85-
r = requests.get(req, timeout = 1)
86-
return "{} => {:d}".format(req, r.status_code)
84+
def handle(event, context):
85+
return {
86+
"statusCode": 200,
87+
"body": "Hello from OpenFaaS!"
88+
}
8789
```
90+
8891
*handler.py*
8992

90-
* Golang example (`golang-http`)
93+
* Golang example (`golang-middleware`)
9194

92-
```golang
95+
```go
9396
package function
94-
97+
9598
import (
96-
"fmt"
97-
"net/http"
98-
99-
handler "github.com/openfaas/templates-sdk/go-http"
99+
"fmt"
100+
"io"
101+
"net/http"
100102
)
101-
102-
// Handle a function invocation
103-
func Handle(req handler.Request) (handler.Response, error) {
104-
var err error
105-
106-
message := fmt.Sprintf("Body: %s", string(req.Body))
107-
108-
return handler.Response{
109-
Body: []byte(message),
110-
StatusCode: http.StatusOK,
111-
}, err
103+
104+
func Handle(w http.ResponseWriter, r *http.Request) {
105+
var input []byte
106+
107+
if r.Body != nil {
108+
defer r.Body.Close()
109+
body, _ := io.ReadAll(r.Body)
110+
input = body
111+
}
112+
113+
w.WriteHeader(http.StatusOK)
114+
w.Write([]byte(fmt.Sprintf("Body: %s", string(input))))
112115
}
113116
```
114117

0 commit comments

Comments
 (0)