From 8a5afefc1c5c7fc9fd8a0724bf123c7b2049b28d Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 18 Jan 2023 17:12:33 +0100 Subject: [PATCH] :tada: Add prepl support And rename the current repl to urepl (user-repl). --- backend/scripts/repl | 5 ++++ backend/src/app/config.clj | 14 +++++---- backend/src/app/main.clj | 11 +++++-- backend/src/app/srepl.clj | 61 ++++++++++++++++++++++++++------------ 4 files changed, 63 insertions(+), 28 deletions(-) diff --git a/backend/scripts/repl b/backend/scripts/repl index 26bf66b88..d253345ee 100755 --- a/backend/scripts/repl +++ b/backend/scripts/repl @@ -10,6 +10,11 @@ export PENPOT_FLAGS="\ enable-demo-users \ disable-secure-session-cookies \ enable-smtp \ + enable-prepl-server \ + enable-urepl-server \ + enable-rpc-climit \ + enable-rpc-rlimit \ + enable-soft-rpc-rlimit \ enable-webhooks \ enable-access-tokens"; diff --git a/backend/src/app/config.clj b/backend/src/app/config.clj index 3ce625f70..2129525ba 100644 --- a/backend/src/app/config.clj +++ b/backend/src/app/config.clj @@ -64,8 +64,6 @@ :tenant "default" :redis-uri "redis://redis/0" - :srepl-host "127.0.0.1" - :srepl-port 6062 :assets-storage-backend :assets-fs :storage-assets-fs-directory "assets" @@ -197,8 +195,10 @@ (s/def ::smtp-ssl ::us/boolean) (s/def ::smtp-tls ::us/boolean) (s/def ::smtp-username (s/nilable ::us/string)) -(s/def ::srepl-host ::us/string) -(s/def ::srepl-port ::us/integer) +(s/def ::urepl-host ::us/string) +(s/def ::urepl-port ::us/integer) +(s/def ::prepl-host ::us/string) +(s/def ::prepl-port ::us/integer) (s/def ::assets-storage-backend ::us/keyword) (s/def ::storage-assets-fs-directory ::us/string) (s/def ::storage-assets-s3-bucket ::us/string) @@ -310,8 +310,10 @@ ::smtp-tls ::smtp-username - ::srepl-host - ::srepl-port + ::urepl-host + ::urepl-port + ::prepl-host + ::prepl-port ::assets-storage-backend ::storage-assets-fs-directory diff --git a/backend/src/app/main.clj b/backend/src/app/main.clj index f6fdaebbf..d7f0e72ea 100644 --- a/backend/src/app/main.clj +++ b/backend/src/app/main.clj @@ -30,6 +30,7 @@ [app.redis :as-alias rds] [app.rpc :as-alias rpc] [app.rpc.doc :as-alias rpc.doc] + [app.srepl :as-alias srepl] [app.storage :as-alias sto] [app.util.time :as dt] [app.worker :as-alias wrk] @@ -414,9 +415,13 @@ ::http.client/client (ig/ref ::http.client/client) ::props (ig/ref :app.setup/props)} - :app.srepl/server - {:port (cf/get :srepl-port) - :host (cf/get :srepl-host)} + [::srepl/urepl ::srepl/server] + {:port (cf/get :urepl-port 6062) + :host (cf/get :urepl-host "localhost")} + + [::srepl/prepl ::srepl/server] + {:port (cf/get :prepl-port 6063) + :host (cf/get :prepl-host "localhost")} :app.setup/builtin-templates {::http.client/client (ig/ref ::http.client/client)} diff --git a/backend/src/app/srepl.clj b/backend/src/app/srepl.clj index ed4b44467..cd5838a76 100644 --- a/backend/src/app/srepl.clj +++ b/backend/src/app/srepl.clj @@ -9,7 +9,10 @@ (:require [app.common.logging :as l] [app.common.spec :as us] + [app.config :as cf] [app.srepl.main] + [app.util.json :as json] + [app.util.locks :as locks] [clojure.core.server :as ccs] [clojure.main :as cm] [clojure.spec.alpha :as s] @@ -20,39 +23,59 @@ (ccs/repl-init) (in-ns 'app.srepl.main)) -(defn repl +(defn user-repl [] (cm/repl :init repl-init :read ccs/repl-read)) +(defn json-prepl + [] + (let [out *out* + lock (locks/create)] + (ccs/prepl *in* + (fn [m] + (binding [*out* out, *flush-on-newline* true, *print-readably* true] + (locks/locking lock + (println (json/encode-str m)))))))) + ;; --- State initialization -(s/def ::name ::us/not-empty-string) -(s/def ::port int?) +(s/def ::port ::us/integer) (s/def ::host ::us/not-empty-string) +(s/def ::flag #{:urepl-server :prepl-server}) +(s/def ::type #{::prepl ::urepl}) +(s/def ::key (s/tuple ::type ::us/keyword)) (defmethod ig/pre-init-spec ::server [_] - (s/keys :opt-un [::port ::host ::name])) + (s/keys :req [::flag] + :req-un [::port ::host])) (defmethod ig/prep-key ::server - [_ cfg] - (merge {:name "main"} cfg)) + [[type _] cfg] + (assoc cfg ::flag (keyword (str (name type) "-server")))) (defmethod ig/init-key ::server - [_ {:keys [port host name] :as cfg}] - (when (and port host name) - (l/info :msg "initializing server repl" :port port :host host :name name) - (ccs/start-server {:address host - :port port - :name name - :accept 'app.srepl/repl}) - cfg)) + [[type _] {:keys [::flag port host] :as cfg}] + (when (contains? cf/flags flag) + (let [accept (case type + ::prepl 'app.srepl/json-prepl + ::urepl 'app.srepl/user-repl) + params {:address host + :port port + :name (name type) + :accept accept}] + + (l/info :msg "initializing repl server" + :name (name type) + :port port + :host host) + + (ccs/start-server params) + + params))) (defmethod ig/halt-key! ::server - [_ cfg] - (when cfg - (ccs/stop-server (:name cfg)))) - - + [_ params] + (some-> params :name ccs/stop-server))