we need more examples
I'm trying to build a mux based system, but we have no examples around setting headers, and how to put stuff in various parts of the chain.
I've tried...
# Access-Control-Allow-Origin: *
function withCors(app, req)
res = app(req)
res.headers["Access-Control-Allow-Origin"] = "*"
res
end
but, the app(req) is not a response object. and I have no way I can find of turning it into one.
The examples all seem to think that app(req) does give you a response object.
and if you want many of your pages to use the same middle layer AFTER the page call, how do you tell it to do that?
Is this possible with Mux at the moment? If not, it can't be used for any sort of real situation...
If I use HttpCommon I can construct a response object that includes the necessary headers with something like
resp = Response(200, getHeaders(), JSON.json(Dict("data" => 1, "error" => false)))
but I can't figure out to attach it to a route in Mux -- things like
@app test = (
Mux.defaults,
page(respond("Nothing to see here...")),
route("/test", req -> respond(Response(200, getHeaders(), "meh"))),
Mux.notfound()
)
serve(test, port=7777)
don't work.
it seems you can return a Dict with :status, :headers, and :body keys to do this.
It seems like it should work, but that returns a response with default headers and no body no matter what:
@app test = (
Mux.defaults,
page(respond("Nothing to see here...")),
route("/squareit", squareit),
route("/solveit", solveit),
page("/test", req -> Dict("body" => "meh")),
Mux.notfound()
)
returns the following for all three of the routes
Request URL:http://localhost:7777/test
Request Method:GET
Status Code:200 OK
Remote Address:127.0.0.1:7777
Response Headers
view source
Connection:keep-alive
Content-Language:en
Content-Length:0
Content-Type:text/html; charset=utf-8
Date:Sun, 08 Jan 2017 22:26:38
Server:Julia/0.5.0
Request Headers
view source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Host:localhost:7777
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537
It took me a day to work it out, but here it is so people don't have to follow....
# Access-Control-Allow-Origin: *
function withCorsHeader(res)
headers = HttpCommon.headers()
headers["Access-Control-Allow-Origin"] = "*"
Dict(
:headers => headers,
:body=> res
)
end
@app service = (
Mux.defaults,
page("/data", res -> withCorsHeader(getData()))
)
Ugh, not sure how I managed to not find this while flailing around. Thanks!!!