Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Pull a few pieces out of utils.clj

Functionality is now in ring-utils and version-utils.
  • Loading branch information...
commit 52ab45f7b0aeea5b3717509ca2b8e0bb703c601f 1 parent 6bb9ee9
@amalloy amalloy authored
View
4 src/foreclojure/core.clj
@@ -16,7 +16,7 @@
[foreclojure.version :only [version-routes]]
[foreclojure.graphs :only [graph-routes]]
[foreclojure.mongo :only [prepare-mongo]]
- [foreclojure.utils :only [wrap-uri-binding]]
+ [foreclojure.ring-utils :only [wrap-request-bindings]]
[foreclojure.periodic :only [schedule-task]]
[ring.adapter.jetty :only [run-jetty]]
[ring.middleware.reload :only [wrap-reload]]
@@ -47,7 +47,7 @@
#(wrap-reload % '(foreclojure.core))
identity))
session/wrap-stateful-session
- wrap-uri-binding
+ wrap-request-bindings
handler/site
wrap-strip-trailing-slash))
View
3  src/foreclojure/problems.clj
@@ -4,7 +4,8 @@
[clojure.string :as s]
[ring.util.response :as response])
(:import [org.apache.commons.mail EmailException])
- (:use [foreclojure.utils :only [from-mongo get-user get-solved login-link *url* flash-msg flash-error row-class approver? can-submit? send-email image-builder with-user as-int maybe-update escape-html]]
+ (:use [foreclojure.utils :only [from-mongo get-user get-solved login-link flash-msg flash-error row-class approver? can-submit? send-email image-builder with-user as-int maybe-update escape-html]]
+ [foreclojure.ring-utils :only [*url*]]
[foreclojure.template :only [def-page content-page]]
[foreclojure.social :only [tweet-link gist!]]
[foreclojure.feeds :only [create-feed]]
View
19 src/foreclojure/ring.clj
@@ -1,12 +1,13 @@
(ns foreclojure.ring
- (:require [clojure.java.io :as io]
- [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]]))
+ (:require [clojure.java.io :as io]
+ [clojure.string :as s]
+ [compojure.route :as route])
+ (:import [java.net URL])
+ (:use [compojure.core :only [GET]]
+ [foreclojure.version-utils :only [strip-version-number]]
+ [foreclojure.ring-utils :only [get-host]]
+ [useful.debug :only [?]]
+ [ring.util.response :only [response]]))
;; copied from compojure.route, modified to use File instead of Stream
(defn resources
@@ -45,7 +46,7 @@
(defn split-hosts [host-handlers]
(let [default (:default host-handlers)]
(fn [request]
- (let [host (get-in request [:headers "host"])
+ (let [host (get-host request)
handler (or (host-handlers host) default)]
(handler request)))))
View
20 src/foreclojure/ring_utils.clj
@@ -0,0 +1,20 @@
+(ns foreclojure.ring-utils
+ (:require [foreclojure.config :as config]))
+
+(def ^:dynamic *url* nil) ; url of current request
+(def ^:dynamic *host* nil) ; Host header sent by client
+(def ^:dynamic *http-scheme* nil) ; keyword, :http or :https
+
+(defn get-host [request]
+ (get-in request [:headers "host"]))
+
+(defn wrap-request-bindings [handler]
+ (fn [req]
+ (binding [*url* (:uri req)
+ *host* (or (get-host req) "www.4clojure.com")
+ *http-scheme* (:scheme req)]
+ (handler req))))
+
+(def static-url (if-let [host config/static-host]
+ #(str (name *http-scheme*) "://" host "/" %)
+ #(str "/" %)))
View
10 src/foreclojure/static.clj
@@ -1,9 +1,9 @@
(ns foreclojure.static
- (:use [compojure.core :only [defroutes GET]]
- [foreclojure.problems :only [solved-stats]]
- [foreclojure.config :only [repo-url]]
- [foreclojure.utils :only [static-url]]
- [foreclojure.template :only [def-page]]))
+ (:use [compojure.core :only [defroutes GET]]
+ [foreclojure.problems :only [solved-stats]]
+ [foreclojure.config :only [repo-url]]
+ [foreclojure.ring-utils :only [static-url]]
+ [foreclojure.template :only [def-page]]))
(def df
(let [df (java.text.DecimalFormat.)
View
12 src/foreclojure/template.clj
@@ -1,9 +1,11 @@
(ns foreclojure.template
- (:require [sandbar.stateful-session :as session])
- (: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? static-url]]))
+ (:require [sandbar.stateful-session :as session])
+ (:use [hiccup.core :only [html]]
+ [hiccup.page-helpers :only [doctype javascript-tag link-to]]
+ [foreclojure.config :only [config repo-url]]
+ [foreclojure.utils :only [page-attributes rendering-info login-url approver? can-submit?]]
+ [foreclojure.ring-utils :only [static-url]]
+ [foreclojure.version-utils :only [css js]]))
;; Global wrapping template
(defn html-doc [body]
View
31 src/foreclojure/utils.clj
@@ -14,15 +14,9 @@
[hiccup.form-helpers :only [label]]
[useful.fn :only [to-fix]]
[somnium.congomongo :only [fetch-one]]
+ [foreclojure.ring-utils :only [*url* static-url]]
[foreclojure.config :only [config repo-url]]))
-(def ^{:dynamic true} *url* nil)
-
-(defn wrap-uri-binding [handler]
- (fn [req]
- (binding [*url* (:uri req)]
- (handler req))))
-
(defn as-int [s]
(if (integer? s) s,
(try (Integer. s)
@@ -158,29 +152,6 @@
(>= (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)]
- (str path version-suffix "." ext)))
-
- (defn strip-version-number [file]
- (string/replace file version-suffix "")))
-
-(letfn [(wrap-versioning [f]
- (fn [& files]
- (for [file files]
- (f (static-url (add-version-number file))))))]
- (def js (wrap-versioning hiccup/include-js))
- (def css (wrap-versioning hiccup/include-css)))
-
-
-
(defn image-builder
"Return a function for constructing an [:img] element from a keyword.
View
21 src/foreclojure/version_utils.clj
@@ -0,0 +1,21 @@
+(ns foreclojure.version-utils
+ (:use [foreclojure.ring-utils :only [static-url]])
+ (:require [foreclojure.git :as git]
+ [foreclojure.config :as config]
+ [clojure.string :as string]
+ [hiccup.page-helpers :as hiccup]))
+
+(let [version-suffix (str "__" git/tag)]
+ (defn add-version-number [file]
+ (let [[_ path ext] (re-find #"(.*)\.(.*)$" file)]
+ (str path version-suffix "." ext)))
+
+ (defn strip-version-number [file]
+ (string/replace file version-suffix "")))
+
+(letfn [(wrap-versioning [f]
+ (fn [& files]
+ (for [file files]
+ (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.