Browse Source

Add function to insert app middleware (#22)

Nikhil Rao 2 years ago
parent
commit
e636f0dd3e
2 changed files with 13 additions and 0 deletions
  1. 1 0
      pynecone/__init__.py
  2. 12 0
      pynecone/app.py

+ 1 - 0
pynecone/__init__.py

@@ -9,6 +9,7 @@ from .components import *
 from .config import Config
 from .config import Config
 from .constants import Env
 from .constants import Env
 from .event import console_log, redirect, window_alert
 from .event import console_log, redirect, window_alert
+from .middleware import Middleware
 from .model import Model, session
 from .model import Model, session
 from .state import ComputedVar as var
 from .state import ComputedVar as var
 from .state import State
 from .state import State

+ 12 - 0
pynecone/app.py

@@ -162,6 +162,18 @@ class App(Base):
             if out is not None:
             if out is not None:
                 return out
                 return out
 
 
+    def add_middleware(self, middleware: Middleware, index: Optional[int] = None):
+        """Add middleware to the app.
+
+        Args:
+            middleware: The middleware to add.
+            index: The index to add the middleware at.
+        """
+        if index is None:
+            self.middleware.append(middleware)
+        else:
+            self.middleware.insert(index, middleware)
+
     def add_page(
     def add_page(
         self,
         self,
         component: Union[Component, ComponentCallable],
         component: Union[Component, ComponentCallable],