Serving data with Ring and Compojure

不打扰是莪最后的温柔 提交于 2020-01-15 11:55:27

问题


I am configuring and setting up a web application to serve the JSON data http://www.ericrochester.com/clj-data-analysis/data/census-race.json file statically.

My dependencies:

  :dependencies [[org.clojure/clojure "1.5.1"]
                 [org.clojure/clojurescript "0.0-2197"]
                 [ring/ring-core "1.1.7"]
                 [ring/ring-jetty-adapter "1.1.7"]
                 [compojure "1.1.3"]
                 [hiccup "1.0.2"]
                 [lein-cljsbuild "0.2.10"]]

As the title says I'm using Ring as a development plugin, i.e.

  :plugins [[lein-ring "0.8.3"]]

The Leiningen project is

   (ns test-app.core
   (:require [compojure.route :as route]
             [compojure.handler :as handler]
             [clojure.string :as str])
   (:use compojure.core
             ring.adapter.jetty
             [ring.middleware.content-type :only
             (wrap-content-type)]
             [ring.middleware.file :only (wrap-file)]
             [ring.middleware.file-info :only
             (wrap-file-info)]
             [ring.middleware.stacktrace :only
             (wrap-stacktrace)]
             [ring.util.response :only (redirect)]))

and in the project.clj

  :ring {:handler test-app.core/app}

which I am not sure if this will tell Ring where the web application is.

Moreover, I am not sure how to serve the JSON data file statically. I have read that "Ring serves static files out of the /resources directory of your project. In this case, create the directory /resources/data and put the data file that you downloaded from http://www.ericrochester.com/clj-data-analysis/data/census-race.json into it."

It is mostly this last part about creating the /resources/data directory where I am lost in the implementation. Could someone show how this part is done?

If I can learn to get past this I am looking to build routes and handlers, i.e.

  (defroutes site-routes
     (GET "/" [] (redirect "/data/census-race.json"))
     (route/resources "/")
     (route/not-found "Page not found"))

and

 (def app
    (-> (handler/site site-routes)
    (wrap-file "resources")
    (wrap-file-info)
    (wrap-content-type)))

回答1:


and in the project.clj

:ring {:handler test-app.core/app}

which I am not sure if this will tell Ring where the web application is.

When you run $ lein ring server-headless it will look for app in the test-app.core namespace. You should have something like the following in your core.clj file:

(def app
  (handler/site app-routes))

It is mostly this last part about creating the /resources/data directory where I am lost in the implementation. Could someone show how this part is done?

In the root of your project directory you should have a resources folder. Create a data folder inside the resources folder.

I'm not sure what problem you're having?




回答2:


If your project looks like this:

project.clj
src/test_app/core.clj
resources/public/data/census-race.json

Then the your site-routes handler will serve up that JSON file when you request the path /data/census-race.json.

You don't need any of the extra middleware like wrap-file, wrap-file-info, or wrap-content-type, since compojure.route/resources already does everything you need.



来源:https://stackoverflow.com/questions/22774386/serving-data-with-ring-and-compojure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!