Fork me on GitHub
#luminus
<
2016-08-13
>
gowder01:08:05

hey folks --- does anyone wanna explain to a web-stuff noob how to actually set headers in a luminus application to have a route give the browser headers appropriate for downloading a json file? I'm using a pretty much straight luminus + mongodb template app with whatever middleware comes in by default, and I've got a route defined as follows: (GET "/download-data" [] (db/dump-dockets)) where db/dump-dockets is just a call to monger's find-maps function to deliver the whole database as a vector of maps. And this works almost perfectly: it returns a well-formated json string containing the contents of the database as expected. Which is 90% of what I want. The 10% shortfall is that it displays it in the browser window instead of prompting the user to download as a file. My minimal knowledge of http stuff suggests to me that that means I need to set a header somewhere, but I'm terrified of messing with the middleware and breaking everything (it's taken a long time to get everything else working...)

jethroksy05:08:08

@gowder: I think you'll need to add the header Content-Disposition: attachment

jethroksy05:08:30

Your usual ring response would look like

jethroksy05:08:55

{:status 200
   :headers {"Content-Type" "application/json"}
   :body {:text "Hello World"}}

jethroksy05:08:11

so assuming you have it in a variable res

jethroksy05:08:57

(-> res
   (assoc [:headers "Content-Disposition"] "attachment"))

gowder16:08:45

thanks @jethroksy ! let's see if I can just slap that on top of a compojure route and have it work by magic 🙂

gowder17:08:42

well, because I really don't understand the magic going on behind the scenes between compojure and ring, so I just used clojure.data.json/write-str to handle matters myself, but seems to work:

(defn downloadable-json-string [m]
  {:status 200 :headers {"Content-Type" "application/json" "Content-Disposition" "attachment"} :body (write-str m)})


(defroutes home-routes
;; ...
  (GET "/data-to-file" [] (downloadable-json-string (db/dump-dockets)))

lizzie23:08:11

Has anyone tried to deploy a Luminus web app to Amazon Web Services (Elastic Beanstalk)? I'm stuck with migrations. The web app currently throws me an error about tables that do not exist. I've tried with following snippet in .ebextensions:

container_commands:
    01-migration:
        command: java -jar target/uberjar/myproject.jar migrate
        leader_only: true
but I know it's a shot in the dark. I have no idea about which command could work, and logs don't give any clues.