Skip to content

Commit

Permalink
feat: support middlewares from env
Browse files Browse the repository at this point in the history
  • Loading branch information
xuhaidong committed Dec 13, 2024
1 parent a85eff1 commit 913f98a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 2 additions & 1 deletion sea/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ def _register_extension(self, name, ext):
self._extensions[name] = ext

def load_middlewares(self):
mids = ["sea.middleware.GuardMiddleware"] + self.config.get("MIDDLEWARES")
env_mids = os.environ.get("SEA_MIDDLEWARES", "").split(",")
mids = ["sea.middleware.GuardMiddleware"] + [mid for mid in env_mids if mid] + self.config.get("MIDDLEWARES")
for mn in mids:
m = utils.import_string(mn)
self._middlewares.insert(0, m)
Expand Down
4 changes: 3 additions & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ def test_baseapp(caplog):
assert _app.config["PORT"] == 4001
assert _app.debug
assert _app.testing
os.environ["SEA_MIDDLEWARES"] = "sea.middleware.BaseMiddleware,"
_app.load_middlewares()

assert len(_app.middlewares) == 3
assert len(_app.middlewares) == 5
print(_app.middlewares)

with mock.patch('sea._app', new=_app):
from app import extensions, servicers
Expand Down

0 comments on commit 913f98a

Please sign in to comment.