From d2c554dcc86040db45c5f98c68c8d904ae802c9c Mon Sep 17 00:00:00 2001 From: didier amyot Date: Mon, 25 Jun 2018 10:35:45 -0400 Subject: [PATCH] added proper configuration using aero --- project.clj | 1 + resources/config.edn | 12 ++++++++++++ src/book/cli/core.clj | 1 + src/book/{cli.clj => cli/exchange.clj} | 2 +- src/book/core.clj | 17 +++++++++++------ src/book/rest/core.clj | 11 +++++++++++ src/book/{web.clj => rest/exchange.clj} | 12 +++++------- src/book/rest/marketdata.clj | 1 + 8 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 resources/config.edn create mode 100644 src/book/cli/core.clj rename src/book/{cli.clj => cli/exchange.clj} (98%) create mode 100644 src/book/rest/core.clj rename src/book/{web.clj => rest/exchange.clj} (95%) create mode 100644 src/book/rest/marketdata.clj diff --git a/project.clj b/project.clj index 9f97f0a..e2bcf53 100644 --- a/project.clj +++ b/project.clj @@ -6,6 +6,7 @@ :dependencies [[org.clojure/clojure "1.9.0"] [org.clojure/core.async "0.4.474"] ;; web + [aero "1.1.3"] [http-kit "2.3.0"] [compojure "1.6.1"] [javax.servlet/javax.servlet-api "3.1.0"] diff --git a/resources/config.edn b/resources/config.edn new file mode 100644 index 0000000..1492e47 --- /dev/null +++ b/resources/config.edn @@ -0,0 +1,12 @@ +{ + :webserver ;; possible config from http-kit's run-server + {:port #long #or [#env PORT 8080] + :ip "0.0.0.0" + :max-body 524288 ;; 512kb, small but enough + :max-ws 524288} + + :securities ;; list of tradable securities + [{:sec-id :foo} + {:sec-id :bar} + {:sec-id :toto} + {:sec-id :tata}]} \ No newline at end of file diff --git a/src/book/cli/core.clj b/src/book/cli/core.clj new file mode 100644 index 0000000..c41bc55 --- /dev/null +++ b/src/book/cli/core.clj @@ -0,0 +1 @@ +(ns book.cli.core) diff --git a/src/book/cli.clj b/src/book/cli/exchange.clj similarity index 98% rename from src/book/cli.clj rename to src/book/cli/exchange.clj index 2e2ffd5..d63a404 100644 --- a/src/book/cli.clj +++ b/src/book/cli/exchange.clj @@ -1,4 +1,4 @@ -(ns book.cli +(ns book.cli.exchange (:require [book.match :refer [sell?]] [book.util :refer :all] [book.exchange :refer [empty-book create-exchange-for-security]] diff --git a/src/book/core.clj b/src/book/core.clj index 38830ed..cfe8e64 100644 --- a/src/book/core.clj +++ b/src/book/core.clj @@ -1,10 +1,15 @@ (ns book.core - (:require [book.cli] - [book.web]) + (:require [book.cli.core] + [book.rest.core] + [aero.core :refer (read-config)]) (:gen-class)) +(def apps {:webserver book.rest.core/-main}) + (defn -main [& argv] - (case (first argv) - "web" (apply book.web/-main (rest argv)) - "cli" (apply book.cli/-main (rest argv)) - (println "First argument must be web or cli"))) + (let [config (-> "config.edn" clojure.java.io/resource read-config) + target (-> argv first keyword) + target-fn (get apps target)] + (if target-fn + (apply target-fn [config]) + (println "target" target "not found")))) \ No newline at end of file diff --git a/src/book/rest/core.clj b/src/book/rest/core.clj new file mode 100644 index 0000000..10f3b4c --- /dev/null +++ b/src/book/rest/core.clj @@ -0,0 +1,11 @@ +(ns book.rest.core + (:require [org.httpkit.server :refer [run-server]] + [compojure.handler :refer [api]] + [book.rest.exchange :refer [create-exchanges-api-for + create-routes]])) + +(defn -main [config] + (let [webserver-config (:webserver config) + exchange-apis (create-exchanges-api-for (:securities config)) + api-routes (create-routes exchange-apis)] + (run-server (api api-routes) webserver-config))) \ No newline at end of file diff --git a/src/book/web.clj b/src/book/rest/exchange.clj similarity index 95% rename from src/book/web.clj rename to src/book/rest/exchange.clj index e518a1d..533f96f 100644 --- a/src/book/web.clj +++ b/src/book/rest/exchange.clj @@ -1,4 +1,4 @@ -(ns book.web +(ns book.rest.exchange (:require [book.exchange :refer [empty-book]] [org.httpkit.server :refer [run-server with-channel send! close on-close]] [compojure.route :refer [not-found]] @@ -39,6 +39,10 @@ :trades-publisher trades-publisher :new-book-order-publisher new-book-order-publisher})) +(defn create-exchanges-api-for [security-definitions] + (let [f (fn [sec] [(:sec-id sec) (create-exchange-api-for (:sec-id sec))])] + (into {} (map f security-definitions)))) + (defn get-new-order-channel [exchange-apis sec-id] (let [exchange (get exchange-apis sec-id) @@ -135,9 +139,3 @@ (GET "/securities/:sec-id/trades" [] (listen-to-trades exchange-apis)) (GET "/securities/:sec-id/orders" [] (listen-to-orders exchange-apis))) (not-found "not found"))) - - -(defn -main [& args] - (let [exchange-apis {:foo (create-exchange-api-for :foo) :bar (create-exchange-api-for :bar)} - api-routes (create-routes exchange-apis)] - (run-server (api api-routes) {:port 8080}))) \ No newline at end of file diff --git a/src/book/rest/marketdata.clj b/src/book/rest/marketdata.clj new file mode 100644 index 0000000..31c2803 --- /dev/null +++ b/src/book/rest/marketdata.clj @@ -0,0 +1 @@ +(ns book.rest.marketdata)