<commit_before>;; lein new compojure-api k3
;; cd k3
;; replace handler.clj with this
;; lein ring server
(ns salvador.handler
  (:require [compojure.api.sweet :refer :all]
            [ring.util.http-response :refer :all]
            [schema.core :as s]
            [clojure.java.io :as io]
            [org.httpkit.client :as http]
            [ring-aws-lambda-adapter.core :refer [defhandler]]
            [salvador.core :as env]
            [clostache.parser :refer [render]]
            [clojure.walk :refer [keywordize-keys]]
            [plumbing.core :refer [fnk]]))

(def aws-gateway-options
  {:x-amazon-apigateway-integration
   {:responses {:default {:statusCode "200"
                          :responseTemplates {"application/json" "$input.json('$.body')"
                                              "text/html" "$input.json('$body.template-body')"}}}
    :requestTemplates { "application/json" (slurp (io/resource "bodymapping.vm")) }
    :uri (str "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/" (env/env :lambda-arn) "/invocations")
    :httpMethod "POST"
    :type "aws"}})

(def template-resource
  (resource
    {:get
     (merge
       aws-gateway-options
       {:responses {200 {:schema s/Any :description "Default response"}
                    302 {:schema s/Any :description "Redirect to `continue` location"}
                    500 {:schema s/Any :description "Horror"}}
        :parameters {:query-params {:template String}}
        :summary "Render template with request map"
        :handler (fn [request]
                   (let [template (-> request :query-params :template)
                         parameters (dissoc (keywordize-keys (:query-params request)) :template)
                         {:keys [status headers body error] :as string} @(http/get template)]
                     {:status 200
                      :headers {"content-type" "text/html" ;gets overwritten
                                "X-Content-Type" "text/html"}
                      :body {:template-source template
                          :parameters parameters
                          :template-body body
                          :content-type "text/html"
                          :template-expanded (render body parameters)}}))})}))

(def app
  (api
    {:swagger
     {:ui "/"
      :spec "/swagger.json"}}
      (context "/render" [] template-resource)))

(defhandler salvador.handler.Lambda app {})
<commit_msg>Fixed synatx<commit_after>;; lein new compojure-api k3
;; cd k3
;; replace handler.clj with this
;; lein ring server
(ns salvador.handler
  (:require [compojure.api.sweet :refer :all]
            [ring.util.http-response :refer :all]
            [schema.core :as s]
            [clojure.java.io :as io]
            [org.httpkit.client :as http]
            [ring-aws-lambda-adapter.core :refer [defhandler]]
            [salvador.core :as env]
            [clostache.parser :refer [render]]
            [clojure.walk :refer [keywordize-keys]]
            [plumbing.core :refer [fnk]]))

(def aws-gateway-options
  {:x-amazon-apigateway-integration
   {:responses {:default {:statusCode "200"
                          :responseParameters {"method.response.header.Content-Type" "integration.response.body.headers.X-Content-Type"}
                          :responseTemplates {"application/json" "$input.json('$.body')"
                                              "text/html" "$input.json('$body.template-body')"}}}
    :requestTemplates { "application/json" (slurp (io/resource "bodymapping.vm")) }
    :uri (str "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/" (env/env :lambda-arn) "/invocations")
    :httpMethod "POST"
    :type "aws"}})

(def template-resource
  (resource
    {:get
     (merge
       aws-gateway-options
       {:responses {200 {:schema s/Any :description "Default response"}
                    302 {:schema s/Any :description "Redirect to `continue` location"}
                    500 {:schema s/Any :description "Horror"}}
        :parameters {:query-params {:template String}}
        :summary "Render template with request map"
        :handler (fn [request]
                   (let [template (-> request :query-params :template)
                         parameters (dissoc (keywordize-keys (:query-params request)) :template)
                         {:keys [status headers body error] :as string} @(http/get template)]
                     {:status 200
                      :headers {"content-type" "text/html" ;gets overwritten
                                "X-Content-Type" "text/html"}
                      :body {:template-source template
                          :parameters parameters
                          :template-body body
                          :content-type "text/html"
                          :template-expanded (render body parameters)}}))})}))

(def app
  (api
    {:swagger
     {:ui "/"
      :spec "/swagger.json"}}
      (context "/render" [] template-resource)))

(defhandler salvador.handler.Lambda app {})

{'fraction_non_alphanumeric': 0.1342721142403808, 'fraction_numerical': 0.010313367711225704, 'mean_word_length': 1.9234782608695653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3592821', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1397, 'n_words': 340}