Comments (6)
Hello. Do you remember why the response dicts were making it hard for you to do redirects?
In any case, Mux is kinda just a system for making your own middleware system in. You can easily replace the toresponse
middleware with something of your own or add your own middleware after toresponse
that will mutate the HTTP.Response
value that it generates.
my_defaults = stack(todict, basiccatch, splitquery, my_toresponse)
@app myapp = (
my_defaults,
page("/about", respond("hi")),
Mux.notfound())
from mux.jl.
I am still using Mux, but have adapted a lot to admit.
I think the main reason I was confused is that Mux execution is still rather intransparent for me. My first initial guess was that all the mux Middleware is everything you need to know. Hence directly returning https response objects didn't seem possible.
Turns out that there is hard coded special support for directly returning https.response objects. It makes everything look much more complicated than it need to be from my perspective.
So I guess having read more of mux source code by now, I would rather argue to get rid of all the special hard coded handling and do everything with Middleware. (It would also be great to massively improve the error message, but that is another issue)
from mux.jl.
Thanks for sharing!
I have a PR to make the error messages better here #157 which I guess I'll merge now cos it has been a few days.
I'm not sure what you mean by hardcoding? Could you explain with examples?
The intention is that Mux only interfaces with the HTTP.Request object in todict and the HTTP.Response object in toresponse and mk_response. And regular users aren't intended to use any of those unless the defaults don't work for them.
The rest of the built-in middleware is supposed to accept dicts in one format (with keys :uri, :path, etc) and return dicts in another (with keys :status, :body, :headers). Users are intended to add their own stuff to the dicts in middleware if they need to.
The main bit of coupling that I know of that is more publicly visible is that we do some type piracy on HTTP.Response (eek!) and the keyword args to Mux.serve() are specific to HTTP.jl
from mux.jl.
This is the main hardcoded part if I remember it correctly (mk_response
)
https://github.com/JuliaWeb/Mux.jl/blob/master/src/server.jl#L32-L50
# conversion functions for known http_handler return objects
mk_response(d) = d
function mk_response(d::Dict)
r = HTTP.Response(get(d, :status, 200))
haskey(d, :body) && (r.body = d[:body])
haskey(d, :headers) && (r.headers = d[:headers])
return r
end
function http_handler(app::App)
handler = (req) -> mk_response(app.warez(req))
# handler.events["error"] = (client, error) -> println(error)
# handler.events["listen"] = (port) -> println("Listening on $port...")
return handler
end
function ws_handler(app::App)
handler = (sock) -> mk_response(app.warez(sock))
return handler
end
My inner intuition expects this to be configurable, as part of the standard default middlewares. But it is not - it is not replaceable, configurable, but has a special extra place and will always be called (not sure about the overhead, hopefully julia can strip this off completely)
For me it would be so much more intuitive to have
ws_handler(app::App) = app.warez
http_handler(app::App) = app.warez
instead, bringing the transformation to HTTP.Response object as a simple middleware.
from mux.jl.
mk_response
will pass HTTP.Response
objects through unchanged (see its first method), so it won't stop you if you choose to replace the default toresponse
middleware with your own.
If the middleware toresponse
gets an HTTP.Response
object it will either throw an error or mangle the object because it will try to convert it to a string like this:
Line 44 in 1d04884
You can avoid that by providing your own toresponse
method (toresponse(res::HTTP.Response) = res
) or by writing your own middleware.
Knowing this, would you still like any changes to Mux? Maybe documenting what to do if you want to accept or return HTTP.Request/Response objects directly? Any code changes?
from mux.jl.
I know everything what you mentioned and have changed the toresponse method respectively for my case.
I feel a bit like repeating myself: The code change I would suggest is to get all transformations into middlewares so that really
ws_handler(app::App) = app.warez
http_handler(app::App) = app.warez
is the only thing left "hard-coded"
It makes the entire design of Mux much clearer and hence also easier to understand for newcomers.
from mux.jl.
Related Issues (20)
- TagBot trigger issue HOT 6
- Add me as a collaborator? HOT 3
- Road to v1 HOT 1
- Error on first request from Firefox HOT 9
- installing Mux v0.7.4 downgrades HTTP v0.9.2 HOT 2
- is Mux.jl an appropriate home for HTTP/Revise glue? HOT 7
- public interface for regression tests? HOT 4
- No :socket key in websocket server object HOT 3
- How to combine method & route ? HOT 1
- `serve` method to integrate websockets broken
- Rest api? HOT 1
- Is there WSS option in Mux v1.0.1? HOT 1
- Precompiling hangs on latest Julia nightly HOT 1
- Feature Request: let splitpath also handle the query intelligently
- Make `http_handler` part of `defaults` and `ws_handler` part of `wdefaults` HOT 1
- WebSockets example in README is slightly broken HOT 2
- Tests broken due to HTTP.jl
- Disable AppVeyor builds
- Tag a version HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mux.jl.