core.clj 812 Bytes
Newer Older
Johannes Knoedtel's avatar
Johannes Knoedtel committed
1
2
(ns gms.core
  (:require [gms.handler :refer [app init destroy parse-port]]
Johannes Knödtel's avatar
Johannes Knödtel committed
3
            [luminus.http-server :as http]
Johannes Knödtel's avatar
Johannes Knödtel committed
4
            [luminus-migrations.core :as migrations]
Johannes Knoedtel's avatar
Johannes Knoedtel committed
5
6
7
8
9
10
11
12
13
14
15
            [taoensso.timbre :as timbre]
            [environ.core :refer [env]])
  (:gen-class))

(defn http-port [port]
  (parse-port (or port (env :port) 3000)))

(defonce server (atom nil))

(defn start-server [port]
  (init)
Johannes Knödtel's avatar
Johannes Knödtel committed
16
  (reset! server (http/start {:handler app :port port})))
Johannes Knoedtel's avatar
Johannes Knoedtel committed
17
18
19
20

(defn stop-server []
  (when @server
    (destroy)
Johannes Knödtel's avatar
Johannes Knödtel committed
21
    (http/stop @server)
Johannes Knoedtel's avatar
Johannes Knoedtel committed
22
23
24
25
    (reset! server nil)))

(defn start-app [[port]]
  (.addShutdownHook (Runtime/getRuntime) (Thread. stop-server))
Johannes Knödtel's avatar
Johannes Knödtel committed
26
  (start-server (http-port port)))
Johannes Knoedtel's avatar
Johannes Knoedtel committed
27
28
29
30
31

(defn -main [& args]
  (cond
    (some #{"migrate" "rollback"} args) (migrations/migrate args)
    :else (start-app args)))