-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handler to take clj payload w/ optional context arg w/ headers & env
- Loading branch information
Showing
18 changed files
with
470 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
{:linters | ||
{:redefined-var {:level :off} | ||
:duplicate-require {:level :off} | ||
:namespace-name-mismatch {:level :off}}} | ||
:namespace-name-mismatch {:level :off}} | ||
:config-in-ns {tests {:linters {:unresolved-symbol {:exclude [(eg/eg) | ||
(eg/ex) | ||
(plumula.mimolette.alpha/defspec-test [spec-check-index])]}}} | ||
index {:linters {:invalid-arity {:level :off}}}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Unit tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- "*" | ||
pull_request: | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout this repo in current branch | ||
uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ github.head_ref || github.ref_name }} | ||
- name: Set up JDK 8 | ||
uses: actions/setup-java@v1 | ||
with: | ||
java-version: 8 | ||
- name: Install Babashka | ||
run: bash < <(curl -s https://raw.githubusercontent.com/babashka/babashka/master/install) | ||
- name: Run unit tests | ||
run: bb tests.clj | ||
working-directory: ${{ github.workspace }}/template/bb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
examples/build | ||
examples/template | ||
.clj-kondo/.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{:paths ["."]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(ns function.handler) | ||
|
||
(defn handler [content {:keys [headers env]}] | ||
[(keys content) (vals content) (:content-type headers) (:upstream-url env)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{:paths ["."]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(ns function.handler) | ||
|
||
(defn handler [content] | ||
[(keys content) (vals content)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{:paths ["."] | ||
:deps {eg/eg {:mvn/version "0.5.6-alpha"}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
(ns function.handler) | ||
|
||
(defn handler [{:keys [body]}] | ||
{:body body}) | ||
(defn handler [content] | ||
content) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,58 @@ | ||
#!/usr/bin/env bb | ||
(ns index ^{:author "Carlos da Cunha Fontes" | ||
:url "https://github.com/ccfontes/faas-bb" | ||
:license {:name "Distributed under the MIT License" | ||
:url "https://github.com/ccfontes/faas-bb/blob/main/LICENSE"}} | ||
(:require | ||
[clojure.walk :refer [keywordize-keys]] | ||
[clojure.string :as str :refer [lower-case]] | ||
[clojure.edn :as edn] | ||
[org.httpkit.server :refer [run-server]] | ||
[ring.middleware.json :as json-middleware] | ||
[function.handler :as function])) | ||
|
||
(require | ||
'[function.handler :as function] | ||
'[org.httpkit.server :refer [run-server]]) | ||
(defn read-string [s] | ||
(try | ||
(let [res (edn/read-string s)] | ||
(if (symbol? res) (str res) res)) | ||
(catch Exception _ | ||
s))) | ||
|
||
(run-server function/handler {:port 8082}) | ||
(defn keywords? [env-val] | ||
(if-some [keywords (edn/read-string env-val)] | ||
keywords | ||
true)) | ||
|
||
@(promise) | ||
(defn ->kebab-case [s] | ||
(lower-case (str/replace s #"_" "-"))) | ||
|
||
(def fn-arg-cnt (comp count first :arglists meta)) | ||
|
||
(defn format-context [m] | ||
(->> m | ||
(map (fn [[k v]] [(->kebab-case k) (read-string v)])) | ||
(into {}) | ||
(keywordize-keys))) | ||
|
||
(defn ->context [headers env] | ||
{:headers (format-context headers) | ||
:env (format-context env)}) | ||
|
||
(def response {:status 200}) | ||
|
||
(defn ->handler [f-var env] | ||
(let [f (var-get f-var) | ||
faas-fn (case (fn-arg-cnt f-var) | ||
1 (comp f :body) | ||
2 #(f (:body %) (->context (:headers %) env)))] | ||
(fn [request] | ||
(merge {:body (faas-fn request)} response)))) | ||
|
||
(defn ->app [f-var env] | ||
(-> (->handler f-var env) | ||
(json-middleware/wrap-json-body {:keywords? (keywords? (get env "keywords"))}) | ||
(json-middleware/wrap-json-response))) | ||
|
||
(defn -main [] | ||
(run-server (->app #'function/handler (System/getenv)) | ||
{:port 8082}) | ||
@(promise)) |
Oops, something went wrong.