001  (ns ^{:doc "In truth, boilerplate from Luminus."
002        :author "Simon Brooke"}
003    mw-ui.repl
004    (:require [mw-ui.handler :as handler]
005          [ring.server.standalone :refer [serve]]
006          [ring.middleware.file :refer [wrap-file]]
007          [ring.middleware.file-info :refer [wrap-file-info]])
008    (:gen-class))
009  
010  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
011  ;;;;
012  ;;;; mw-ui: a servlet user/visualisation interface for MicroWorld.
013  ;;;;
014  ;;;; This program is free software; you can redistribute it and/or
015  ;;;; modify it under the terms of the GNU General Public License
016  ;;;; as published by the Free Software Foundation; either version 2
017  ;;;; of the License, or (at your option) any later version.
018  ;;;;
019  ;;;; This program is distributed in the hope that it will be useful,
020  ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
021  ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
022  ;;;; GNU General Public License for more details.
023  ;;;;
024  ;;;; You should have received a copy of the GNU General Public License
025  ;;;; along with this program; if not, write to the Free Software
026  ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
027  ;;;; USA.
028  ;;;;
029  ;;;; Copyright (C) 2014 Simon Brooke
030  ;;;;
031  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
032  
033  
034  (defonce server (atom nil))
035  
036  (defn get-handler []
037    ;; #'app expands to (var app) so that when we reload our code,
038    ;; the server is forced to re-resolve the symbol in the var
039    ;; rather than having its own copy. When the root binding
040    ;; changes, the server picks it up without having to restart.
041    (-> #'handler/app
042        ; Makes static assets in $PROJECT_DIR/resources/public/ available.
043        (wrap-file "resources")
044        ; Content-Type, Content-Length, and Last Modified headers for files in body
045        (wrap-file-info)))
046  
047  (defn start-server
048    "used for starting the server in development mode from REPL"
049    [& [port]]
050    (let [port (if port (Integer/parseInt port) 3000)]
051      (reset! server
052              (serve (get-handler)
053                     {:port port
054                      :init handler/init
055                      :auto-reload? true
056                      :destroy handler/destroy
057                      :join? false}))
058      (println (str "You can view the site at http://localhost:" port))))
059  
060  (defn stop-server []
061    (.stop @server)
062    (reset! server nil))
063  
064  (defn -main []
065    (start-server))
066