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

How to combine method & route ?

Open whatsthecraic opened this issue 3 years ago • 1 comments

e.g.

mux(
method("GET", route("path/to/this", handle_1)),
method("POST", route("path/to/that", handle_2)),
notfound(),
)

whatsthecraic avatar Nov 18 '21 17:11 whatsthecraic

Each branch needs to terminate somewhere with a middleware that expects a single argument (like notfound() or respond("something")). route and method are both branching middlewares: they expect not to be last in the tree of functions to call.

Try:

julia> using Mux

julia> using Mux: notfound

julia> @app app = (
         Mux.defaults,
         method("GET",
           route("path/to/this", respond("hi")),
           notfound()
         ),
         method("POST",
           route("path/to/that", respond("bye!")),
           notfound()
         ),
         notfound(),
       )

julia> serve(app)
Task (runnable) @0x00007fbbcbe931f0

shell> curl http://localhost:8000/path/to/this
hi
shell> curl -X POST http://localhost:8000/path/to/this
Not found
shell> curl -X POST http://localhost:8000/path/to/that
bye!

cmcaine avatar Nov 26 '21 05:11 cmcaine