Skip to content

Instantly share code, notes, and snippets.

@apeckham
Created September 13, 2018 23:11
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save apeckham/d70e08e4dcc54dfd1531b94f1fbc267e to your computer and use it in GitHub Desktop.
(ns hello-ring.core
(:require [ring.middleware.file :refer [wrap-file]]
[reitit.ring :as ring]
[ring.adapter.jetty :refer [run-jetty]])
(:gen-class))
(def handler
(ring/ring-handler
(ring/router
["/math" {:get {:handler (fn [request]
{:status 200
:headers {"Content-Type" "text/html"}
:body "Hello World"})}}]
{:data {:middleware [#_[wrap-file "."]]}}) ;;; doesn't work here
(ring/create-default-handler)))
(defn -main
[& args]
(run-jetty #_handler (wrap-file handler ".") {:port 3000})) ;;; works here
@apeckham
Copy link
Author

""""
ikitommi [12:11 AM]
@aaron51 reitit takes route-first approach, where the middleware (defined in router) are only applied if a route is matched. In your example there is only GET /math defined - if that doesn’t match, the wrap-file is not run. Options here are:

  1. use the middleware for the default routes, e.g. (wrap-file (ring/create-default-handler) ".")
  2. add a wildcard route ["/* (wrap-file (constantly nil) ".")] where the handler always misses (edited)

1 is much better, as the wrap-file is only run if none of the actual routes match.
I think that wrap-file always first checks if there is a file available. This mean doing slow IO for ALL calls.
""""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment