summaryrefslogtreecommitdiff
path: root/src/clj/nicktodo/core.clj
diff options
context:
space:
mode:
Diffstat (limited to 'src/clj/nicktodo/core.clj')
-rw-r--r--src/clj/nicktodo/core.clj59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/clj/nicktodo/core.clj b/src/clj/nicktodo/core.clj
new file mode 100644
index 0000000..a6e4b40
--- /dev/null
+++ b/src/clj/nicktodo/core.clj
@@ -0,0 +1,59 @@
+(ns nicktodo.core
+ (:require
+ [nicktodo.handler :as handler]
+ [nicktodo.nrepl :as nrepl]
+ [luminus.http-server :as http]
+ [nicktodo.config :refer [env]]
+ [clojure.tools.cli :refer [parse-opts]]
+ [clojure.tools.logging :as log]
+ [mount.core :as mount])
+ (:gen-class))
+
+;; log uncaught exceptions in threads
+(Thread/setDefaultUncaughtExceptionHandler
+ (reify Thread$UncaughtExceptionHandler
+ (uncaughtException [_ thread ex]
+ (log/error {:what :uncaught-exception
+ :exception ex
+ :where (str "Uncaught exception on" (.getName thread))}))))
+
+(def cli-options
+ [["-p" "--port PORT" "Port number"
+ :parse-fn #(Integer/parseInt %)]])
+
+(mount/defstate ^{:on-reload :noop} http-server
+ :start
+ (http/start
+ (-> env
+ (update :io-threads #(or % (* 2 (.availableProcessors (Runtime/getRuntime)))))
+ (assoc :handler (handler/app))
+ (update :port #(or (-> env :options :port) %))
+ (select-keys [:handler :host :port])))
+ :stop
+ (http/stop http-server))
+
+(mount/defstate ^{:on-reload :noop} repl-server
+ :start
+ (when (env :nrepl-port)
+ (nrepl/start {:bind (env :nrepl-bind)
+ :port (env :nrepl-port)}))
+ :stop
+ (when repl-server
+ (nrepl/stop repl-server)))
+
+
+(defn stop-app []
+ (doseq [component (:stopped (mount/stop))]
+ (log/info component "stopped"))
+ (shutdown-agents))
+
+(defn start-app [args]
+ (doseq [component (-> args
+ (parse-opts cli-options)
+ mount/start-with-args
+ :started)]
+ (log/info component "started"))
+ (.addShutdownHook (Runtime/getRuntime) (Thread. stop-app)))
+
+(defn -main [& args]
+ (start-app args))