(ns bitgloo-web.core (:gen-class) (:require [bitgloo-web.content :as content]) (:require [clojure.string :as str]) (:require [ring.adapter.jetty :refer [run-jetty]]) (:require [ring.middleware.resource :refer [wrap-resource]]) (:require [ring.middleware.content-type :refer [wrap-content-type]]) (:require [ring.middleware.not-modified :refer [wrap-not-modified]]) (:require [ring.util.response :as resp])) (def image-extensions [".jpg" ".bmp"]) (defn image? [path] (some (partial str/ends-with? path) image-extensions)) (defn home-redirect [uri] (if (= "/" uri) "/home" uri)) (defn request-handler [content-path request] (when (= :get (:request-method request)) (-> (:uri request) (home-redirect) ((partial str content-path)) (content/load-page) (resp/response) (resp/content-type "text/html") (resp/charset "utf8")))) (defn wrap-content-images [handler content-path] (fn [request] (let [uri (:uri request)] (if (and (= :get (:request-method request)) (image? uri)) (resp/file-response (str content-path uri)) (handler request))))) (defn wrap-log-request [handler] (fn [request] (-> request (select-keys [:headers :protocol :request-method :uri]) (update :headers get "x-forwarded-for") (vals) (conj "request:") ((partial apply println))) (handler request))) (defn -main [& args] (if (= 2 (count args)) (let [port (-> args first Integer/parseInt) content-path (second args)] (-> (partial request-handler content-path) (wrap-content-images content-path) (wrap-resource "public") (wrap-content-type) (wrap-not-modified) (wrap-log-request) (run-jetty {:port port}))) (println "usage: bitgloo port content-path")))