Last active
August 29, 2015 14:06
-
-
Save rarous/f204375f7901b68cbe9d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:dependencies [[org.clojure/clojure "1.7.0-alpha2"] | |
[http-kit "2.1.18"] | |
[enlive "1.1.5"] | |
[environ "0.5.0"] | |
[com.domkm/silk "0.0.1-SNAPSHOT" :exclusions [org.clojure/clojure]] | |
[prone "0.6.0"] | |
[ring "1.3.0"] | |
[ring/ring-defaults "0.1.0"] | |
[ring-basic-authentication "1.0.5"] | |
[ring.middleware.etag "1.0.0-SNAPSHOT"] | |
[bk/ring-gzip "0.1.1"]] | |
:main nefritovacesta.web | |
:profiles {:dev {:env {:production false}} | |
:production {:env {:production true}}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns foobar.web | |
(:require [org.httpkit.server :refer [run-server]] | |
[net.cgrand.enlive-html :refer [deftemplate]] | |
[prone.middleware :as prone] | |
[ring.middleware.defaults :refer [wrap-defaults site-defaults]] | |
[ring.middleware.etag.core :as etag] | |
[ring.middleware.gzip :refer [wrap-gzip]] | |
[ring.util.response :refer [charset] :as resp] | |
[environ.core :refer [env]] | |
[domkm.silk :as silk] | |
[domkm.silk.serve :as serve])) | |
(deftemplate index-template "index.html" []) | |
(deftemplate error-template "500.html" []) | |
(defn view [template] | |
(apply str (template))) | |
(defn render [template] | |
(-> {:status 200 | |
:headers {"Content-Type" "text/html"} | |
:body (view template)} | |
(charset "UTF-8"))) | |
(def handlers | |
{:index (fn [req] (render index-template))}) | |
(def routes | |
(silk/routes {:index [[]]})) | |
(defn- wrap-error-page [handler] | |
(fn [req] | |
(try | |
(handler req) | |
(catch Exception ex | |
{:status 500 | |
:headers {"Content-Type" "text/html"} | |
:body (view error-template)})))) | |
(def create-md5-etag (etag/create-hashed-etag-fn etag/md5)) | |
(def app | |
(-> (serve/ring-handler routes handlers) | |
((if (= env :production) | |
wrap-error-page | |
prone/wrap-exceptions)) | |
(etag/with-etag {:etag-generator create-md5-etag}) | |
(wrap-defaults site-defaults) | |
wrap-gzip)) | |
(defn -main [& [port]] | |
(let [port (Integer. (or port (env :port) 5000))] | |
(run-server app {:port port}) | |
(print (str "Web server running on port " port)))) | |
;; For interactive development: | |
;; (stop) | |
;; (def stop (-main)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment