sinasamavati/leptus

How to use leptus alongside with other cowboy features?

coquin opened this issue · 2 comments

As I see from source code of leptus it starts cowboy under the hood (src/leptus.erl):

get_listener_func(http) -> start_http;
get_listener_func(https) -> start_https;
get_listener_func(spdy) -> start_spdy.
...
ListenerFunc = get_listener_func(Listener),
...
Res = cowboy:ListenerFunc(Ref, NbAcceptors, ListenerOpts,
                              [{env, [{dispatch, Dispatch1}]}]),

Leptus is REST framework but I would like to use websockets and eventsource as well. Is there any way to write "classical" cowboy handlers for such cases? Or any way to customize cowboy start to, for example, supply onrequest and onresponse hooks for cowboy?

Leptus doesn't support writing classical handlers for those cases.
If you want to not miss any other features of cowboy, you should make the dispatch and use cowboy function(s) to start a listener.
To achieve this, you need to use leptus_router:paths/1 to gather routes (Paths) from your leptus handlers and then make the Dispatch using cowboy_router:compile(Paths) and then sort the Dispatch with leptus_router:sort_dispatch/1. After that, you can pass the sorted Dispatch to cowboy:start_http/4. Here is an example for the mentioned steps: https://github.com/s1n4/leptus/blob/master/src/leptus.erl#L91-L102

If you have any idea on how classical handlers for websockets should be, I'm interested to know and I appreciate it. Maybe we could provide a beautiful way for handling websockets in Leptus.

Closing.