]> git.ipfire.org Git - dbl.git/commitdiff
api: Add a middleware to correctly close database sessions
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jan 2026 17:02:37 +0000 (17:02 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jan 2026 17:14:05 +0000 (17:14 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/dnsbl/api/__init__.py
src/dnsbl/api/middlewares.py [new file with mode: 0644]

index 4ffe659f6d0518e93f03763ff4f5a5059e0ebc34..20bef28f2b5f3619597b18433ac9453417732c8b 100644 (file)
@@ -70,6 +70,7 @@ dist_pkgpython_api_PYTHON = \
        src/dnsbl/api/__init__.py \
        src/dnsbl/api/domains.py \
        src/dnsbl/api/lists.py \
+       src/dnsbl/api/middlewares.py \
        src/dnsbl/api/reports.py
 
 pkgpython_apidir = $(pkgpythondir)/api
index b35297f5aa1d2713710278baf9e2d5d45d33ddf6..4289cd66cc953048433ef6b77c57870ba3be5fa6 100644 (file)
@@ -24,6 +24,9 @@ import fastapi.security
 # Import the backend
 from .. import Backend
 
+# Import middlewares
+from . import middlewares
+
 # Initialize the app
 app = fastapi.FastAPI(
        title = "IPFire DNSBL API",
@@ -31,6 +34,7 @@ app = fastapi.FastAPI(
        # Enable debug mode
        debug = True,
 )
+app.add_middleware(middlewares.DatabaseSessionMiddleware)
 
 # Initialize the backend
 backend = app.state.backend = Backend(
diff --git a/src/dnsbl/api/middlewares.py b/src/dnsbl/api/middlewares.py
new file mode 100644 (file)
index 0000000..534731b
--- /dev/null
@@ -0,0 +1,29 @@
+###############################################################################
+#                                                                             #
+# dnsbl - A DNS Blocklist Compositor For IPFire                               #
+# Copyright (C) 2026 IPFire Development Team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+import fastapi
+
+class DatabaseSessionMiddleware(fastapi.applications.BaseHTTPMiddleware):
+       async def dispatch(self, request: fastapi.Request, call_next):
+               backend = request.app.state.backend
+
+               # Acquire a new database session and process the request
+               with backend.db.session() as session:
+                       return await call_next(request)