Skip to content

Instantly share code, notes, and snippets.

@eproxus
Last active November 19, 2024 21:31
Show Gist options
  • Save eproxus/bd40f3a06e2c78dea539d2ba0471e36f to your computer and use it in GitHub Desktop.
Save eproxus/bd40f3a06e2c78dea539d2ba0471e36f to your computer and use it in GitHub Desktop.

Revisions

  1. eproxus revised this gist Nov 4, 2023. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions COWBOY_MIDDLEWARE.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,5 @@
    # How to Create a Cowboy Middleware

    | :warning: |
    |:-----------|
    |Middlewares are "deprecated" because stream handlers can do everything middlewares can do and more. Consider using a stream handler instead. |

    Add an entry to the `middlewares`. Usually you want to keep the standard Cowboy
    handlers afterwards, but you can replace them if you want.

  2. eproxus renamed this gist Jun 6, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. eproxus revised this gist Jun 2, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # How To Create a Cowboy Middleware
    # How to Create a Cowboy Middleware

    | :warning: |
    |:-----------|
  4. eproxus revised this gist Jun 2, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # How To Create a Cowboy Middleware

    | :warning: |
    |:-----------|
    |Middlewares are "deprecated" because stream handlers can do everything middlewares can do and more. Consider using a stream handler instead. |
    @@ -17,7 +19,7 @@ handlers afterwards, but you can replace them if you want.

    A middleware implements the behavior `cowboy_middleware`.

    # Caveats
    ## Caveats

    * You cannot change a response after it has been sent. For example, if you add a
    middleware after the generic `cowboy_handler` middleware. If a previous
  5. eproxus revised this gist Jun 2, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    | :warning: |
    |:---------:|
    |:-----------|
    |Middlewares are "deprecated" because stream handlers can do everything middlewares can do and more. Consider using a stream handler instead. |

    Add an entry to the `middlewares`. Usually you want to keep the standard Cowboy
  6. eproxus revised this gist Jun 2, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    | :warning: |
    | :warning: |
    |:---------:|
    | Middlewares are "deprecated" because stream handlers can do everything middlewares can do and more. Consider using a stream handler instead. |
    |Middlewares are "deprecated" because stream handlers can do everything middlewares can do and more. Consider using a stream handler instead. |

    Add an entry to the `middlewares`. Usually you want to keep the standard Cowboy
    handlers afterwards, but you can replace them if you want.
  7. eproxus revised this gist Jun 2, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    | :warning: |
    |:---------:|
    |foo |
    | Middlewares are "deprecated" because stream handlers can do everything middlewares can do and more. Consider using a stream handler instead. |

    Add an entry to the `middlewares`. Usually you want to keep the standard Cowboy
    handlers afterwards, but you can replace them if you want.
  8. eproxus created this gist Jun 2, 2021.
    26 changes: 26 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    | :warning: |
    |:---------:|
    |foo |

    Add an entry to the `middlewares`. Usually you want to keep the standard Cowboy
    handlers afterwards, but you can replace them if you want.

    ```erlang
    {ok, Pid} = cowboy:start_clear(ListenerName,
    [{port, Port}],
    #{
    env => #{dispatch => Dispatch},
    middlewares => [my_middleware, cowboy_router, cowboy_handler]
    }
    ),
    ```

    A middleware implements the behavior `cowboy_middleware`.

    # Caveats

    * You cannot change a response after it has been sent. For example, if you add a
    middleware after the generic `cowboy_handler` middleware. If a previous
    middleware has called the `cowboy_req:reply/4` function the response has
    already been sent on the wire. For that purpose you need to use a stream
    handler instead.
    9 changes: 9 additions & 0 deletions my_middleware.erl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    -module(my_middleware).

    -behaviour(cowboy_middleware).

    -export([execute/2]).

    execute(Req, Env) ->
    io:format("~p request~n", [cowboy_req:method(Req)]),
    {ok, Req2, Env}.