mirror of
https://github.com/openfaas/faas.git
synced 2025-06-09 16:56:47 +00:00
This is part of the following activity: https://github.com/openfaas/docs/pull/249 Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
107 lines
2.7 KiB
Markdown
107 lines
2.7 KiB
Markdown
## Develop your own function
|
|
|
|
### Working on the API Gateway or Watchdog
|
|
|
|
To work on either of the FaaS Golang components checkout the "./build.sh" scripts and acompanying Dockerfiles.
|
|
|
|
* [Roadmap and Contributing](https://github.com/openfaas/faas/blob/master/ROADMAP.md)
|
|
|
|
### Creating a function
|
|
|
|
Functions run as Docker containers with the Watchdog component embedded to handle communication with the API Gateway.
|
|
|
|
You can find the [reference documentation for the Watchdog here](https://github.com/openfaas/faas/tree/master/watchdog).
|
|
|
|
**Markdown Parser**
|
|
|
|
This is the basis of a function which generates HTML from MarkDown:
|
|
|
|
```
|
|
FROM golang:1.9.7
|
|
RUN mkdir -p /go/src/app
|
|
COPY handler.go /go/src/app
|
|
WORKDIR /go/src/app
|
|
RUN go get github.com/microcosm-cc/bluemonday && \
|
|
go get github.com/russross/blackfriday
|
|
|
|
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
|
|
|
|
ADD https://github.com/openfaas/faas/releases/download/0.9.14/fwatchdog /usr/bin
|
|
RUN chmod +x /usr/bin/fwatchdog
|
|
|
|
ENV fprocess="/go/src/app/app"
|
|
|
|
CMD ["/usr/bin/fwatchdog"]
|
|
```
|
|
|
|
The base Docker container is not important, you just need to add the watchdog component and then set the fprocess to execute your binary at runtime.
|
|
|
|
Update the Docker stack with this:
|
|
|
|
```
|
|
markdown:
|
|
image: alexellis2/faas-markdownrender:latest
|
|
labels:
|
|
function: "true"
|
|
networks:
|
|
- functions
|
|
```
|
|
|
|
**Word counter with busybox**
|
|
|
|
```
|
|
FROM alpine:latest
|
|
|
|
ADD https://github.com/openfaas/faas/releases/download/0.9.14/fwatchdog /usr/bin
|
|
RUN chmod +x /usr/bin/fwatchdog
|
|
|
|
ENV fprocess="wc"
|
|
CMD ["fwatchdog"]
|
|
```
|
|
|
|
Update your Docker stack with this definition:
|
|
|
|
```
|
|
wordcount:
|
|
image: alexellis2/faas-alpinefunction:latest
|
|
labels:
|
|
function: "true"
|
|
networks:
|
|
- functions
|
|
environment:
|
|
fprocess: "wc"
|
|
```
|
|
|
|
**Tip:**
|
|
|
|
You can optimize Docker to cache getting the watchdog by using curl, instead of ADD.
|
|
To do so, replace the related lines with:
|
|
|
|
```
|
|
RUN apt-get update && apt-get install -y curl \
|
|
&& curl -sL https://github.com/openfaas/faas/releases/download/0.9.14/fwatchdog > /usr/bin/fwatchdog \
|
|
&& chmod +x /usr/bin/fwatchdog \
|
|
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
|
|
```
|
|
|
|
or with the following for Alpine based images:
|
|
|
|
```
|
|
RUN apk --no-cache add curl \
|
|
&& curl -sL https://github.com/openfaas/faas/releases/download/0.9.14/fwatchdog > /usr/bin/fwatchdog \
|
|
&& chmod +x /usr/bin/fwatchdog
|
|
```
|
|
|
|
### Testing your function
|
|
|
|
You can test your function through a webbrowser against the UI portal on port 8080.
|
|
|
|
http://localhost:8080/
|
|
|
|
You can also invoke a function by name with curl:
|
|
|
|
```
|
|
curl --data-binary @README.md http://localhost:8080/function/func_wordcount
|
|
```
|
|
|