(ns ^{:doc "Functions to allow the wiki to be started and stopped from the REPL."
      :author "Simon Brooke"}
  smeagol.repl
  (:require 
   [ring.middleware.file :refer [wrap-file]]
   [ring.middleware.file-info :refer [wrap-file-info]]
   [ring.server.standalone :refer [serve]]
   [smeagol.handler :refer [app destroy init]])
    ;; (:import [javax.servlet AsyncContext]) 
  )

;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;;;
;; ;;;; Smeagol: a very simple Wiki engine.
;; ;;;;
;; ;;;; This program is free software; you can redistribute it and/or
;; ;;;; modify it under the terms of the GNU General Public License
;; ;;;; as published by the Free Software Foundation; either version 2
;; ;;;; of the License, or (at your option) any later version.
;; ;;;;
;; ;;;; This program is distributed in the hope that it will be useful,
;; ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; ;;;; GNU General Public License for more details.
;; ;;;;
;; ;;;; You should have received a copy of the GNU General Public License
;; ;;;; along with this program; if not, write to the Free Software
;; ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
;; ;;;; USA.
;; ;;;;
;; ;;;; Copyright (C) 2014 Simon Brooke
;; ;;;;
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defonce server (atom nil))

(defn get-handler []
  ;; #'app expands to (var app) so that when we reload our code,
  ;; the server is forced to re-resolve the symbol in the var
  ;; rather than having its own copy. When the root binding
  ;; changes, the server picks it up without having to restart.
  (-> #'app
      ; Makes static assets in $PROJECT_DIR/resources/public/ available.
      (wrap-file "resources")
      ; Content-Type, Content-Length, and Last Modified headers for files in body
      (wrap-file-info)))

(defn start-server
  "used for starting the server in development mode from REPL"
  [& [port]]
  (let [port (if port (Integer/parseInt (str port)) 3000)]
    (reset! server
            (serve (get-handler)
                   {:port port
                    :init init
                    :auto-reload? true
                    :destroy destroy
                    :join? false}))
    (println (str "You can view the site at http://localhost:" port))))

(defn stop-server []
  (.stop @server)
  (reset! server nil))

;; ;;(start-server 4444)

