rgrinberg / opium

Sinatra like web toolkit for OCaml

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add new middlewares for some common scenarios

anuragsoni opened this issue · comments

Right now Opium ships with a few middlewares, (router, static files handling, debugging)
We can improve the developer experience for users by providing some more middlewares out of the box. Some ideas for common use-cases that can be addressed are:

  1. Logging
    1.1 For opium specific logs, we should use the source defined for opium. This will allow end-users to set different reporting levels for their application.
    1.2 Allow users to add tags to logs - documentation
    1.3 Allow generating a unique id per request that can be used to identify a request and used for all logs that are linked to a specific request.
    1.4 Make it easy to add logs in a format that is easy to parse. It could be key value pairs, ex: user=foo, target=/hello/world..., or a json formatted entry, ex: {"user": "foo", "target": "/hello/world"}
  2. Request body parsing, handle both json and xml?
  3. Decoding query params

We can add these as a new package in the opium repository so its easy to ensure that they stay in sync when the codebase evolves. This issue can be used to track the list of middlewares that should be added as a first draft.

@shonfeder Forgot to tag you here before. Feel free to list more middlewares that you think can be useful

As we discussed, once #149 is merged, I will be focusing on implementing various middlewares in opium_kernel.

Here's a non-exhaustive list of the middlewares I had in mind:

  • Allow CORS
  • Deflater (gzip compression)
  • Add a content-length header
  • Basic and Bearer auth
  • Sessions
  • CSRF protection
  • File uploads
  • Parsers (JSON, Multipart, URL encoded)

I'm happy to add more to the list if you have use cases for other middleware 🙂

What comes to mind looking at https://github.com/django/django/tree/master/django/middleware

  • Caching
  • Security headers
  • Clickjacking

I think that would be a really neat set of middlewares 😃
At https://github.com/oxidizing/sihl we are working on Rock middlewares as well and would love to contribute.

There are now a number of built-in middlewares, including the ones described in the description of the issue. I propose that we (and users) open new issues for individual middlewares to request new ones.