Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow different behavior per hostname

  • Loading branch information...
commit 4ef7b4e810cacea2d852984cae3ad48a4f6b46bf 1 parent 5b86579
Alan Malloy amalloy authored
3  config.clj
View
@@ -9,6 +9,9 @@
:advanced-user-count 50
:pass ""
:repo-url "https://github.com/4clojure/4clojure"
+ :hosts {;; :static "static.4clojure.com"
+ ;; :dynamic "www.4clojure.com"
+ :redirects ["4clojure.com"]}
:golfing-active true
:heartbeat nil ; set to, eg, [1 :hour] for periodic heap information on stdout
;; this list is just for bootstrapping - the real DB is authoritative
6 src/foreclojure/config.clj
View
@@ -9,3 +9,9 @@
;; Defs both for convenience and compile-time verification of simple settings
(def repo-url (or (:repo-url config)
(throw (Exception. "config.clj needs a :repo-url key"))))
+
+(letfn [(host [key]
+ (get-in config [:hosts key]))]
+ (def static-host (host :static))
+ (def dynamic-host (host :dynamic))
+ (def redirect-hosts (host :redirects)))
69 src/foreclojure/core.clj
View
@@ -1,14 +1,15 @@
(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 GET]]
+ (:use [compojure.core :only [defroutes routes GET]]
[foreclojure.static :only [static-routes welcome-page]]
[foreclojure.problems :only [problems-routes]]
[foreclojure.login :only [login-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]]
+ [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]]
@@ -25,31 +26,55 @@
(def *block-server* false)
-(defroutes main-routes
- (GET "/" [] (welcome-page))
- login-routes
- register-routes
- problems-routes
- users-routes
- static-routes
- social-routes
- version-routes
- graph-routes
- golf-routes
+(defroutes resource-routes
(-> (resources "/*")
(wrap-url-as-file)
(wrap-file-info)
- (wrap-versioned-expiry))
- (route/not-found "Page not found"))
+ (wrap-versioned-expiry)))
-(def app (-> #'main-routes
- ((if (:wrap-reload config)
- #(wrap-reload % '(foreclojure.core))
- identity))
- session/wrap-stateful-session
+(def dynamic-routes
+ (-> (routes (GET "/" [] (welcome-page))
+ login-routes
+ register-routes
+ problems-routes
+ users-routes
+ static-routes
+ social-routes
+ version-routes
+ graph-routes
+ golf-routes)
+ ((if (:wrap-reload config)
+ #(wrap-reload % '(foreclojure.core))
+ identity))
+ session/wrap-stateful-session
+ wrap-uri-binding
+ 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 (wrap-debug route)])]))
+
+(def app (-> (split-hosts host-handlers)
+ wrap-404
handler/site
- wrap-uri-binding
- wrap-strip-trailing-slash
wrap-gzip))
(defn register-heartbeat []
20 src/foreclojure/ring.clj
View
@@ -1,9 +1,11 @@
(ns foreclojure.ring
(:require [clojure.java.io :as io]
- [clojure.string :as s])
+ [clojure.string :as s]
+ [compojure.route :as route])
(:import [java.net URL])
(:use [compojure.core :only [GET]]
[foreclojure.utils :only [strip-version-number]]
+ [useful.debug :only [?]]
[ring.util.response :only [response]]))
;; copied from compojure.route, modified to use File instead of Stream
@@ -36,3 +38,19 @@
(handler)
(assoc-in [:headers "Cache-control"]
"public, max-age=31536000"))))
+
+(defn wrap-debug [handler]
+ (fn [request]
+ (? (handler (? request)))))
+
+(defn split-hosts [host-handlers]
+ (let [default (:default host-handlers)]
+ (fn [request]
+ (let [host (get-in request [:headers "host"])
+ handler (or (host-handlers host) default)]
+ (handler request)))))
+
+(defn wrap-404 [handler]
+ (fn [request]
+ (or (handler request)
+ (route/not-found "Page not found"))))
19 src/foreclojure/template.clj
View
@@ -3,7 +3,7 @@
(:use [hiccup.core :only [html]]
[hiccup.page-helpers :only [doctype javascript-tag link-to]]
[foreclojure.config :only [config repo-url]]
- [foreclojure.utils :only [css js page-attributes rendering-info login-url approver? can-submit?]]))
+ [foreclojure.utils :only [css js page-attributes rendering-info login-url approver? can-submit? static-url]]))
;; Global wrapping template
(defn html-doc [body]
@@ -14,22 +14,23 @@
[:html
[:head
[:title (:title attrs)]
- [:link {:rel "alternate" :type "application/atom+xml" :title "Atom" :href "http://4clojure.com/problems/rss"}]
- [:link {:rel "shortcut icon" :href "/favicon2.ico"}]
+ [:link {:rel "alternate" :type "application/atom+xml" :title "Atom" :href "/problems/rss"}]
+ [:link {:rel "shortcut icon" :href (static-url "favicon2.ico")}]
[:style {:type "text/css"}
".syntaxhighlighter { overflow-y: hidden !important; }"]
- (css "/css/style.css" "/css/demo_table.css" "/css/shCore.css" "/css/shThemeDefault.css")
- (js "/vendor/script/jquery-1.5.2.min.js" "/vendor/script/jquery.dataTables.min.js")
- (js "/script/foreclojure.js")
- (js "/vendor/script/xregexp.js" "/vendor/script/shCore.js" "/vendor/script/shBrushClojure.js")
- (js "/vendor/script/ace/ace.js" "/vendor/script/ace/mode-clojure.js")
+ (css "css/style.css" "css/demo_table.css" "css/shCore.css" "css/shThemeDefault.css")
+ (js "vendor/script/jquery-1.5.2.min.js" "vendor/script/jquery.dataTables.min.js")
+ (js "script/foreclojure.js")
+ (js "vendor/script/xregexp.js" "vendor/script/shCore.js" "vendor/script/shBrushClojure.js")
+ (js "vendor/script/ace/ace.js" "vendor/script/ace/mode-clojure.js")
[:script {:type "text/javascript"} "SyntaxHighlighter.all()"]]
[:body
(when (:fork-banner attrs)
[:div#github-banner [:a {:href repo-url
:alt "Fork 4Clojure on Github!"}]])
[:div#top
- (link-to "/" [:img#logo {:src "/images/4clj-logo.png" :alt "4clojure.com"}])]
+ (link-to "/" [:img#logo {:src (static-url "images/4clj-logo.png")
+ :alt "4clojure.com"}])]
[:div#content
[:div#menu
(for [[link text & [tabbed]]
9 src/foreclojure/utils.clj
View
@@ -1,6 +1,7 @@
(ns foreclojure.utils
(:require [sandbar.stateful-session :as session]
[ring.util.response :as response]
+ [foreclojure.config :as config]
[clojure.walk :as walk]
[clojure.string :as string]
[foreclojure.git :as git]
@@ -167,6 +168,12 @@
(>= (count (get-solved username))
(:advanced-user-count config)))))
+(let [prefix (str (when-let [host config/static-host]
+ (str "http://" host))
+ "/")]
+ (defn static-url [url]
+ (str prefix url)))
+
(let [version-suffix (str "__" git/tag)]
(defn add-version-number [file]
(let [[_ path ext] (re-find #"(.*)\.(.*)$" file)]
@@ -178,7 +185,7 @@
(letfn [(wrap-versioning [f]
(fn [& files]
(for [file files]
- (f (add-version-number file)))))]
+ (f (static-url (add-version-number file))))))]
(def js (wrap-versioning hiccup/include-js))
(def css (wrap-versioning hiccup/include-css)))
Please sign in to comment.
Something went wrong with that request. Please try again.