Hi Stu! Here’s req->ring
:
(defn- get-path
"Extracts request URI, removing the leading `/<stage>` path added by AWS.
This implementation fails if no such leading stage is found in the path."
[req]
(let [s (get-in req [:datomic.ion.edn.api-gateway/data :requestContext :stage] "stg")
p (get-in req [:datomic.ion.edn.api-gateway/data :requestContext :http :path] "/")]
(get (str/split p (re-pattern (str "/" s))) 1 "/"))) ; get rid of stage leading path!
(defn- get-req-method
"Extracts request method."
[req]
(-> req
(get-in [:datomic.ion.edn.api-gateway/data :requestContext :http :method] "GET")
str/lower-case
keyword))
;; The following libs threaded the same path before us. We might need to look at their code for more inspiration.
;; https://github.com/mhjort/ring-apigw-lambda-proxy/blob/master/src/ring/middleware/apigw.clj
;; https://github.com/jpb/ring-aws-lambda-adapter/blob/master/src/ring_aws_lambda_adapter/core.clj
;; Also, this other example appeared after our implementation and seems to contain great wisdom.
;; https://github.com/cnuernber/cljs-lambda-gateway-example/blob/master/src/gateway_example/proxy_lambda.clj
;; He seems to have learned some tricks from https://github.com/FieryCod/holy-lambda
(defn- req->ring
"Transforms an ionized API Gateway request into a more ring-like request.
See https://github.com/ring-clojure/ring/blob/master/SPEC.
See https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html.
See https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html."
[req]
(assoc req
:server-port (get-in req [:datomic.ion.edn.api-gateway/data :headers :x-forwarded-port] 443)
;;:server-name (get-in req [:headers "host"] "example.com") ; is already present
:remote-addr (get-in req [:datomic.ion.edn.api-gateway/data :headers :x-forwarded-for] "0.0.0.0")
:uri (get-path req)
;;:query-string (generate-query-string
;; (get-in req [:datomic.ion.edn.api-gateway/data :queryStringParameters] {}))
:scheme (keyword (get-in req [:datomic.ion.edn.api-gateway/data :headers :x-forwarded-proto] "https"))
:request-method (get-req-method req)
:protocol (get-in req [:datomic.ion.edn.api-gateway/data :requestContext :http :protocol] "HTTP/1.1")
;;:ssl-client-cert ; ignored
;;:headers ; are already present
;;:body InputStream
))