From a33144f2f9dc0840cf3ee3a898b40b309e7baed1 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 26 Jun 2020 02:02:30 +0200 Subject: [PATCH] Middleware? --- pyproject.toml | 2 +- royalnet/constellation/api/apistar.py | 1 - royalnet/constellation/constellation.py | 13 ++++++++++++- royalnet/version.py | 2 +- sample_config.toml | 3 +++ 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 4f683256..64fa9a7b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ [tool.poetry] name = "royalnet" - version = "5.9.2" + version = "5.9.3" description = "A multipurpose bot and web framework" authors = ["Stefano Pigozzi "] license = "AGPL-3.0+" diff --git a/royalnet/constellation/api/apistar.py b/royalnet/constellation/api/apistar.py index 4eb4154d..5f30db36 100644 --- a/royalnet/constellation/api/apistar.py +++ b/royalnet/constellation/api/apistar.py @@ -21,7 +21,6 @@ class ApiStar(PageStar, ABC): tags: List[str] = [] - async def page(self, request: Request) -> JSONResponse: if request.query_params: data = request.query_params diff --git a/royalnet/constellation/constellation.py b/royalnet/constellation/constellation.py index 0c28aa61..8a98291f 100644 --- a/royalnet/constellation/constellation.py +++ b/royalnet/constellation/constellation.py @@ -4,6 +4,8 @@ import logging import importlib import uvicorn import starlette.applications +import starlette.middleware +import starlette.middleware.cors import royalnet.alchemy as ra import royalnet.herald as rh import royalnet.utils as ru @@ -96,7 +98,16 @@ class Constellation: self.events: Dict[str, rc.Event] = {} """A dictionary containing all :class:`~rc.Event` that can be handled by this :class:`Constellation`.""" - self.starlette = starlette.applications.Starlette(debug=__debug__) + middleware = [] + if constellation_cfg.get("cors_middleware", False): + log.info("CORS middleware: enabled") + middleware.append( + starlette.middleware.Middleware(starlette.middleware.cors.CORSMiddleware, {"allow_origins": ["*"]}) + ) + else: + log.info("CORS middleware: disabled") + + self.starlette = starlette.applications.Starlette(debug=__debug__, middleware=middleware) """The :class:`~starlette.Starlette` app.""" self.stars: List[PageStar] = [] diff --git a/royalnet/version.py b/royalnet/version.py index 62374c71..d6a72e3e 100644 --- a/royalnet/version.py +++ b/royalnet/version.py @@ -1 +1 @@ -semantic = "5.9.2" +semantic = "5.9.3" diff --git a/sample_config.toml b/sample_config.toml index fbe24ae5..a6ed18c7 100644 --- a/sample_config.toml +++ b/sample_config.toml @@ -55,6 +55,9 @@ enabled = true address = "0.0.0.0" # The port on which the Constellation should run port = 44445 +# If the CORS middleware should be enabled +# https://www.starlette.io/middleware/#corsmiddleware +cors_middleware = true [Serfs]