(ns wanijo.main
  (:require [ring.adapter.jetty :refer [run-jetty]]
            [wanijo.handler :as wanijo-handler])
  (:gen-class))

(defonce server (atom nil))

(defn start-server!
  [& {:keys [port join?]
      :or {port 3080
           join? true}}]
  (reset! server
          (run-jetty wanijo-handler/standalone-app
                     {:port port
                      :join? join?})))

(defn start-server-dev! []
  (start-server! :join? false
                 :port 8080
                 :handler wanijo-handler/dev-app))

(defn stop-server! []
  (when @server
    (.stop @server)))

(defn restart-server-dev! []
  (stop-server!)
  (start-server-dev!))

(defn -main [& args]
  (start-server! :port (Integer/valueOf (or (System/getenv "port")
                                            "3080"))
                 :join? false))
