Fork me on GitHub
#luminus
<
2017-10-23
>
lummm03:10:52

Oops. Many days later, it was my first run in with :optimizations :advanced

lummm03:10:47

Specifically variable renaming, which broke my google auth2 interfacing

mmer08:10:20

Hi, I sent a message last night and it went missing - I am getting the following error:

mmer08:10:53

Looking online I can see several suggestions about increasing the version of various dependencies. However, I can't see any of them in my luminus project and also lein ancient does not show up any issues. Anyone got any ideas?

mmer08:10:27

This is caused when I tried to use the latest alpha version of clojure so I could access spec.

yogthos12:10:05

have you checked your version of leiningen?

Bravi12:10:55

how is clojure app usually deployed on web? I’m using DigitalOcean and I read this: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-clojure-web-application-on-freebsd-10-2 is it still the same procedure or perhaps it’s a bit simpler now?

yogthos21:10:59

digital ocean guide seems reasonable, there are also some examples on the Luminus site here http://www.luminusweb.net/docs/deployment.md

Brad20:10:19

In a project using Luminus I’m trying to output Json which has timestamp data. I’m getting this error.

Cannot JSON encode object of class: class org.joda.time.DateTime:
I’ve read the response page (http://www.luminusweb.net/docs/responses.md) and it appears that things should just work so I’m thinking I’m probably missing something. The wrap-formats appears to be in place and it looks like it should be if called convert the Joda instance to string. In my application I started by having my route return the result of a query and I set the content-type to application/json. This appears to work without a date/timestamp in the data but gives the error. To try and figure things out I’ve returned to the guestbook sample app and added a route as follows. This gives the same error. The guestbook app works fine and I’ve only added the following:
(defn test []
  (-> {:now (l/local-now)}
  (response/ok)
  (response/content-type "application/json")))

(defroutes home-routes
  (GET "/" request (home-page request))
  (POST "/" request (save-message! request))
  (GET "/about" [] (about-page))
  (GET "/test" [] (test)))
Can anyone give me a suggestion as to what to try next? Is my test function defined incorrectly? Is there something different that should be done over in handler.clj as far as wrapping the routes?

yogthos20:10:35

you shouldn’t have to add content type manually, that should be inferred from the accept header sent by the client

yogthos21:10:33

but you do need to wrap the timestamp in a data structure for it to serialize correctly, e.g: (GET "/time" [] (response/ok {:result (l/local-now)}))

Brad21:10:09

Thanks, Should this work? I’ve added your example to the guestbook app.

(GET "/time" [] (response/ok {:result (l/local-now)}))
Then call it as follows:
$ curl -i -H "Accept: application/json" 

yogthos23:10:34

yup that should work out of the box

yogthos23:10:16

actually I lied, you need to add a JSON encoder in the middleware as well, it's setup to use transit by default

(def restful-format-options
  (update
    muuntaja/default-options
    :formats
    merge
    {"application/transit+json"
     {:decoder [(partial transit-format/make-transit-decoder :json)]
      :encoder [#(transit-format/make-transit-encoder
                   :json
                   (merge
                     %
                     {:handlers {org.joda.time.DateTime joda-time-writer}}))]}}))

yogthos00:10:46

here's one way you could do this:

(cheshire.generate/add-encoder org.joda.time.DateTime
  (fn [c jsonGenerator]
    (.writeString jsonGenerator (-> ^ReadableInstant c .getMillis .toString))))

(def restful-format-options
  (update
    muuntaja/default-options
    :formats
    merge

    {"application/json"
     json-format/json-format

     "application/transit+json"
     {:decoder [(partial transit-format/make-transit-decoder :json)]
      :encoder [#(transit-format/make-transit-encoder
                   :json
                   (merge
                     %
                     {:handlers {org.joda.time.DateTime joda-time-writer}}))]}}))
`

Brad01:10:43

Ah. That works. Thanks very much!

Brad21:10:25

Here it gave me the error again.

zlrth22:10:30

crossposted from #ring