-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
awb99
committed
Jun 25, 2024
1 parent
7fdc4f5
commit 289f86f
Showing
18 changed files
with
205 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
(ns build | ||
(:require | ||
[babashka.fs :as fs] | ||
[clojure.tools.build.api :as b] | ||
[deps-deploy.deps-deploy :as dd])) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,14 +7,28 @@ | |
|
||
:timbre {:start (modular.log/timbre-config! | ||
(:timbre/clj (deref (clip/ref :config))))} | ||
|
||
:webly {:start (webly.app.app/start-webly | ||
(deref (clip/ref :config)) | ||
(deref (clip/ref :config)) | ||
(:profile #ref [:modular])) | ||
:stop (webly.app.app/stop-webly this)} | ||
|
||
:clj-service {:start (goldly.service/start-clj-services | ||
(clip/ref :exts))} | ||
:stop (webly.app.app/stop-webly this)} | ||
|
||
:permission {:start (modular.permission.core/start-permissions | ||
{:demo {:roles #{:logistic} | ||
:password "a231498f6c1f441aa98482ea0b224ffa" ; "1234" | ||
:email ["[email protected]"]} | ||
:boss {:roles #{:logistic :supervisor :accounting} | ||
:password "a231498f6c1f441aa98482ea0b224ffa" ; "1234" | ||
:email ["[email protected]"]} | ||
:florian {:roles #{:logistic} | ||
:password "a231498f6c1f441aa98482ea0b224ffa" ; 1234 | ||
:email ["[email protected]"]} | ||
:john {:roles #{:logistic} | ||
:password "a231498f6c1f441aa98482ea0b224ffa" ; "1234" | ||
:email ["[email protected]"]}})} | ||
|
||
|
||
:clj-service {:start (clj-service.core/start-clj-services (clip/ref :permission) (clip/ref :exts))} | ||
|
||
; | ||
}} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
(ns clj-service.core | ||
(:require | ||
[clojure.set :refer [rename-keys]] | ||
[taoensso.timbre :as timbre :refer [info error]] | ||
[extension :refer [write-target-webly get-extensions]] | ||
[modular.permission.service :refer [add-permissioned-service]] | ||
[modular.clj-service.websocket :refer [create-websocket-responder]])) | ||
|
||
;; EXPOSE FUNCTION | ||
|
||
(defn- resolve-symbol [s] | ||
(try | ||
(requiring-resolve s) | ||
(catch Exception ex | ||
(error "Exception in exposing service " s " - symbol cannot be required.") | ||
(throw ex)))) | ||
|
||
(defn expose-function | ||
"exposes one function | ||
services args: this - created via clj-service.core | ||
permission-service - created via modular.permission.core/start-permissions | ||
function args: service - fully qualified symbol | ||
permission - a set following modular.permission role based access | ||
fixed-args - fixed args to be passed to the function executor as the beginning arguments" | ||
[this permission-service {:keys [function permission fixed-args] | ||
:or {fixed-args [] | ||
permission nil}}] | ||
(assert this "you need to pass the clj-service state") | ||
(assert permission-service "you need to pass the modular.permission.core state") | ||
(assert (symbol? function)) | ||
(let [service-fn (resolve-symbol function)] | ||
(add-permissioned-service permission-service function permission) | ||
(swap! this assoc function {:service-fn service-fn | ||
:permission permission | ||
:fixed-args fixed-args}))) | ||
|
||
(defn expose-functions | ||
"exposes multiple functions with the same permission and fixed-args." | ||
[this permission-service | ||
{:keys [function-symbols permission fixed-args name] | ||
:or {permission nil | ||
fixed-args [] | ||
name "services"}}] | ||
(assert (vector? function-symbols)) | ||
(info "exposing [" name "] permission: " permission " functions: " function-symbols) | ||
(doall | ||
(map (fn [function] | ||
(expose-function this permission-service {:function function | ||
:permission permission | ||
:fixed-args fixed-args})) function-symbols))) | ||
|
||
; services list | ||
|
||
(defn services-list [this] | ||
(keys @this)) | ||
|
||
; start service | ||
|
||
(defn- exts->services [exts] | ||
(->> (get-extensions exts {:clj-services nil}) | ||
(map :clj-services) | ||
(remove nil?))) | ||
|
||
(defn start-clj-services | ||
"starts the clj-service service. | ||
exposes stateless services that are discovered via the extension system. | ||
non stateless services need to be exposed via expose-service" | ||
[permission-service exts] | ||
(info "starting clj-services ..") | ||
(let [this (atom {}) | ||
services (exts->services exts)] | ||
(write-target-webly :clj-services services) | ||
; expose services list (which is stateful) | ||
(expose-function this permission-service | ||
{:service-fn 'clj-service.core/services-list | ||
:permission nil | ||
:fixed-args [this]}) | ||
; expose stateless services discovered via extension-manager | ||
(doall | ||
(map (fn [clj-service] | ||
(expose-functions | ||
this permission-service | ||
(rename-keys clj-service {:symbols :function-symbols}))) | ||
services)) | ||
; create websocket message handler | ||
(create-websocket-responder this permission-service) | ||
; return the service state | ||
this)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
(ns clj-service.executor | ||
(:require | ||
[de.otto.nom.core :as nom] | ||
[modular.permission.core :refer [user-authorized?]])) | ||
|
||
;; USER | ||
|
||
(defonce ^:dynamic | ||
^{:doc "The user-id for which a clj-service gets executed"} | ||
*user* nil) | ||
|
||
(defn execute [this permission-service user-id fun-symbol & args] | ||
(if-let [fun (get this fun-symbol)] | ||
(let [{:keys [function _permission fixed-args]} fun | ||
full-args (concat fixed-args args)] | ||
(if (user-authorized? permission-service fun-symbol user-id) | ||
(try {:result (apply function full-args)} | ||
(catch clojure.lang.ExceptionInfo e | ||
(nom/fail ::exception {:fun fun-symbol | ||
:error (pr-str e) | ||
:message (str "exception when executing function " fun-symbol)})) | ||
(catch AssertionError e | ||
(nom/fail ::assert-error {:fun fun-symbol | ||
:error (pr-str e) | ||
:message (str "assert error when executing function " fun-symbol)})) | ||
(catch Exception e | ||
(nom/fail ::exception {:fun fun-symbol | ||
:error (pr-str e) | ||
:message (str "exception when executing function " fun-symbol)}))) | ||
(nom/fail ::no-permission {:fun fun-symbol | ||
:user user-id | ||
:message (str "user-id " user-id " is not authorized for: " fun-symbol)}))) | ||
(nom/fail ::function-not-found {:fun fun-symbol | ||
:message "No service defined for this symbol."}))) | ||
|
12 changes: 7 additions & 5 deletions
12
src/goldly/service/handler.clj → src/clj_service/handler.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
(ns clj-service.websocket | ||
(:require | ||
[clojure.string] | ||
[de.otto.nom.core :as nom] | ||
[modular.ws.core :refer [send-response]] | ||
[modular.ws.msg-handler :refer [-event-msg-handler]] | ||
[modular.permission.session :refer [get-user]] | ||
[clj-service.executor :refer [execute *user*]])) | ||
|
||
(defn create-websocket-responder [this permission-service] | ||
(defmethod -event-msg-handler :clj/service | ||
[{:keys [event _id _?data uid] :as req}] | ||
(let [[_ params] event ; _ is :clj/service | ||
{:keys [fun args]} params | ||
user (get-user permission-service uid)] | ||
(future | ||
(let [r (binding [*user* user] | ||
(execute this permission-service user fun args))] | ||
(if (nom/anomaly? r) | ||
(send-response req :clj/service {:error "Execution exception" | ||
:uid uid}) | ||
(send-response req :clj/service {:result r}))))))) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.