Search code examples
gradleclojurering

path to html page using ring and gradle


My folder structure looks like this

src
    main
        clojure
               .../core.clj
        webapp
              WEB-INF
                     index.html

In my core.clj I have a main function for jetty

(ns com.lapots.platform.web.core
    (:use ring.adapter.jetty)
    (:use com.lapots.platform.web.router.core)
    (:require [ring.middleware.reload :refer [wrap-reload]])
    (:import [org.eclipse.jetty.server.handler StatisticsHandler])
    (:gen-class))

(def a-minute 60000)

(defn conf
    [server]
    (let [stats-handler (StatisticsHandler.)
          default-handler (.getHandler server)]
        (.setHandler stats-handler default-handler)
        (.setHandler server stats-handler)
        (.setStopTimeout server a-minute)
        (.setStopAtShutdown server true)))

(def app
    (-> routes
        wrap-reload))

(defn -main [& args]
    (run-jetty app {:port 3000 :configurator conf :join? false}))

router/core.clj has this code

(ns com.lapots.platform.web.router.core
    (:require [compojure.core :refer [defroutes GET ANY]]
              [liberator.core :refer [defresource resource]]
              [ring.util.response :as resp]))

(defresource rest-handler
             :handle-ok "rest response"
             :etag "fixed-etag"
             :available-media-types ["text/html"])

(defn wrapped-file-response [request]
    (println "Attempt to read index.html")
    (resp/resource-response "index.html" {:root "resources"}))

(defroutes routes
           (GET "/" request rest-handler)
           (GET "/home" request wrapped-file-response))

I start jetty server as a typical gradle task

task startServer(dependsOn: classes, type: JavaExec) {
    main = 'com.lapots.platform.web.core'
    classpath = sourceSets.main.runtimeClasspath
}

But it is unable to resolve index.html file. (/ returns correct rest response message).

How to specify path to html page for routes?


Solution

  • You didn't show your build.gradle so I am unable to determine how your classpath is configured. If you are using defaults then the issue is that your index.html file is not available on classpath as it should be under ${rootDir}/src/main/resources (which is a default location for resource/non-code files that should be included on classpath) and in resources subdirectory as your ring.util.response/resource-response :root is configured to resource.

    Thus your index.html should be located under following path: ${rootDir}/src/main/resources/resources/index.html.