diff --git a/backend/src/app/http.clj b/backend/src/app/http.clj index 3fd8f3fc8..53c1113d3 100644 --- a/backend/src/app/http.clj +++ b/backend/src/app/http.clj @@ -88,8 +88,9 @@ (rr/create-resource-handler {:path "/"}) (rr/create-default-handler)))) + (defn- create-router - [{:keys [session rpc google-auth gitlab-auth metrics ldap-auth storage] :as cfg}] + [{:keys [session rpc google-auth gitlab-auth metrics ldap-auth storage svgparse] :as cfg}] (rr/router [["/metrics" {:get (:handler metrics)}] @@ -107,7 +108,7 @@ [middleware/keyword-params] [middleware/cookies]]} - ;; ["/svg" {:post handlers/parse-svg}] + ["/svg" {:post svgparse}] ["/oauth" ["/google" {:post (:auth-handler google-auth)}] diff --git a/backend/src/app/main.clj b/backend/src/app/main.clj index 5ed0e2370..f0f2fc7a0 100644 --- a/backend/src/app/main.clj +++ b/backend/src/app/main.clj @@ -80,8 +80,13 @@ :google-auth (ig/ref :app.http.auth/google) :gitlab-auth (ig/ref :app.http.auth/gitlab) :ldap-auth (ig/ref :app.http.auth/ldap) + :svgparse (ig/ref :app.svgparse/handler) :storage (ig/ref :app.storage/storage)} + ;; HTTP Handler for SVG parsing + :app.svgparse/handler + {:metrics (ig/ref :app.metrics/metrics)} + :app.rpc/rpc {:pool (ig/ref :app.db/pool) :session (ig/ref :app.http.session/session) diff --git a/backend/src/app/svgparse.clj b/backend/src/app/svgparse.clj index 5dc54c518..05d05dc9f 100644 --- a/backend/src/app/svgparse.clj +++ b/backend/src/app/svgparse.clj @@ -10,9 +10,13 @@ (ns app.svgparse (:require [app.common.exceptions :as ex] - [clojure.xml :as xml] + [app.common.spec :as us] + [app.metrics :as mtx] + [clojure.java.io :as io] [clojure.java.shell :as shell] - [clojure.java.io :as io]) + [clojure.spec.alpha :as s] + [clojure.xml :as xml] + [integrant.core :as ig]) (:import java.io.InputStream org.apache.commons.io.IOUtils)) @@ -35,3 +39,29 @@ (with-open [istream (io/input-stream input)] (-> (clean-svg istream) (xml/parse)))) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Handler +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(declare handler) + +(defmethod ig/pre-init-spec ::handler [_] + (s/keys :req-un [::mtx/metrics])) + +(defmethod ig/init-key ::handler + [_ {:keys [metrics] :as cfg}] + (->> {:registry (:registry metrics) + :type :summary + :name "http_handler_svgparse_timing" + :help "svg parse timings"} + (mtx/instrument handler))) + +(defn- handler + [{:keys [headers body] :as request}] + (when (not= "image/svg+xml" (get headers "content-type")) + (ex/raise :type :validation + :code :unsupported-mime-type + :mime (get headers "content-type"))) + {:status 200 + :body (parse body)})