Mux.jl icon indicating copy to clipboard operation
Mux.jl copied to clipboard

Scheme based routing (WIP)

Open sarvghotra opened this issue 8 years ago • 4 comments

There are 2 problems:

  1. When I try to test it on a localhost uri (from uri = req.uri) doesn't return the complete URI. It just returns :/ where as the address used is http://localhost:8000. As a result uri.scheme is empty. So I am not sure if scheme function works, don't know how to test it.

  2. Using Mux.defaults converts a req to a dict and this dict doesn't have anything like req[:uri]. So I have used req.uri which means Mux.defaults can't be used. A possible solution is to add req'[:uri] = req.uri here.

using Mux
using Base.Test
using Lazy
import Requests
@app test = (
  #Mux.defaults,
  #page("/dum", respond("<h1>Boo!</h1>")),
  scheme("http", respond("<h1>http</h1>")),
  scheme("https", respond("<h1>https</h1>")),
  #GET(respond("<h1>get!</h1>")),
  Mux.notfound())
serve(test)
@test Requests.text(Requests.get("http://localhost:8000/dum")) == "<h1>http</h1>"

It fails. Someone please give me some pointers. @MikeInnes

sarvghotra avatar Mar 08 '17 20:03 sarvghotra

Codecov Report

Merging #38 into master will decrease coverage by 2.76%. The diff coverage is 0%.

@@            Coverage Diff            @@
##           master     #38      +/-   ##
=========================================
- Coverage   54.86%   52.1%   -2.77%     
=========================================
  Files           7       7              
  Lines         113     119       +6     
=========================================
  Hits           62      62              
- Misses         51      57       +6
Impacted Files Coverage Δ
src/routing.jl 58.06% <0%> (-13.94%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update c83c057...a7f666a. Read the comment docs.

codecov-io avatar Mar 08 '17 23:03 codecov-io

It would be good to add scheme to the request dict as you've suggested, to allow routing on it. Unfortunately I have no idea how to get the HttpServer.jl's URI thing to behave properly – or to test whether it's working.

Perhaps you could test whether it works with HTTP.jl, and see whether it's feasible to move Mux.jl to use that.

MikeInnes avatar Mar 24 '17 18:03 MikeInnes

Looks like uri field of request is not set anywhere in RequestParser.jl. I have raised an issue here for the same.

Like you have suggested, we can try using HTTP.jl for setting up a server. @MikeInnes

sarvghotra avatar Apr 01 '17 00:04 sarvghotra

I did try using HTTP server instead of HttpServer but it (HTTP) doesn't work with the Request and Response types of HttpCommon. It only supports it own HTTP.Request and HTTP.Response. For eg: Sample1: Following code works fine.

using HTTP
function testapp(req::HTTP.Request)
  return HTTP.Response("yo") #respond("yo")
end

serverlog = HTTP.FIFOBuffer()
server = HTTP.Server((req, rep) -> testapp(req), serverlog)
tsk = @async HTTP.serve(server)
r = HTTP.get("http://127.0.0.1:8081/")
@test HTTP.status(r) == 200

Sample2: But following one raises error.

using HTTP, HttpCommon
function testapp(req::HttpCommon.Request)
  return HttpCommon.Response("yo") #respond("yo")
end

serverlog = HTTP.FIFOBuffer()
server = HTTP.Server((req, rep) -> testapp(req), serverlog)
tsk = @async HTTP.serve(server)
r = HTTP.get("http://127.0.0.1:8081/")
@test HTTP.status(r) == 200

So before I start working to replace HttpServer with HTTP its good to have expert's advice on this because this will require more than server.jl file to change (for eg. respond in routing.jl).

sarvghotra avatar Apr 01 '17 20:04 sarvghotra

Closing as this is very out of date. Users can now use req[:uri] and branch to define their own scheme helper. Something like this (untested):

scheme(sch, app) = branch(sch == req[:uri].scheme)

cmcaine avatar Sep 10 '23 17:09 cmcaine