solara icon indicating copy to clipboard operation
solara copied to clipboard

Solara middleware support example request

Open JovanVeljanoski opened this issue 7 months ago • 2 comments

Hello,

Would it be possible to get (at least) a very minimal example of adding some custom middleware on a basic solara server (just the default starlette thing we get via solara run my_app.py.

I would really like (need) to add some custom logging, measure timing, rate limits etc..

It would be extra nice if I could log per route or so.

A very minimal example would be highly appreciated, if possible.

Thank you! Jovan.

JovanVeljanoski avatar Apr 20 '25 12:04 JovanVeljanoski

Hi Jovan,

Do you want to log specific actions of users? Beause a starlette middleware will not help (since we only have the initial websocket request). If you want to log specific page visits (e.g. using mixpanel), you could use the top layout, would you like an example for that?

cheers,

Maarten

maartenbreddels avatar Apr 29 '25 09:04 maartenbreddels

Hi,

I had 2 goals with this question:

  1. Just to learn how to add middleware for whatever purpose (and if that was possible/supported in solara)
  2. Actually just to log the initial websocket request (i realized this once I figured how to do this - but good enough for me).

So I realized you can get access to the underlying starlette app via from solara.server.starlette import app as starlette_app and then starlette_app.add_middleware(...). In the starlette docs there is already some info on how to write middleware.

So i think it is ok to close this! Thank you!

JovanVeljanoski avatar May 01 '25 10:05 JovanVeljanoski