Skip to content

Commit

Permalink
Add HTTP mode template
Browse files Browse the repository at this point in the history
  • Loading branch information
ccfontes authored Apr 28, 2023
1 parent 7bf6084 commit 7ca1505
Show file tree
Hide file tree
Showing 22 changed files with 108 additions and 37 deletions.
4 changes: 4 additions & 0 deletions .clj-kondo/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{:linters
{:redefined-var {:level :off}
:duplicate-require {:level :off}
:namespace-name-mismatch {:level :off}}}
1 change: 1 addition & 0 deletions .github/workflows/clj-kondo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ jobs:
- uses: nnichols/clojure-lint-action@v2
with:
pattern: "*.clj"
fail_on_error: true
github_token: ${{ secrets.github_token }}
reporter: github-pr-review
30 changes: 22 additions & 8 deletions .github/workflows/faas_fn_build_invoke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@ jobs:
steps:
- name: Install OpenFaaS CLI
run: curl -sSL https://cli.openfaas.com | sudo -E sh
- name: Build Function from scratch and invoke it
- name: Build a Function for each language from scratch and invoke it
run: |
mkdir test
cd test
faas template pull https://github.com/${{ github.repository_owner }}/faas-bb#${{ github.head_ref || github.ref_name }}
faas new --lang bb my-bb-function --prefix ghcr.io/${{ github.repository_owner }}
faas build -f my-bb-function.yml
if [ "$(echo "Hello world" | docker run -i ghcr.io/${{ github.repository_owner }}/my-bb-function:latest ./index.clj)" != "Hello world" ]; then
faas new --lang bb-streaming my-bb-streaming-function --prefix ghcr.io/${{ github.repository_owner }}
faas build -f my-bb-streaming-function.yml
if [ "$(echo "Hello world" | docker run -i ghcr.io/${{ github.repository_owner }}/my-bb-streaming-function:latest ./index.clj)" != "Hello world" ]; then
exit 1
fi
faas new --lang bb my-bb-http-function --prefix ghcr.io/${{ github.repository_owner }}
(docker stop my-bb-http-function || exit 0)
(docker rm my-bb-http-function || exit 0)
faas build -f my-bb-http-function.yml
docker run -d --name my-bb-http-function ghcr.io/${{ github.repository_owner }}/my-bb-http-function:latest ./index.clj
if [ "$(docker exec my-bb-http-function curl -X POST --data-raw "Hello world" --retry 3 --retry-delay 2 --retry-connrefused http://127.0.0.1:8082)" != "Hello world" ]; then
exit 2
fi
- name: Build provided Function examples and invoke them
env:
DOCKER_REGISTRY_IMG_ORG_PATH: ghcr.io/${{ github.repository_owner }}
Expand All @@ -34,13 +42,19 @@ jobs:
cd examples
faas template pull https://github.com/${{ github.repository_owner }}/faas-bb#${{ github.head_ref || github.ref_name }}
faas build
if [ "$(echo world | docker run -i ghcr.io/${{ github.repository_owner }}/bb-hello:latest ./index.clj)" != "Hello, world" ]; then
if [ "$(echo world | docker run -i ghcr.io/${{ github.repository_owner }}/bb-streaming-hello:latest ./index.clj)" != "Hello, world" ]; then
exit 1
fi
if [ "$(echo '{"a" {"b" 10}}' | docker run -i ghcr.io/${{ github.repository_owner }}/bb-lib:latest ./index.clj)" != "[10]" ]; then
if [ "$(echo '{"a" {"b" 10}}' | docker run -i ghcr.io/${{ github.repository_owner }}/bb-streaming-lib:latest ./index.clj)" != "[10]" ]; then
exit 2
fi
if [ -n "$(echo '{"a" 10}' | docker run -i ghcr.io/${{ github.repository_owner }}/bb-lib:latest ./index.clj)" ]; then
if [ -n "$(echo '{"a" 10}' | docker run -i ghcr.io/${{ github.repository_owner }}/bb-streaming-lib:latest ./index.clj)" ]; then
exit 3
fi
docker run -i ghcr.io/${{ github.repository_owner }}/bb-lib:latest function/handler-test.clj
docker run -i ghcr.io/${{ github.repository_owner }}/bb-streaming-lib:latest function/handler-test.clj
(docker stop bb-http-hello || exit 0)
(docker rm bb-http-hello || exit 0)
docker run -d --name bb-http-hello ghcr.io/${{ github.repository_owner }}/bb-http-hello:latest ./index.clj
if [ "$(docker exec bb-http-hello curl -X POST --data-raw "world" --retry 3 --retry-delay 2 --retry-connrefused http://127.0.0.1:8082)" != "Hello, world" ]; then
exit 5
fi
8 changes: 3 additions & 5 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,18 @@ If you ever need to update the template, simply run the command above with the `

=== Create a Babashka Function ===

Two languages are supported: `bb` (HTTP mode) and `bb-streaming`.

Create Babashka Functions as with the following command example:
[source, bash]
----
faas new --lang bb my-bb-function
----
A new project is created for a function defined as `my-bb-function`. It will contain:
`of-watchdog` mode is HTTP. A new project is created for a function defined as `my-bb-function`. It will contain:

* a `function.handler` namespace that is required for the template to work properly. The requirement for this namespace is to have a top-level function defined as `handler`.
* a `bb.edn` file specifying classpath to find `function.handler`.

== Specs ==

OpenFaaS watchdog is `of-watchdog` in https://github.com/openfaas/of-watchdog#3-streaming-fork-modestreaming---default[Streaming fork] mode.

== link:examples[Function examples] ==

See the link:examples[examples] directory to find a fully working set of OpenFaaS Functions written in Babashka.
Expand Down
4 changes: 1 addition & 3 deletions examples/README.adoc
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
= OpenFaaS Functions in Babashka =

Hello world OpenFaaS Function in Babashka.

See the `bb-` prefixed directories to find the Babashka Function code examples.
Go into the link:bb[bb] and link:bb-streaming[bb-streaming] directories to find the Babashka Function code examples.

The following documentation explains how to run these Functions in OpenFaaS.

Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions examples/http/bb-hello/handler.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(ns function.handler)

(defn handler [{:keys [body]}]
{:body (str "Hello, " (slurp body))})
18 changes: 11 additions & 7 deletions examples/stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
bb-hello:
bb-streaming-hello:
lang: bb-streaming
handler: ./streaming/bb-hello
image: ${DOCKER_REGISTRY_IMG_ORG_PATH}/bb-streaming-hello
bb-streaming-lib:
lang: bb-streaming
handler: ./streaming/bb-lib
image: ${DOCKER_REGISTRY_IMG_ORG_PATH}/bb-streaming-lib
bb-http-hello:
lang: bb
handler: ./bb-hello
image: ${DOCKER_REGISTRY_IMG_ORG_PATH}/bb-hello
bb-lib:
lang: bb
handler: ./bb-lib
image: ${DOCKER_REGISTRY_IMG_ORG_PATH}/bb-lib
handler: ./http/bb-hello
image: ${DOCKER_REGISTRY_IMG_ORG_PATH}/bb-http-hello
1 change: 1 addition & 0 deletions examples/streaming/bb-hello/bb.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:paths ["."]}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
27 changes: 27 additions & 0 deletions template/bb-streaming/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM ghcr.io/openfaas/of-watchdog:0.9.11 AS watchdog
FROM babashka/babashka:1.3.176 AS babashka
FROM clojure:tools-deps-1.11.1.1257

RUN set -e

COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
COPY --from=babashka /usr/local/bin/bb /usr/local/bin/bb

ENV HOME /home/app

RUN addgroup --system app && adduser --system --ingroup app app && \
mkdir -p $HOME/.deps.clj/1.11.1.1257/ClojureTools && \
mv /root/.m2 $HOME && \
chown app:app -R $HOME/.m2 && \
mv /usr/local/lib/clojure/libexec/clojure-tools-1.11.1.1257.jar $HOME/.deps.clj/1.11.1.1257/ClojureTools

USER app
WORKDIR $HOME

COPY index.clj function/bb.edn ./
COPY function function

RUN bb prepare

HEALTHCHECK --interval=2s CMD [ -e /tmp/.lock ] || exit 1
CMD ["fwatchdog"]
1 change: 1 addition & 0 deletions template/bb-streaming/function/bb.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:paths ["."]}
3 changes: 3 additions & 0 deletions template/bb-streaming/function/handler.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(ns function.handler)

(defn handler [s] s)
16 changes: 16 additions & 0 deletions template/bb-streaming/index.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bb

(require '[function.handler :as function])

(defn read-lines
([] (read-lines ""))
([acc] (if-let [line (read-line)]
(recur (str acc line))
acc)))

(defn main []
(let [input (read-lines)
output (function/handler input)]
(println output)))

(main)
4 changes: 4 additions & 0 deletions template/bb-streaming/template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: bb-streaming
fprocess: ./index.clj
welcome_message: |
You have created a new Function which uses Babashka
4 changes: 3 additions & 1 deletion template/bb/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ COPY function function

RUN bb prepare

EXPOSE 8080
ENV mode="http"
ENV upstream_url="http://127.0.0.1:8082"

HEALTHCHECK --interval=2s CMD [ -e /tmp/.lock ] || exit 1
CMD ["fwatchdog"]
3 changes: 2 additions & 1 deletion template/bb/function/handler.clj
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
(ns function.handler)

(defn handler [s] s)
(defn handler [{:keys [body]}]
{:body body})
17 changes: 5 additions & 12 deletions template/bb/index.clj
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
#!/usr/bin/env bb

(require '[function.handler :as function])
(require
'[function.handler :as function]
'[org.httpkit.server :refer [run-server]])

(defn read-lines
([] (read-lines ""))
([acc] (if-let [line (read-line)]
(recur (str acc line))
acc)))
(run-server function/handler {:port 8082})

(defn main []
(let [input (read-lines)
output (function/handler input)]
(println output)))

(main)
@(promise)

0 comments on commit 7ca1505

Please sign in to comment.