Skip to content

Commit

Permalink
Add hiccup Function example
Browse files Browse the repository at this point in the history
  • Loading branch information
ccfontes committed Nov 8, 2023
1 parent caf6d80 commit d966d7b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/faas_fn_build_invoke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,10 @@ jobs:
if [ "$(docker exec bb-routes-bell curl -X PUT --retry 3 --retry-delay 2 --retry-connrefused http://127.0.0.1:8082/foo/123)" != '[["id"],["123"]]' ]; then
exit 9
fi
(docker stop bb-hiccup || exit 0)
(docker rm bb-hiccup || exit 0)
bb_hiccup_output=$(docker exec bb-hiccup curl -X GET -d '{"item-list": ["foo","bar","spam"]}' --retry 3 --retry-delay 2 --retry-connrefused http://127.0.0.1:8082)
if [[ "${bb_hiccup_output:0:5}" != "<HTML" ]]; then
exit 10
fi
6 changes: 5 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ In `bb` language:
{:content-type "application/json"}
----

`:body` is the payload body. When passing a JSON payload and using `bb` language, the payload will be automatically parsed as a Clojure map with keyword keys. There are cases where it's preferable to have string keys in the payload body, and it's possible to support them by setting `keywords: false` in the Function in `stack.yml`:
`:body` is the payload body. When passing a JSON object payload and using `bb` language, the payload will be automatically parsed as a Clojure map with keyword keys. There are cases where it's preferable to have string keys in the payload body, and it's possible to support them by setting `keywords: false` in the Function in `stack.yml`:
[source, yml]
----
my-function:
Expand Down Expand Up @@ -102,6 +102,10 @@ In `bb-streaming` language:
----
The `event` is extracted from the HTTP payload body, and the function return is used as the HTTP payload body for the response.

== Adding namespaces

Namespace for each babashka source file created side by side with `handler.clj` must also be prefixed with `function.` Example: when creating a `view.clj` file in the same directory as `handler.clj`, namespace is `function.view`.

== link:examples[Function examples]

See the link:examples[examples] directory to find a fully working set of OpenFaaS Functions written in Babashka.
Expand Down
1 change: 1 addition & 0 deletions examples/http/bb-hiccup/bb.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:paths ["."]}
5 changes: 5 additions & 0 deletions examples/http/bb-hiccup/handler.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns function.handler
(:require [function.view :as view]))

(defn handler [{:keys [body]}]
(view/render-page "My Page" body))
17 changes: 17 additions & 0 deletions examples/http/bb-hiccup/view.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns function.view
(:require [hiccup2.core :refer [html]]))

(defn ->item-list [lst]
[:ul (for [x lst] [:li x])])

(defn hiccup-page [title {:keys [item-list]}]
[:html
[:head
[:title title]]
[:body
[:h1 title]
[:div {:id "item-list"}
(->item-list item-list)]]])

(defn render-page [title data]
(-> (hiccup-page title data) html str))
4 changes: 4 additions & 0 deletions examples/stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ functions:
lang: bb
handler: ./http/bb-routes-bell
image: ${DOCKER_REGISTRY_IMG_ORG_PATH}/bb-routes-bell
bb-hiccup:
lang: bb
handler: ./http/bb-hiccup
image: ${DOCKER_REGISTRY_IMG_ORG_PATH}/bb-hiccup
2 changes: 1 addition & 1 deletion template/bb/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ENV HOME /home/app
RUN addgroup --system app && adduser --system --ingroup app app && \
mkdir -p $HOME/.deps.clj/1.11.1.1347/ClojureTools && \
mv /root/.m2 $HOME && \
chown app:app -R $HOME/.m2 && \
chown app:app -R $HOME && \
mv /usr/local/lib/clojure/libexec/clojure-tools-1.11.1.1347.jar $HOME/.deps.clj/1.11.1.1347/ClojureTools

USER app
Expand Down

0 comments on commit d966d7b

Please sign in to comment.