2020-12-24 14:32:19 +01:00
|
|
|
;; 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.
|
|
|
|
;;
|
2021-01-07 11:26:41 +01:00
|
|
|
;; Copyright (c) 2020-2021 UXBOX Labs SL
|
2020-12-24 14:32:19 +01:00
|
|
|
|
|
|
|
(ns app.rpc
|
|
|
|
(:require
|
|
|
|
[app.common.data :as d]
|
2021-01-25 09:29:41 +01:00
|
|
|
[app.common.exceptions :as ex]
|
2020-12-24 14:32:19 +01:00
|
|
|
[app.common.spec :as us]
|
|
|
|
[app.db :as db]
|
|
|
|
[app.metrics :as mtx]
|
2021-01-25 09:29:41 +01:00
|
|
|
[app.rlimits :as rlm]
|
2020-12-24 14:32:19 +01:00
|
|
|
[app.util.services :as sv]
|
|
|
|
[clojure.spec.alpha :as s]
|
|
|
|
[clojure.tools.logging :as log]
|
|
|
|
[cuerdas.core :as str]
|
|
|
|
[integrant.core :as ig]))
|
|
|
|
|
|
|
|
(defn- default-handler
|
2021-01-31 19:25:26 +01:00
|
|
|
[_]
|
2020-12-24 14:32:19 +01:00
|
|
|
(ex/raise :type :not-found))
|
|
|
|
|
2021-02-22 12:48:21 +01:00
|
|
|
(defn- run-hook
|
|
|
|
[hook-fn response]
|
|
|
|
(ex/ignoring (hook-fn))
|
|
|
|
response)
|
|
|
|
|
2020-12-24 14:32:19 +01:00
|
|
|
(defn- rpc-query-handler
|
|
|
|
[methods {:keys [profile-id] :as request}]
|
|
|
|
(let [type (keyword (get-in request [:path-params :type]))
|
|
|
|
data (assoc (:params request) ::type type)
|
|
|
|
data (if profile-id
|
|
|
|
(assoc data :profile-id profile-id)
|
|
|
|
(dissoc data :profile-id))
|
|
|
|
result ((get methods type default-handler) data)
|
|
|
|
mdata (meta result)]
|
|
|
|
|
|
|
|
(cond->> {:status 200 :body result}
|
2021-03-05 17:30:49 +01:00
|
|
|
(fn? (:transform-response mdata))
|
|
|
|
((:transform-response mdata) request))))
|
2020-12-24 14:32:19 +01:00
|
|
|
|
|
|
|
(defn- rpc-mutation-handler
|
|
|
|
[methods {:keys [profile-id] :as request}]
|
|
|
|
(let [type (keyword (get-in request [:path-params :type]))
|
|
|
|
data (d/merge (:params request)
|
|
|
|
(:body-params request)
|
|
|
|
(:uploads request))
|
|
|
|
data (if profile-id
|
|
|
|
(assoc data :profile-id profile-id)
|
|
|
|
(dissoc data :profile-id))
|
|
|
|
result ((get methods type default-handler) data)
|
|
|
|
mdata (meta result)]
|
|
|
|
(cond->> {:status 200 :body result}
|
2021-02-22 12:48:21 +01:00
|
|
|
(fn? (:transform-response mdata))
|
|
|
|
((:transform-response mdata) request)
|
|
|
|
|
|
|
|
(fn? (:before-complete mdata))
|
|
|
|
(run-hook (:before-complete mdata)))))
|
2020-12-24 14:32:19 +01:00
|
|
|
|
2021-01-25 09:29:41 +01:00
|
|
|
(defn- wrap-with-metrics
|
2021-01-25 20:14:40 +01:00
|
|
|
[cfg f mdata]
|
|
|
|
(mtx/wrap-summary f (::mobj cfg) [(::sv/name mdata)]))
|
2020-12-24 14:32:19 +01:00
|
|
|
|
2021-01-25 09:29:41 +01:00
|
|
|
;; Wrap the rpc handler with a semaphore if it is specified in the
|
|
|
|
;; metadata asocciated with the handler.
|
|
|
|
(defn- wrap-with-rlimits
|
|
|
|
[cfg f mdata]
|
|
|
|
(if-let [key (:rlimit mdata)]
|
|
|
|
(let [rlinst (get-in cfg [:rlimits key])]
|
|
|
|
(when-not rlinst
|
|
|
|
(ex/raise :type :internal
|
|
|
|
:code :rlimit-not-configured
|
|
|
|
:hint (str/fmt "%s rlimit not configured" key)))
|
2021-02-22 12:39:55 +01:00
|
|
|
(log/tracef "adding rlimit to '%s' rpc handler" (::sv/name mdata))
|
2021-01-25 09:29:41 +01:00
|
|
|
(fn [cfg params]
|
|
|
|
(rlm/execute rlinst (f cfg params))))
|
|
|
|
f))
|
|
|
|
|
|
|
|
(defn- wrap-impl
|
2021-01-25 20:14:40 +01:00
|
|
|
[cfg f mdata]
|
2021-01-25 09:29:41 +01:00
|
|
|
(let [f (wrap-with-rlimits cfg f mdata)
|
2021-01-25 20:14:40 +01:00
|
|
|
f (wrap-with-metrics cfg f mdata)
|
|
|
|
spec (or (::sv/spec mdata) (s/spec any?))]
|
2021-02-22 12:39:55 +01:00
|
|
|
(log/tracef "registering '%s' command to rpc service" (::sv/name mdata))
|
2020-12-24 14:32:19 +01:00
|
|
|
(fn [params]
|
|
|
|
(when (and (:auth mdata true) (not (uuid? (:profile-id params))))
|
2021-01-08 14:12:56 +01:00
|
|
|
(ex/raise :type :authentication
|
|
|
|
:code :authentication-required
|
2021-01-08 14:31:24 +01:00
|
|
|
:hint "authentication required for this endpoint"))
|
2020-12-24 14:32:19 +01:00
|
|
|
(f cfg (us/conform spec params)))))
|
|
|
|
|
|
|
|
(defn- process-method
|
2021-01-25 20:14:40 +01:00
|
|
|
[cfg vfn]
|
2020-12-24 14:32:19 +01:00
|
|
|
(let [mdata (meta vfn)]
|
|
|
|
[(keyword (::sv/name mdata))
|
2021-01-25 20:14:40 +01:00
|
|
|
(wrap-impl cfg (deref vfn) mdata)]))
|
2020-12-24 14:32:19 +01:00
|
|
|
|
|
|
|
(defn- resolve-query-methods
|
|
|
|
[cfg]
|
2021-01-25 20:14:40 +01:00
|
|
|
(let [mobj (mtx/create
|
|
|
|
{:name "rpc_query_timing"
|
|
|
|
:labels ["name"]
|
|
|
|
:registry (get-in cfg [:metrics :registry])
|
2021-02-22 12:52:53 +01:00
|
|
|
:type :histogram
|
2021-01-25 20:14:40 +01:00
|
|
|
:help "Timing of query services."})
|
|
|
|
cfg (assoc cfg ::mobj mobj)]
|
|
|
|
(->> (sv/scan-ns 'app.rpc.queries.projects
|
|
|
|
'app.rpc.queries.files
|
|
|
|
'app.rpc.queries.teams
|
|
|
|
'app.rpc.queries.comments
|
|
|
|
'app.rpc.queries.profile
|
|
|
|
'app.rpc.queries.recent-files
|
|
|
|
'app.rpc.queries.viewer)
|
|
|
|
(map (partial process-method cfg))
|
|
|
|
(into {}))))
|
2020-12-24 14:32:19 +01:00
|
|
|
|
|
|
|
(defn- resolve-mutation-methods
|
|
|
|
[cfg]
|
2021-01-25 20:14:40 +01:00
|
|
|
(let [mobj (mtx/create
|
|
|
|
{:name "rpc_mutation_timing"
|
|
|
|
:labels ["name"]
|
|
|
|
:registry (get-in cfg [:metrics :registry])
|
2021-02-22 12:52:53 +01:00
|
|
|
:type :histogram
|
2021-01-25 20:14:40 +01:00
|
|
|
:help "Timing of mutation services."})
|
|
|
|
cfg (assoc cfg ::mobj mobj)]
|
|
|
|
(->> (sv/scan-ns 'app.rpc.mutations.demo
|
|
|
|
'app.rpc.mutations.media
|
|
|
|
'app.rpc.mutations.profile
|
|
|
|
'app.rpc.mutations.files
|
|
|
|
'app.rpc.mutations.comments
|
|
|
|
'app.rpc.mutations.projects
|
|
|
|
'app.rpc.mutations.viewer
|
|
|
|
'app.rpc.mutations.teams
|
2021-02-25 14:54:00 +01:00
|
|
|
'app.rpc.mutations.management
|
2021-02-18 14:07:13 +01:00
|
|
|
'app.rpc.mutations.ldap
|
2021-01-25 20:14:40 +01:00
|
|
|
'app.rpc.mutations.verify-token)
|
|
|
|
(map (partial process-method cfg))
|
|
|
|
(into {}))))
|
2020-12-24 14:32:19 +01:00
|
|
|
|
|
|
|
(s/def ::storage some?)
|
|
|
|
(s/def ::session map?)
|
|
|
|
(s/def ::tokens fn?)
|
|
|
|
|
|
|
|
(defmethod ig/pre-init-spec ::rpc [_]
|
2021-01-25 09:29:41 +01:00
|
|
|
(s/keys :req-un [::db/pool ::storage ::session ::tokens ::mtx/metrics ::rlm/rlimits]))
|
2020-12-24 14:32:19 +01:00
|
|
|
|
|
|
|
(defmethod ig/init-key ::rpc
|
|
|
|
[_ cfg]
|
|
|
|
(let [mq (resolve-query-methods cfg)
|
|
|
|
mm (resolve-mutation-methods cfg)]
|
|
|
|
{:methods {:query mq :mutation mm}
|
|
|
|
:query-handler #(rpc-query-handler mq %)
|
|
|
|
:mutation-handler #(rpc-mutation-handler mm %)}))
|