Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for async engine operations and objects with identity #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM clojure:lein-2.8.1-alpine AS build
ENV DEBUG_COMPILE ${DEBUG_COMPILE}
ENV NO_PERF_COMPILE ${NO_PERF_COMPILE}
ENV SHOW_RULES ${SHOW_RULES}
WORKDIR /app
ADD . /app
RUN lein clean && \
lein uberjar

FROM openjdk:jre-alpine
COPY --from=build /app/target/arete-0.6.1-standalone.jar /
CMD ["java", "-cp", "/arete-0.6.1-standalone.jar", "engine.api", "id-function=(fn [x] [(:namespace x) (:kind x) (:name x)])"]
14 changes: 13 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@
:url "http://www.eclipse.org/legal/epl-v10.html"}
:main engine.viewer
:dependencies [[org.clojure/clojure "1.10.0"]
[org.clojure/tools.logging "0.4.0"]
[org.flatland/ordered "1.5.7"]
;; https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
[javax.xml.bind/jaxb-api "2.3.0"]
;; https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime
[org.glassfish.jaxb/jaxb-runtime "2.3.3"]
[javax.xml/jaxws-api "2.0EA3"]
[clj-yaml "0.4.0"]
[potemkin "0.4.5"]
[org.clojure/data.json "0.2.6"]
[org.javasimon/javasimon-core "4.1.3"]
[org.clojure/core.async "1.3.610"]]
[org.clojure/core.async "1.3.610"]
[liberator "0.15.1"]
[compojure "1.6.0"]
[ring/ring-jetty-adapter "1.6.2"]
[ring/ring-core "1.6.2"]
[com.rpl/specter "1.1.3"]]
:jvm-opts ["-XX:+IgnoreUnrecognizedVMOptions" "--add-modules java.xml.bind"]
:profiles {:uberjar {:aot :all}}
:deploy-repositories [["releases"
{:sign-releases false :url "https://clojars.org/repo"}]
Expand Down
162 changes: 162 additions & 0 deletions src/engine/api.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
(ns engine.api
(:gen-class)
(:require [liberator.core :refer [resource defresource]]
[liberator.representation :refer [ring-response]]
[liberator.dev :refer [wrap-trace]]
[ring.middleware.params :refer [wrap-params]]
[ring.adapter.jetty :as jetty]
[compojure.handler :as handler]
[compojure.route :as route]
[compojure.core :refer [routes GET POST PUT DELETE]]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.walk :as walk]
[clojure.data.json :as json]
[clojure.edn :as edn]
[clojure.tools.logging :as log]
[engine.core :as eng]
engine.dynamic))

(def ^:dynamic *engine* nil)

(defn body-as-string
[ctx]
(if-let [body (get-in ctx [:request :body])]
(condp instance? body
java.lang.String body
(slurp (io/reader body)))))

(defn protect-qualified-keywords [exp]
(walk/postwalk #(if (and (keyword? %)
(not= (inc (count (name %)))
(count (str %))))
(subs (str %) 1)
%)
exp))

(defn body-as-data [ctx]
(protect-qualified-keywords
(json/read-str (body-as-string ctx) :key-fn keyword)))

(defn do-get-wmes [_]
(*engine* :wmes))

(defn do-get-wme-list [_]
(*engine* :wme-list))

(defn post-wmes [ctx]
(let [input (body-as-data ctx)]
(*engine* :add-wmes (map #(update % :type keyword) input))
{::retval "ok"}))

(defn put-wmes [ctx]
(let [input (body-as-data ctx)]
(*engine* :update-wmes (map #(update % :type keyword) input))
{::retval "ok"}))

(defn delete-wmes [ctx]
(let [input (body-as-data ctx)]
(*engine* :remove-wmes (map #(update % :type keyword) input))
{::retval "ok"}))

(defn batch-wmes [ctx]
(let [input (body-as-data ctx)]
(*engine* :batch-wme-ops (reduce-kv
(fn [m k v]
(assoc m k (map #(update % :type keyword) v)))
{}
input))
{::retval "ok"}))

(defresource get-wmes
:allowed-methods [:get]
:available-media-types ["application/json"]
:handle-ok do-get-wmes)

(defresource get-wme-list
:allowed-methods [:get]
:available-media-types ["application/json"]
:handle-ok do-get-wme-list)

(defresource add-new-wmes
:allowed-methods [:post]
:available-media-types ["application/json"]
:post! post-wmes
:handle-created (fn [_] {::retval "ok"}))

(defresource update-wmes
:allowed-methods [:put]
:available-media-types ["application/json"]
:put! put-wmes
:handle-ok (fn [_] {::retval "ok"}))

(defresource remove-wmes
:allowed-methods [:delete]
:available-media-types ["application/json"]
:delete! delete-wmes)

(defresource batch-wme-ops
:allowed-methods [:post]
:available-media-types ["application/json"]
:post! batch-wmes
:handle-created (fn [_] {::retval "ok"}))

(def api-routes
(routes
(GET "/wmes" [] get-wmes)
(GET "/wme-list" [] get-wme-list)
(POST "/wmes" [] add-new-wmes)
(PUT "/wmes" [] update-wmes)
(DELETE "/wmes" [] remove-wmes)
(POST "/wme-batch" [] batch-wme-ops)
(route/not-found (format (json/write-str {:message "Not found!"})))))

(defn wrap-fallback-exception [engine]
(fn [handler]
(fn [request]
(try
(binding [*engine* engine] (handler request))
(catch Exception e
(log/error e "unhandled exception")
;; since I don't like to ever return 500 ;^)
{:status 418 :body (.getMessage e)})))))

(defn handler [modules config-map]
(let [engine (apply eng/spawn modules)]
(engine :configure config-map)
[engine ((wrap-fallback-exception engine)
(-> api-routes handler/api wrap-params))]))

(defn- ensure-dynamic [modules]
(conj (remove #{:engine.dynamic} (map keyword (vec modules))) :engine.dynamic))

(defn run
([modules]
(run modules {}))
([modules config-map]
(let [engine (apply eng/spawn (ensure-dynamic modules))]
(engine :configure config-map)
engine)))

(defn start
([modules]
(start modules {}))
([modules config-map]
(println "Running jetty...")
(let [[eng handler-fun] (handler (ensure-dynamic modules) config-map)]
(jetty/run-jetty handler-fun {:port 3000 :join? false})
eng)))

(defn -main [& config-and-modules]
(let [[config modules] (reduce (fn [[c m] i]
(if (str/index-of i "=")
(let [[k v] (str/split i #"=")]
[(assoc c (keyword k) (eval (edn/read-string v))) m])
[c (conj m i)]))
[{} []]
config-and-modules)]
[config modules]
[config-and-modules config modules]
(start modules config)))


Loading