@severed-infinity https://github.com/juxt/edge/blob/352d90b68855a75da8e6e77123b6bc0335aafe06/app/src/edge/web_server.clj#L35 edge has an example of how to do this
I noticed that your Safari exception included "/resources", which is wrong. You need to open /public/js/main.js
, not /resources/public/js/main.js
http://localhost:5000/public/js/main.js <-- this one
that work for accessing them through the URL, and reloading the page it finds some of the files. now the console produces ClojureScript could not load :main, did you forget to specify :asset-path?
["resources/public/" (assoc (new-classpath-resource "public") :id :yugioheffectbuilder.resources/static)]
worked but its not very intuitive. looking at it I couldn’t say what it does except for the fact I had to find out from someone else
That clojurescript error indicates you nees to set asset-path. If you're just learning this stuff, I recommend using edge which has clojurescript all setup for you.
Been using clojure for a few years now but I wouldn’t by no means call myself professional, tons I still need to learn. I don’t tend to touch Clojurescript all that much because I find its errors to be x10 more annoying than Clojure’s. This is just the first time I’ve used Yada for a project, I’ve used Bidi successfully but I didn’t use file assets for that project, it was for an API
At a keyboard now, so I can help further now.
(new-classpath-resource "public")
this creates a yada resource which serves a directory from the classpath. For your purposes, this means it serves the "public" folder inside of resources
.
The route defines what URL you want to serve from. So you could serve public under "/wazoo/"
if you wanted to. In this case you've chosen resources/public
, I think you may have chosen resources/
because of the folder structure on your filesytem, but it's not necessary here.
:asset-path
in your clojurescript build config defines where clojurescript should look for the main.out files, in your case, you have to hard code it to /resources/public/main.out
.
Changing "public/"
to “resource/public/` as the URL path fixed that :asset-path
issue and correctly loaded all of the files rather than just some of the files.
I think that is likely the default asset path then, which I've not seen in use before.
I was going by Yada manual (which hasn’t been updated since January 2017) that had a small part on serving directories that only had ["route" (new-classpath-resource "directory")]
and then proceeding to use the URL of the file path with no issues, at least by the documentation.
But I am happy that it works now
Now all I have to do is how to correctly start, shutdown, and reload a server as I usually get "CompilerException java.net.BindException: Address already in use"
`
@severed-infinity here's my aleph server start/stop stuff - https://gist.github.com/fee0055f7c492cf363e37a14170c2b22
the create-aleph-server
fn returns a [server stop-fn]
pair, so you can see how to stop the server
I’m using the yada/listener
function which creates an aleph server so I need to work around the listener return.
;; Copyright © 2014-2017, JUXT LTD.
(ns yada.aleph
(:require
[aleph.http :as http]
[clojure.tools.logging :refer :all]
[yada.handler :refer [as-handler]]))
(defn listener
"Start an HTTP listener on a given port. If not specified, listener
will be started on any available port. Returns {:port port :close fn}"
[routes & [aleph-options]]
(let [server
(http/start-server
(as-handler routes)
(merge aleph-options {:port (or (:port aleph-options) 0) :raw-stream? true}))]
{:port (aleph.netty/port server)
:close (fn [] (.close ^java.io.Closeable server))
:server server}))
listener
is returning you the close fn in it's result map - just call that when you want to close the server
That is what I am doing but as I mention in my last message it returns "CompilerException java.net.BindException: Address already in use"
so it would seem it is trying to start up another server
can you post the code ?
(your code that is)
yeah it’s working right now by changing it to a var than a function but here it is
(ns yugiohcardeffect.clj.core
(:require [yada.yada :as yada]
[yada.resources.classpath-resource :refer [new-classpath-resource]]
[<http://clojure.java.io|clojure.java.io> :refer [file resource]]))
(def resource-routes
(yada/listener
["/"
[["" (yada/handler (file "app.html"))]
["resources/public/" (assoc (new-classpath-resource "public") :id :yugioheffectbuilder.resources/static)]]]
{:port 5000}))
#_resource-routes
#_(println (resource-routes))
#_((:close resource-routes))
#_(def reload-routes
(do
((:close resource-routes))
resource-routes))
#_reload-routes
my first suspicion is that ((:close resource-routes))
is returning a Deferred
... hold on a min while i confirm that
returns #object[yada.aleph$listener$fn__22486 0x60dbe3e2 yada.aleph$listener$fn__22486@60dbe3e2]
for me
and ((:close resource-routes))
returns nil for me
oh... hold on... resource-routes is a def
not a defn
- what are you actually doing to reload ?
it was defn
until about 10 minutes ago. so prior to changing to def
:
(ns yugiohcardeffect.clj.core
(:require [yada.yada :as yada]
[yada.resources.classpath-resource :refer [new-classpath-resource]]
[<http://clojure.java.io|clojure.java.io> :refer [file resource]]))
(defn resource-routes []
(yada/listener
["/"
[["" (yada/handler (file "app.html"))]
["resources/public/" (assoc (new-classpath-resource "public") :id :yugioheffectbuilder.resources/static)]]]
{:port 5000}))
#_(resource-routes) <---- first
#_(println (resource-routes))
#_((:close (resource-routes))) <---- second, see if it closes
#_(def reload-routes
(do
((:close (resource-routes)))
(resource-routes)))
#_reload-routes <---- last, if close works, see if I can reload
but anyway, I’d load up the repl, send (resource-routes)
or resource-routes
to repl. Test ((:close (resource-routes)))
or ((:close resource-routes))` to see if it closes, if it does run the first again. Then lastly run reload-routes
when I need to update, if part 1 and 2 work. But I find using the defn
approach causes "CompilerException java.net.BindException: Address already in use"
.you need to store the result of (resource-routes)
and then get the close fn from that result... so e.g.
(defonce server (atom nil))
(defn start! [] (reset! server (resource-routes)))
(defn stop! [] (when (:close @server) ((:close @server))))
(defn restart! [] (stop!) (start!))
(there may be typos - i haven't tested that code)
Ah I would of assumed that resoure-routes would already be holding the result
if resource-routes
is a defn then it's not holding the result... if resource-routes
is a def then it will be holding the result... but you probably don't want to do it that way because recompiling will redefine the var
yeah what you just posted is pretty much what I was looking for