Fork me on GitHub
#mount
<
2015-12-28
>
genRaiy12:12:41

@tolitius: does the state have to be in the same file as the handler? How does mount.core find start otherwise?

genRaiy12:12:52

not working yet …. I have this

genRaiy12:12:56

(ns app.handler
    (:require [castra.middleware :as castra]
      [compojure.core :as c]
      [compojure.route :as route]
      [ring.middleware.defaults :as d]
      [ring.util.response :as response]
      [mount.core :refer [defstate]]
      [app.datomic-seed :refer [get-conn]]))

(c/defroutes app-routes
             (c/GET "/" req (response/content-type (response/resource-response "index.html") "text/html"))
             (route/resources "/" {:root ""}))

(def app
  (-> app-routes
      (castra/wrap-castra 'app.api)
      (castra/wrap-castra-session "a 16-byte secret")
      (d/wrap-defaults d/api-defaults)))

(defstate datomic-conn :start (get-conn))

genRaiy12:12:34

which is similar to your example

genRaiy12:12:00

boot dev configured like this:

genRaiy12:12:10

(deftask dev
  "Build castra-simple for local development."
  []
  (comp
    (serve
      :init 'mount.core/start
      :handler 'app.handler/app
      :reload true
      :port 8000)
    (watch)
    (speak)
    (hoplon)
    (reload)
    (cljs-repl)
    (cljs)))

genRaiy12:12:33

and finally I also want to use the connection in the app

genRaiy12:12:42

(ns app.api
    (:require
      [app.handler :refer [datomic-conn]]
      [app.datomic-query :refer [fetch-random-data]]
      [castra.core :refer [defrpc]]))

(defrpc get-state []
        {:random (fetch-random-data conn)})

genRaiy12:12:47

the code for get-conn is like this:

genRaiy12:12:54

(defn seed-db [conn]
      (print "Seeding DB... ")
      (create-schema conn)
      (doall (map #(insert-seed-data conn %) (range 256)))
      (println "done"))

(defn get-conn []
      (println "Fetching conn ... ")
      (if (d/create-database uri)
        (seed-db (d/connect uri)))
      (d/connect uri))

genRaiy12:12:15

but I see no messages and the connection is always nil

genRaiy12:12:57

I have pushed the failing code to my github so that you can take a look at the whole thing if you wish

tolitius14:12:02

ah.. did not pick up that you used boot, so did an example for you in lein-ring simple_smile

tolitius14:12:39

looks like serve calls :init before app.handler is compiled, or it might be compiled, but not visible (yet, since these are boot tmp dirs) to mount at the point of :init

tolitius14:12:20

it does not matter where the defstate lives, as long as it is under :source-paths for a given task

tolitius14:12:39

mount.core/start does get called, but there is nothing to start (mount does not see sources at the point of :init)

tolitius14:12:55

in order to make it work you can just create a function anywhere in sources:

(defn init [] (mount.core/start))
and call it from :init:
(serve
      :init 'app.handler/init

genRaiy14:12:22

ah ha - ok! thanks

genRaiy14:12:55

re boot … I’m trying to keep up with the cool cats simple_smile

tolitius14:12:03

yea, I am learning it myself. does look like mount/start is called before the app.handler is compiled

tolitius14:12:45

too see that you can put

(println "app.handler is compiled..")
into app.handler

tolitius14:12:03

and add this task:

(deftask mount-start []
  (println "started:" (mount/start))
  identity)

tolitius14:12:42

then instead of :init, just put this task in dev, you'll see that mount/start is called before that println

tolitius14:12:54

this is not to solve this, just to see it for yourself

tolitius14:12:56

I need to look what serve's :init does to understand better how external (outside of :source-paths) calls are handled.

tolitius15:12:29

since it's handled a bit differently from lein's way. this works: https://github.com/tolitius/mount/blob/lein-ring/project.clj#L34

tolitius17:12:50

let me know of init function solved it for you. I added a packaging section to the docs that explains it a bit more: https://github.com/tolitius/mount/blob/master/README.md#packaging

genRaiy18:12:54

adding the (defn init[]…) function did the trick.

genRaiy18:12:10

rock-n-roll simple_smile

genRaiy22:12:45

It's added into a pull request for the hoplon guys

genRaiy22:12:04

For the Datomic demo