-
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 single param as event of request w/ context & ret w/ …
…compojure render
- Loading branch information
Showing
20 changed files
with
885 additions
and
129 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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
(ns function.handler) | ||
|
||
(defn handler [{:keys [body]}] | ||
{:body (str "Hello, " (slurp body))}) | ||
{:body (str "Hello, " body)}) |
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 [content {:keys [headers env]}] | ||
[(keys content) (vals content) (:content-type headers) (:upstream-url env)]) | ||
(defn handler [{:keys [body headers context]}] | ||
[(keys body) (vals body) (:content-type headers) (:upstream-url context)]) |
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 [content] | ||
[(keys content) (vals content)]) | ||
(defn handler [{:keys [body]}] | ||
[(keys body) (vals body)]) |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
{:paths ["."] | ||
{:paths ["." "lib"] | ||
: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,58 +1,34 @@ | ||
(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"}} | ||
(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] | ||
[ring.middleware.json :refer [wrap-json-body]] | ||
[ring.middleware.text :refer [wrap-text-body]] | ||
[ring.middleware.headers :refer [wrap-lowercase-headers wrap-friendly-headers]] | ||
[ring.middleware.headers] | ||
[ring.util.walk :as ring-walk] | ||
[compojure.response :as response] | ||
[function.handler :as function])) | ||
|
||
(defn read-string [s] | ||
(try | ||
(let [res (edn/read-string s)] | ||
(if (symbol? res) (str res) res)) | ||
(catch Exception _ | ||
s))) | ||
(def keywords? #(if (nil? %) true %)) | ||
|
||
(defn keywords? [env-val] | ||
(if-some [keywords (edn/read-string env-val)] | ||
keywords | ||
true)) | ||
(defn ->handler [f env] | ||
(fn [request] | ||
(response/render f | ||
(assoc request :context env)))) | ||
|
||
(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 ->app [f env] | ||
(-> (->handler f env) | ||
(wrap-friendly-headers) | ||
(wrap-text-body) | ||
(wrap-json-body {:keywords? (-> env :keywords keywords?)}) | ||
(wrap-lowercase-headers))) | ||
|
||
(defn -main [] | ||
(run-server (->app #'function/handler (System/getenv)) | ||
{:port 8082}) | ||
@(promise)) | ||
(let [env (ring-walk/format-context (System/getenv))] | ||
(run-server (->app function/handler env) | ||
{:port 8082}) | ||
@(promise))) |
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,80 @@ | ||
(ns compojure.response | ||
"A protocol for generating Ring response maps" | ||
{:author "James Reeves" | ||
:contributors "Modified by Carlos da Cunha Fontes to work with Babashka" | ||
:url "https://github.com/weavejester/compojure" | ||
:license {:name "Distributed under the MIT License, the same as Ring."}} | ||
(:refer-clojure :exclude [send]) | ||
(:require [ring.util.mime-type :as mime] | ||
[ring.util.response :as response])) | ||
|
||
(defprotocol Renderable | ||
"A protocol that tells Compojure how to handle the return value of routes | ||
defined by [[GET]], [[POST]], etc. | ||
This protocol supports rendering strings, maps, functions, refs, files, seqs, | ||
input streams and URLs by default, and may be extended to cover many custom | ||
types." | ||
(render [x request] | ||
"Render `x` into a form suitable for the given request map.")) | ||
|
||
(defprotocol Sendable | ||
"A protocol that tells Compojure how to handle the return value of | ||
asynchronous routes, should they require special attention." | ||
(send* [x request respond raise])) | ||
|
||
(defn send | ||
"Send `x` as a Ring response. Checks to see if `x` satisfies [[Sendable]], | ||
and if not, falls back to [[Renderable]]." | ||
[x request respond raise] | ||
(if (satisfies? Sendable x) | ||
(send* x request respond raise) | ||
(respond (render x request)))) | ||
|
||
(defn- guess-content-type [response name] | ||
(if-let [mime-type (mime/ext-mime-type (str name))] | ||
(response/content-type response mime-type) | ||
response)) | ||
|
||
(extend-protocol Renderable | ||
nil | ||
(render [_ _] nil) | ||
String | ||
(render [body _] | ||
(-> (response/response body) | ||
(response/content-type "text/html; charset=utf-8"))) | ||
clojure.lang.IPersistentMap | ||
(render [resp-map _] | ||
(merge (with-meta (response/response "") (meta resp-map)) | ||
resp-map)) | ||
clojure.lang.Fn | ||
(render [func request] (render (func request) request)) | ||
clojure.lang.MultiFn | ||
(render [func request] (render (func request) request)) | ||
clojure.lang.IDeref | ||
(render [ref request] (render (deref ref) request)) | ||
java.io.File | ||
(render [file _] | ||
(-> (response/file-response (str file)) | ||
(guess-content-type file))) | ||
clojure.lang.ISeq | ||
(render [coll _] | ||
(-> (response/response coll))) | ||
clojure.lang.PersistentVector | ||
(render [coll _] | ||
(-> (response/response coll))) | ||
java.io.InputStream | ||
(render [stream _] (response/response stream)) | ||
java.net.URL | ||
(render [url _] | ||
(-> (response/url-response url) | ||
(guess-content-type url)))) | ||
|
||
(extend-protocol Sendable | ||
clojure.lang.Fn | ||
(send* [func request respond raise] | ||
(func request #(send % request respond raise) raise)) | ||
clojure.lang.MultiFn | ||
(send* [func request respond raise] | ||
(func request #(send % request respond raise) raise))) | ||
|
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,30 @@ | ||
(ns ring.middleware.headers | ||
{: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 stringify-keys]] | ||
[ring.util.walk :as ring-walk])) | ||
|
||
(defn wrap-friendly-headers | ||
"Middleware that converts all header: | ||
keys to keywords in the request, and back to strings in the response. | ||
values to clojure values in the request, and back to strings in the response. | ||
Purposed to be used right after the set defined handler, at the beginning of the middleware stack, or first of '->'" | ||
[handler] | ||
(fn [request] | ||
(let [->friendly-headers-req (comp ring-walk/read-val-strings keywordize-keys) | ||
->friendly-headers-resp (comp stringify-keys ring-walk/write-val-strings) | ||
response (handler (update request :headers ->friendly-headers-req))] | ||
(update response :headers ->friendly-headers-resp)))) | ||
|
||
(defn wrap-lowercase-headers | ||
"Middleware that converts all header keys in ring request and response to lowercase strings. | ||
Assumes that all middleware applied before this is configured with lower-case. | ||
Prevents outside world from breaking this stack prepared for HTTP/2. | ||
Prevents any user or middleware set headers from breaking HTTP/2 in the outside world. | ||
Purposed to be used at the end of the middleware stack, or last of '->'" | ||
[handler] | ||
(fn [request] | ||
(let [response (handler (update request :headers ring-walk/lowerify-keys))] | ||
(update response :headers ring-walk/lowerify-keys)))) |
Oops, something went wrong.