janet-lang / spork

Various Janet utility modules - the official "Contrib" library.
MIT License
123 stars 35 forks source link

httpf example inside main? #200

Closed neezer closed 2 weeks ago

neezer commented 2 weeks ago

I may be a dum-dum, but why does examples/httpf-simple.janet cease to work if you start the server inside main?

Eg.

(import spork/httpf)

(defn home
  "The homepage"
  {:path "/"}
  [&]
  @[[:h1 "Hello, world!"]])

(defn main [&]
  (-> (httpf/server)
      httpf/add-bindings-as-routes
      httpf/listen))

The above gives me a 404 error when requesting /. I'm guessing it's because httpf/add-bindings-as-routes doesn't see the home function despite having the :path metadata, but I can't figure out why.

Any help would be appreciated. The existing example starts the HTTP server when running jpm build or judge, which is neither expected nor helpful, thus my attempts to wrap it inside main.

neezer commented 2 weeks ago

Ahh, it was an environment issue.

I need to call httpf/add-bindings-as-routes outside of main:

(import spork/httpf)

(def server (httpf/server))

(defn home
  "The homepage"
  {:path "/"}
  [&]
  @[[:h1 "Hello, world!"]])

(httpf/add-bindings-as-routes server)

(defn main [&]
  (httpf/listen server))