Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

we need more examples #28

Open
blairn opened this issue Aug 3, 2016 · 6 comments
Open

we need more examples #28

blairn opened this issue Aug 3, 2016 · 6 comments

Comments

@blairn
Copy link

blairn commented Aug 3, 2016

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?

@amellnik
Copy link

amellnik commented Jan 8, 2017

Is this possible with Mux at the moment? If not, it can't be used for any sort of real situation...

@amellnik
Copy link

amellnik commented Jan 8, 2017

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.

@shashi
Copy link
Collaborator

shashi commented Jan 8, 2017

it seems you can return a Dict with :status, :headers, and :body keys to do this.

@amellnik
Copy link

amellnik commented Jan 8, 2017

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

@blairn
Copy link
Author

blairn commented Jan 9, 2017

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

@amellnik
Copy link

amellnik commented Jan 9, 2017

Ugh, not sure how I managed to not find this while flailing around. Thanks!!!

amellnik pushed a commit to amellnik/Mux.jl that referenced this issue Apr 17, 2018
amellnik pushed a commit to amellnik/Mux.jl that referenced this issue Apr 17, 2018
@cmcaine cmcaine mentioned this issue Dec 20, 2020
18 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants