This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-12-28
Channels
- # admin-announcements (72)
- # aws (23)
- # beginners (43)
- # boot (140)
- # cider (11)
- # cljs-dev (4)
- # cljsrn (82)
- # clojars (2)
- # clojure (215)
- # clojure-nl (2)
- # clojure-russia (149)
- # clojurecup (4)
- # clojurescript (159)
- # cursive (19)
- # datomic (47)
- # editors (1)
- # emacs (27)
- # hoplon (32)
- # jobs (11)
- # ldnclj (3)
- # mount (33)
- # off-topic (1)
- # om (380)
- # onyx (1)
- # re-frame (2)
- # reagent (54)
- # yada (63)
@tolitius: does the state have to be in the same file as the handler? How does mount.core find start otherwise?
(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))
(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)))
(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)})
(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))
I have pushed the failing code to my github so that you can take a look at the whole thing if you wish
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
it does not matter where the defstate
lives, as long as it is under :source-paths
for a given task
mount.core/start does get called, but there is nothing to start (mount does not see sources at the point of :init
)
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
yea, I am learning it myself. does look like mount/start is called before the app.handler
is compiled
and add this task:
(deftask mount-start []
(println "started:" (mount/start))
identity)
then instead of :init
, just put this task in dev
, you'll see that mount/start is called before that println
I need to look what serve's :init
does to understand better how external (outside of :source-paths
) calls are handled.
since it's handled a bit differently from lein's way. this works: https://github.com/tolitius/mount/blob/lein-ring/project.clj#L34
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