Mux.jl
Mux.jl copied to clipboard
Scheme based routing (WIP)
There are 2 problems:
-
When I try to test it on a localhost
uri(fromuri = req.uri) doesn't return the complete URI. It just returns:/where as the address used is http://localhost:8000. As a resulturi.schemeis empty. So I am not sure ifschemefunction works, don't know how to test it. -
Using
Mux.defaultsconverts areqto a dict and this dict doesn't have anything likereq[:uri]. So I have usedreq.uriwhich meansMux.defaultscan't be used. A possible solution is to addreq'[:uri] = req.urihere.
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
Codecov Report
Merging #38 into master will decrease coverage by
2.76%. The diff coverage is0%.
@@ 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 dataPowered by Codecov. Last update c83c057...a7f666a. Read the comment docs.
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.
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
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).
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)