Skip to content

Latest commit

 

History

History
114 lines (77 loc) · 3.17 KB

DEV.md

File metadata and controls

114 lines (77 loc) · 3.17 KB

Develop your own function

TestDrive

Before you start development, you may want to take FaaS for a test drive which sets up a stack of sample functions from docker-compose.yml. You can then build your own functions and add them to the stack.

You can test-drive FaaS with a set of sample functions as defined in docker-compose.yml on play-with-docker.com for free, or on your own laptop.

Working on the API Gateway or Watchdog

To work on either of the FaaS Golang components checkout the "./build.sh" scripts and acompanying Dockerfiles.

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.

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.6/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.6/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.6/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.6/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