mirror of
https://github.com/penpot/penpot.git
synced 2025-03-12 15:51:37 -05:00
🚧 Adapt backend code to vertx changes.
This commit is contained in:
parent
fd6362e463
commit
cb0ff7b82f
5 changed files with 125 additions and 106 deletions
|
@ -12,7 +12,7 @@
|
||||||
[uxbox.core :refer [system]]
|
[uxbox.core :refer [system]]
|
||||||
[uxbox.config :as cfg]
|
[uxbox.config :as cfg]
|
||||||
[uxbox.http.errors :as errors]
|
[uxbox.http.errors :as errors]
|
||||||
[uxbox.http.interceptors :as interceptors]
|
[uxbox.http.middleware :as middleware]
|
||||||
[uxbox.http.session :as session]
|
[uxbox.http.session :as session]
|
||||||
[uxbox.http.handlers :as handlers]
|
[uxbox.http.handlers :as handlers]
|
||||||
[uxbox.http.debug :as debug]
|
[uxbox.http.debug :as debug]
|
||||||
|
@ -20,7 +20,7 @@
|
||||||
[vertx.core :as vc]
|
[vertx.core :as vc]
|
||||||
[vertx.http :as vh]
|
[vertx.http :as vh]
|
||||||
[vertx.web :as vw]
|
[vertx.web :as vw]
|
||||||
[vertx.web.interceptors :as vxi]))
|
[vertx.web.middleware :as vwm]))
|
||||||
|
|
||||||
(defn- on-start
|
(defn- on-start
|
||||||
[ctx]
|
[ctx]
|
||||||
|
@ -30,30 +30,32 @@
|
||||||
:allow-methods #{:post :get :patch :head :options :put}
|
:allow-methods #{:post :get :patch :head :options :put}
|
||||||
:allow-headers #{:x-requested-with :content-type :cookie}}
|
:allow-headers #{:x-requested-with :content-type :cookie}}
|
||||||
|
|
||||||
interceptors [(vxi/cookies)
|
routes [["/sub/:file-id" {:middleware [[vwm/cookies]
|
||||||
(vxi/params)
|
[vwm/cors cors-opts]
|
||||||
(vxi/cors cors-opts)
|
[middleware/format-response-body]
|
||||||
interceptors/parse-request-body
|
[session/auth]]
|
||||||
interceptors/format-response-body
|
:handler ws/handler
|
||||||
(vxi/errors errors/handle)]
|
:method :get}]
|
||||||
|
|
||||||
routes [["/sub/:file-id" {:interceptors [(vxi/cookies)
|
["/api" {:middleware [[vwm/cookies]
|
||||||
(vxi/cors cors-opts)
|
[vwm/params]
|
||||||
interceptors/format-response-body
|
[vwm/cors cors-opts]
|
||||||
(session/auth)]
|
[middleware/parse-request-body]
|
||||||
:get ws/handler}]
|
[middleware/format-response-body]
|
||||||
|
[middleware/method-match]
|
||||||
|
[vwm/errors errors/handle]]}
|
||||||
|
["/echo" {:handler handlers/echo-handler}]
|
||||||
|
|
||||||
["/api" {:interceptors interceptors}
|
["/login" {:handler handlers/login-handler
|
||||||
["/echo" {:all handlers/echo-handler}]
|
:method :post}]
|
||||||
["/login" {:post handlers/login-handler}]
|
["/logout" {:handler handlers/logout-handler
|
||||||
["/logout" {:post handlers/logout-handler}]
|
:method :post}]
|
||||||
["/debug"
|
["/w" {:middleware [session/auth]}
|
||||||
["/emails" {:get debug/emails-list}]
|
["/mutation/:type" {:middleware [vwm/uploads]
|
||||||
["/emails/:id" {:get debug/email}]]
|
:handler handlers/mutation-handler
|
||||||
["/w" {:interceptors [(session/auth)]}
|
:method :post}]
|
||||||
["/mutation/:type" {:interceptors [(vxi/uploads)]
|
["/query/:type" {:handler handlers/query-handler
|
||||||
:post handlers/mutation-handler}]
|
:method :get}]]]]
|
||||||
["/query/:type" {:get handlers/query-handler}]]]]
|
|
||||||
|
|
||||||
handler (vw/handler ctx
|
handler (vw/handler ctx
|
||||||
(vw/assets "/media/*" {:root "resources/public/media/"})
|
(vw/assets "/media/*" {:root "resources/public/media/"})
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
(ns uxbox.http.handlers
|
(ns uxbox.http.handlers
|
||||||
(:require
|
(:require
|
||||||
[promesa.core :as p]
|
[promesa.core :as p]
|
||||||
|
[uxbox.common.exceptions :as ex]
|
||||||
[uxbox.emails :as emails]
|
[uxbox.emails :as emails]
|
||||||
[uxbox.http.session :as session]
|
[uxbox.http.session :as session]
|
||||||
[uxbox.services.init]
|
[uxbox.services.init]
|
||||||
|
|
|
@ -1,72 +0,0 @@
|
||||||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
||||||
;;
|
|
||||||
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
|
||||||
;; defined by the Mozilla Public License, v. 2.0.
|
|
||||||
;;
|
|
||||||
;; Copyright (c) 2019-2020 Andrey Antukh <niwi@niwi.nz>
|
|
||||||
|
|
||||||
(ns uxbox.http.interceptors
|
|
||||||
(:require
|
|
||||||
[vertx.web :as vw]
|
|
||||||
[uxbox.config :as cfg]
|
|
||||||
[uxbox.common.exceptions :as ex]
|
|
||||||
[uxbox.util.transit :as t])
|
|
||||||
(:import
|
|
||||||
io.vertx.ext.web.RoutingContext
|
|
||||||
io.vertx.ext.web.FileUpload
|
|
||||||
io.vertx.core.buffer.Buffer))
|
|
||||||
|
|
||||||
(def parse-request-body
|
|
||||||
{:enter (fn [{:keys [request] :as data}]
|
|
||||||
(let [body (:body request)
|
|
||||||
mtype (get-in request [:headers "content-type"])]
|
|
||||||
(if (= "application/transit+json" mtype)
|
|
||||||
(try
|
|
||||||
(let [params (t/decode (t/buffer->bytes body))]
|
|
||||||
(update data :request assoc :body-params params))
|
|
||||||
(catch Exception e
|
|
||||||
(ex/raise :type :parse
|
|
||||||
:message "Unable to parse transit from request body."
|
|
||||||
:cause e)))
|
|
||||||
data)))})
|
|
||||||
|
|
||||||
(def format-response-body
|
|
||||||
{:leave (fn [{:keys [response] :as data}]
|
|
||||||
(let [body (:body response)
|
|
||||||
type (if (:debug-humanize-transit cfg/config)
|
|
||||||
:json-verbose
|
|
||||||
:json)]
|
|
||||||
(cond
|
|
||||||
(coll? body)
|
|
||||||
(-> data
|
|
||||||
(assoc-in [:response :body]
|
|
||||||
(t/bytes->buffer (t/encode body {:type type})))
|
|
||||||
(update-in [:response :headers]
|
|
||||||
assoc "content-type" "application/transit+json"))
|
|
||||||
|
|
||||||
(nil? body)
|
|
||||||
(-> data
|
|
||||||
(assoc-in [:response :status] 204)
|
|
||||||
(assoc-in [:response :body] ""))
|
|
||||||
|
|
||||||
:else
|
|
||||||
data)))})
|
|
||||||
|
|
||||||
(def handle-uploads
|
|
||||||
{:enter (fn [data]
|
|
||||||
(let [rcontext (get-in data [:request ::vw/routing-context])
|
|
||||||
uploads (.fileUploads ^RoutingContext rcontext)
|
|
||||||
uploads (reduce (fn [acc ^FileUpload upload]
|
|
||||||
(assoc acc
|
|
||||||
(keyword (.name upload))
|
|
||||||
{:type :uploaded-file
|
|
||||||
:mtype (.contentType upload)
|
|
||||||
:path (.uploadedFileName upload)
|
|
||||||
:name (.fileName upload)
|
|
||||||
:size (.size upload)}))
|
|
||||||
{}
|
|
||||||
uploads)]
|
|
||||||
(assoc-in data [:request :upload-params] uploads)))})
|
|
||||||
|
|
85
backend/src/uxbox/http/middleware.clj
Normal file
85
backend/src/uxbox/http/middleware.clj
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
;;
|
||||||
|
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||||
|
;; defined by the Mozilla Public License, v. 2.0.
|
||||||
|
;;
|
||||||
|
;; Copyright (c) 2019-2020 Andrey Antukh <niwi@niwi.nz>
|
||||||
|
|
||||||
|
(ns uxbox.http.middleware
|
||||||
|
(:require
|
||||||
|
[promesa.core :as p]
|
||||||
|
[vertx.web :as vw]
|
||||||
|
[uxbox.config :as cfg]
|
||||||
|
[uxbox.common.exceptions :as ex]
|
||||||
|
[uxbox.util.transit :as t])
|
||||||
|
(:import
|
||||||
|
io.vertx.ext.web.RoutingContext
|
||||||
|
io.vertx.ext.web.FileUpload
|
||||||
|
io.vertx.core.buffer.Buffer))
|
||||||
|
|
||||||
|
(defn- wrap-parse-request-body
|
||||||
|
[handler]
|
||||||
|
(fn [{:keys [headers body] :as request}]
|
||||||
|
(let [mtype (get headers "content-type")]
|
||||||
|
(if (= "application/transit+json" mtype)
|
||||||
|
(try
|
||||||
|
(let [params (t/decode (t/buffer->bytes body))]
|
||||||
|
(handler (assoc request :body-params params)))
|
||||||
|
(catch Exception e
|
||||||
|
(ex/raise :type :parse
|
||||||
|
:message "Unable to parse transit from request body."
|
||||||
|
:cause e)))
|
||||||
|
(handler request)))))
|
||||||
|
|
||||||
|
(def parse-request-body
|
||||||
|
{:name ::parse-request-body
|
||||||
|
:compile (constantly wrap-parse-request-body)})
|
||||||
|
|
||||||
|
(defn- impl-format-response-body
|
||||||
|
[response]
|
||||||
|
(let [body (:body response)
|
||||||
|
type (if (:debug-humanize-transit cfg/config)
|
||||||
|
:json-verbose
|
||||||
|
:json)]
|
||||||
|
(cond
|
||||||
|
(coll? body)
|
||||||
|
(-> response
|
||||||
|
(assoc :body (t/bytes->buffer (t/encode body {:type type})))
|
||||||
|
(update :headers assoc
|
||||||
|
"content-type"
|
||||||
|
"application/transit+json"))
|
||||||
|
|
||||||
|
(nil? body)
|
||||||
|
(assoc response :status 204 :body "")
|
||||||
|
|
||||||
|
:else
|
||||||
|
response)))
|
||||||
|
|
||||||
|
(defn- wrap-format-response-body
|
||||||
|
[handler]
|
||||||
|
(fn [request]
|
||||||
|
(-> (p/do! (handler request))
|
||||||
|
(p/then' (fn [response]
|
||||||
|
(cond-> response
|
||||||
|
(map? response) (impl-format-response-body)))))))
|
||||||
|
|
||||||
|
(def format-response-body
|
||||||
|
{:name ::format-response-body
|
||||||
|
:compile (constantly wrap-format-response-body)})
|
||||||
|
|
||||||
|
|
||||||
|
(defn- wrap-method-match
|
||||||
|
[handler]
|
||||||
|
(fn [request]))
|
||||||
|
|
||||||
|
(def method-match
|
||||||
|
{:name ::method-match
|
||||||
|
:compile (fn [data opts]
|
||||||
|
(when-let [method (:method data)]
|
||||||
|
(fn [handler]
|
||||||
|
(fn [request]
|
||||||
|
(if (= (:method request) method)
|
||||||
|
(handler request)
|
||||||
|
{:status 405 :body ""})))))})
|
|
@ -7,7 +7,6 @@
|
||||||
(ns uxbox.http.session
|
(ns uxbox.http.session
|
||||||
(:require
|
(:require
|
||||||
[promesa.core :as p]
|
[promesa.core :as p]
|
||||||
[sieppari.context :as spx]
|
|
||||||
[vertx.core :as vc]
|
[vertx.core :as vc]
|
||||||
[uxbox.db :as db]
|
[uxbox.db :as db]
|
||||||
[uxbox.util.uuid :as uuid]))
|
[uxbox.util.uuid :as uuid]))
|
||||||
|
@ -45,12 +44,16 @@
|
||||||
(catch java.lang.IllegalArgumentException e
|
(catch java.lang.IllegalArgumentException e
|
||||||
nil)))
|
nil)))
|
||||||
|
|
||||||
(defn auth
|
(defn- wrap-auth
|
||||||
[]
|
[handler]
|
||||||
{:enter (fn [data]
|
(fn [request]
|
||||||
(let [token (parse-token (:request data))]
|
(let [token (parse-token request)]
|
||||||
(-> (retrieve token)
|
(-> (retrieve token)
|
||||||
(p/then' (fn [user-id]
|
(p/then (fn [profile-id]
|
||||||
(if user-id
|
(if profile-id
|
||||||
(update data :request assoc :user user-id)
|
(handler (assoc request :profile-id profile-id))
|
||||||
data))))))})
|
(handler request))))))))
|
||||||
|
|
||||||
|
(def auth
|
||||||
|
{:nane ::auth
|
||||||
|
:compile (constantly wrap-auth)})
|
||||||
|
|
Loading…
Add table
Reference in a new issue