github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/docs/quick_start/add_plugins.md (about)

     1  # Add Plugins
     2  
     3  Any API Definition can make use of plugins. A plugin is a way to add functionalities to your definition, like rate limit, CORS, authentication, etc...
     4  
     5  In this tutorial we will add a [rate limit](/docs/plugins/rate_limit.md) plugin to our definition.
     6  
     7  ## 1. Add the plugin
     8  
     9  Let's create a file called `rate_limit.json` with our new additions to the API definition. We will create a rate limit rule that we can only send `5 req/m`
    10   just for us to play around with it.
    11  
    12  ```json
    13  {
    14      "name" : "my-endpoint",
    15      "active" : true,
    16      "proxy" : {
    17          "listen_path" : "/example/*",
    18          "upstreams" : {
    19              "balancing": "roundrobin",
    20              "targets": [
    21                  {"target": "http://www.mocky.io/v2/595625d22900008702cd71e8"}
    22              ]
    23          },
    24          "methods" : ["GET"]
    25      },
    26  	"plugins": [
    27          {
    28              "name": "rate_limit",
    29              "enabled": true,
    30              "config": {
    31                  "limit": "5-M",
    32                  "policy": "local"
    33              }
    34          }
    35  	]
    36  }
    37  ```
    38  
    39  Now lets update our API definition:
    40  
    41  {% codetabs name="HTTPie", type="bash" -%}
    42  http -v PUT localhost:8081/apis/my-endpoint "Authorization:Bearer yourToken" "Content-Type: application/json" < rate_limit.json
    43  {%- language name="CURL", type="bash" -%}
    44  curl -X "PUT" localhost:8081/apis/my-endpoint -H "Authorization:Bearer yourToken" -H "Content-Type: application/json" -d @rate_limit.json
    45  {%- endcodetabs %}
    46  
    47  Done! Now Janus already reloaded the configuration and the rate limit is enabled on your endpoint.
    48  
    49  ## 2. Check if the plugin is working
    50  
    51  Lets make a few requests to our endpoint and see if the rate limit is working.
    52  
    53  {% codetabs name="HTTPie", type="bash" -%}
    54  http -v GET http://localhost:8080/example
    55  {%- language name="CURL", type="bash" -%}
    56  curl -X "GET" http://localhost:8080/example
    57  {%- endcodetabs %}
    58  
    59  You should see a few extra headers on the response of this request:
    60  
    61  ```
    62  X-Ratelimit-Limit →5
    63  X-Ratelimit-Remaining →4
    64  X-Ratelimit-Reset →1498773715
    65  ```
    66  
    67  This means the plugin is working properly. Now lets make the same request 4 more times... On the fifth time you should get:
    68  
    69  ```
    70  Status Code: 429 Too Many Requests
    71  
    72  Limit exceeded
    73  ```
    74  
    75  After 1 minute you should be able to make 5 more requests :)
    76  
    77  In the [next part](add_auth.md) we'll learn how to protect our endpoint.