diff --git a/Makefile.local b/Makefile.local index 8cb95ce..fb7deed 100644 --- a/Makefile.local +++ b/Makefile.local @@ -1,32 +1,32 @@ # should be provided by the ci build TAG=latest IMAGE_NAME=softwareheritage/graphql:$(TAG) # ci build should provide the right label IMAGE_LABEL=dummy # can be overridden by ci REGISTRY_URL=registry-1.docker.io .PHONY: run-dev run-dev: export SWH_CONFIG_FILENAME=config/dev.yml; uvicorn swh.graphql.server:make_app_from_configfile --reload --factory .PHONY: run-dev-stable run-dev-stable: export SWH_CONFIG_FILENAME=config/dev.yml; uvicorn swh.graphql.server:make_app_from_configfile --factory .PHONY: run-wsgi run-wsgi: - export SWH_CONFIG_FILENAME=config/staging.yml; gunicorn --workers=2 'swh.graphql.server:make_app_from_configfile()' + export SWH_CONFIG_FILENAME=config/staging.yml; gunicorn 'swh.graphql.server:make_app_from_configfile()' -w 2 -k uvicorn.workers.UvicornWorker .PHONY: run-dev-docker run-dev-docker: docker-compose -f docker-compose.yml -f docker-compose-dev.yml up --build .PHONY: run-wsgi-docker run-wsgi-docker: docker-compose -f docker-compose.yml -f docker-compose-staging.yml up --build .PHONY: update-desktop6 update-desktop6: git pull origin master docker-compose -f docker-compose.yml -f docker-compose-staging.yml restart diff --git a/config/staging.yml b/config/staging.yml index 2e7ee1f..f989165 100644 --- a/config/staging.yml +++ b/config/staging.yml @@ -1,11 +1,11 @@ storage: cls: remote url: http://webapp.internal.staging.swh.network:5002 search: cls: remote url: http://webapp.internal.staging.swh.network:5010 debug: no -server-type: wsgi +server-type: asgi diff --git a/docker-compose-staging.yml b/docker-compose-staging.yml index 3dd8637..a04a9e4 100644 --- a/docker-compose-staging.yml +++ b/docker-compose-staging.yml @@ -1,8 +1,8 @@ version: "3.4" services: app: dns: 192.168.100.29 environment: - SWH_CONFIG_FILENAME=config/staging.yml - command: gunicorn --bind=0.0.0.0:8000 --workers=2 'swh.graphql.server:make_app_from_configfile()' + command: gunicorn --bind=0.0.0.0:8000 -w 2 -k uvicorn.workers.UvicornWorker 'swh.graphql.server:make_app_from_configfile()' diff --git a/swh/graphql/server.py b/swh/graphql/server.py index 5df83d9..7a92919 100644 --- a/swh/graphql/server.py +++ b/swh/graphql/server.py @@ -1,91 +1,86 @@ # Copyright (C) 2022 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import os from typing import Any, Dict, Optional from swh.core import config from swh.search import get_search as get_swh_search from swh.storage import get_storage as get_swh_storage graphql_cfg = None storage = None search = None def get_storage(): global storage if not storage: storage = get_swh_storage(**graphql_cfg["storage"]) return storage def get_search(): global search if not search: search = get_swh_search(**graphql_cfg["search"]) return search def load_and_check_config(config_path: Optional[str]) -> Dict[str, Any]: """Check the minimal configuration is set to run the api or raise an error explanation. Args: config_path: Path to the configuration file to load Raises: Error if the setup is not as expected Returns: configuration as a dict """ if not config_path: raise EnvironmentError("Configuration file must be defined") if not os.path.exists(config_path): raise FileNotFoundError(f"Configuration file {config_path} does not exist") cfg = config.read(config_path) if "storage" not in cfg: raise KeyError("Missing 'storage' configuration") return cfg def make_app_from_configfile(): """Loading the configuration from a configuration file. SWH_CONFIG_FILENAME environment variable defines the configuration path to load. """ + from starlette.middleware.cors import CORSMiddleware + from .app import schema from .errors.handlers import format_error global graphql_cfg if not graphql_cfg: config_path = os.environ.get("SWH_CONFIG_FILENAME") graphql_cfg = load_and_check_config(config_path) server_type = graphql_cfg.get("server-type") if server_type == "asgi": from ariadne.asgi import GraphQL - from starlette.middleware.cors import CORSMiddleware - # Enable cors in the asgi version application = CORSMiddleware( GraphQL(schema, debug=graphql_cfg["debug"], error_formatter=format_error), + # FIXME, restrict origins after deploying the JS client allow_origins=["*"], allow_methods=("GET", "POST", "OPTIONS"), ) - else: - from ariadne.wsgi import GraphQL - - application = GraphQL( - schema, debug=graphql_cfg["debug"], error_formatter=format_error - ) return application