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 25, 2024
1 parent a85eff1 commit 8d90789
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# CHANGELOG

## [Unreleased] - 2023-08-21
## [3.1.2] - 2024-12-18
### Added
- Support reading grpc middlewares from env and removing duplicates according to config middlewares

## [3.1.1] - 2023-08-21
### Added
- Support grpc reflection

Expand Down
5 changes: 4 additions & 1 deletion sea/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ def _register_extension(self, name, ext):
self._extensions[name] = ext

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

assert len(_app.middlewares) == 3
assert len(_app.middlewares) == 4

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

0 comments on commit 8d90789

Please sign in to comment.