Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 108 lines (98 sloc) 4.406 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
(ns foreclojure.core
  (:require [compojure.route :as route]
            [compojure.handler :as handler]
            [foreclojure.config :as config]
            [sandbar.stateful-session :as session])
  (:use [compojure.core :only [defroutes routes GET]]
            [foreclojure.static :only [static-routes welcome-page]]
            [foreclojure.api :only [api-routes]]
            [foreclojure.datatable :only [datatable-routes]]
            [foreclojure.problems :only [problems-routes]]
            [foreclojure.login :only [login-routes]]
            [foreclojure.settings :only [settings-routes]]
            [foreclojure.register :only [register-routes]]
            [foreclojure.golf :only [golf-routes]]
            [foreclojure.ring :only [resources wrap-strip-trailing-slash wrap-url-as-file wrap-versioned-expiry split-hosts wrap-404 wrap-debug]]
            [foreclojure.users :only [users-routes]]
            [foreclojure.config :only [config]]
            [foreclojure.social :only [social-routes]]
            [foreclojure.version :only [version-routes]]
            [foreclojure.graphs :only [graph-routes]]
            [foreclojure.mongo :only [prepare-mongo]]
            [foreclojure.ring-utils :only [wrap-request-bindings]]
            [foreclojure.periodic :only [schedule-task]]
            [ring.adapter.jetty :only [run-jetty]]
            [ring.middleware.reload :only [wrap-reload]]
            [ring.middleware.stacktrace :only [wrap-stacktrace]]
            [ring.middleware.file-info :only [wrap-file-info]]
            [ring.middleware.gzip :only [wrap-gzip]]))

(def *block-server* false)

(defroutes resource-routes
  (-> (resources "/*")
      (wrap-url-as-file)
      (wrap-file-info)
      (wrap-versioned-expiry)))

(def dynamic-routes
  (-> (routes (GET "/" [] (welcome-page))
              login-routes
              register-routes
              problems-routes
              users-routes
              static-routes
              social-routes
              version-routes
              graph-routes
              api-routes
              datatable-routes
              golf-routes
              settings-routes)
      ((if (:wrap-reload config)
         #(wrap-reload % '(foreclojure.core))
         identity))
      session/wrap-stateful-session
      wrap-request-bindings
      handler/site
      wrap-strip-trailing-slash))

(let [canonical-host (or config/dynamic-host "www.4clojure.com")]
  (defn redirect-routes [request]
    (let [{:keys [scheme uri]} request
          proper-uri (str (name scheme)
                          "://"
                          canonical-host
                          uri)]
      {:status 302
       :headers {"Location" proper-uri}
       :body (str "<a href='" proper-uri "'>"
                  proper-uri
                  "</a>")})))

(def host-handlers (reduce into
                           {:default (routes dynamic-routes resource-routes)}
                           [(for [host config/redirect-hosts]
                              [host redirect-routes])
                            (for [[host route] [[config/static-host resource-routes]
                                                [config/dynamic-host dynamic-routes]]
                                  :when host]
                              [host route])]))

(def app (-> (split-hosts host-handlers)
             wrap-404
             wrap-gzip))

(defn register-heartbeat []
  (when-let [period (:heartbeat config)]
    (apply schedule-task
           (let [^java.io.PrintWriter out *out*
                 ^Runtime r (Runtime/getRuntime)]
             (fn []
               (.println out (format "%d/%d/%d MB free/total/max"
                                     (int (/ (. r (freeMemory)) 1e6))
                                     (int (/ (. r (totalMemory)) 1e6))
                                     (int (/ (. r (maxMemory)) 1e6))))))
           period)))

(let [default-jetty-port 8080]
  (defn run []
    (prepare-mongo)
    (register-heartbeat)
    (run-jetty (var app) {:join? *block-server*
                          :port (get config :jetty-port default-jetty-port)})))

(defn -main [& args]
  (binding [*block-server* true]
    (run)))
Something went wrong with that request. Please try again.