Fork me on GitHub
#yada
<
2018-03-21
>
dominicm09:03:02

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

severed-infinity15:03:25

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?

severed-infinity16:03:00

["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

dominicm18:03:47

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.

severed-infinity22:03:05

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

dominicm22:03:54

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.

severed-infinity22:03:34

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.

dominicm22:03:27

I think that is likely the default asset path then, which I've not seen in use before.

severed-infinity22:03:27

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.

severed-infinity22:03:37

But I am happy that it works now

severed-infinity22:03:47

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"`

mccraigmccraig23:03:30

the create-aleph-server fn returns a [server stop-fn] pair, so you can see how to stop the server

severed-infinity23:03:31

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}))

mccraigmccraig23:03:46

listener is returning you the close fn in it's result map - just call that when you want to close the server

severed-infinity23:03:07

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

mccraigmccraig23:03:47

can you post the code ?

mccraigmccraig23:03:16

(your code that is)

severed-infinity23:03:43

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]]
            [ :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

mccraigmccraig23:03:40

my first suspicion is that ((:close resource-routes)) is returning a Deferred... hold on a min while i confirm that

severed-infinity23:03:21

returns #object[yada.aleph$listener$fn__22486 0x60dbe3e2 yada.aleph$listener$fn__22486@60dbe3e2] for me

severed-infinity23:03:17

and ((:close resource-routes)) returns nil for me

mccraigmccraig23:03:52

oh... hold on... resource-routes is a def not a defn - what are you actually doing to reload ?

severed-infinity23:03:08

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]]
            [ :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".

mccraigmccraig23:03:48

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!))

mccraigmccraig23:03:10

(there may be typos - i haven't tested that code)

severed-infinity23:03:53

Ah I would of assumed that resoure-routes would already be holding the result

mccraigmccraig23:03:52

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

severed-infinity23:03:39

yeah what you just posted is pretty much what I was looking for