Skip to content

Instantly share code, notes, and snippets.

@apeckham
Created Sep 13, 2018
Embed
What would you like to do?
(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

This comment has been minimized.

Copy link
Owner Author

@apeckham apeckham commented Sep 14, 2018

""""
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