From 52db23603fe2436d0adc40f4051e68a19cf84112 Mon Sep 17 00:00:00 2001 From: Tanja Bergmann Date: Fri, 26 Jul 2019 11:16:38 +0200 Subject: [PATCH 1/3] Add root route to server without --enable-api --- rasa/core/run.py | 11 +++++++++-- rasa/server.py | 12 ++++++++---- tests/conftest.py | 8 ++++++++ tests/test_server.py | 11 +++++++++++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/rasa/core/run.py b/rasa/core/run.py index bbda948154e2..bf1c2c9d21aa 100644 --- a/rasa/core/run.py +++ b/rasa/core/run.py @@ -19,6 +19,7 @@ from rasa.core.utils import AvailableEndpoints, configure_file_logging from rasa.model import get_model_subdirectories, get_model from rasa.utils.common import update_sanic_log_level, class_from_module_path +from rasa.server import add_root_route logger = logging.getLogger() # get the root logger @@ -66,6 +67,13 @@ def _create_single_channel(channel, credentials): ) +def _create_app_without_api(cors: Optional[Union[Text, List[Text]]] = None): + app = Sanic(__name__, configure_logging=False) + add_root_route(app) + CORS(app, resources={r"/*": {"origins": cors or ""}}, automatic_options=True) + return app + + def configure_app( input_channels: Optional[List["InputChannel"]] = None, cors: Optional[Union[Text, List[Text]]] = None, @@ -92,8 +100,7 @@ def configure_app( endpoints=endpoints, ) else: - app = Sanic(__name__, configure_logging=False) - CORS(app, resources={r"/*": {"origins": cors or ""}}, automatic_options=True) + app = _create_app_without_api(cors) if input_channels: rasa.core.channels.channel.register(input_channels, app, route=route) diff --git a/rasa/server.py b/rasa/server.py index 5c17fc91519d..493b463ed5a8 100644 --- a/rasa/server.py +++ b/rasa/server.py @@ -274,6 +274,13 @@ async def _load_agent( return loaded_agent +def add_root_route(app: Sanic): + @app.get("/") + async def hello(request: Request): + """Check if the server is running and responds with the version.""" + return response.text("Hello from Rasa: " + rasa.__version__) + + def create_app( agent: Optional["Agent"] = None, cors_origins: Union[Text, List[Text]] = "*", @@ -311,10 +318,7 @@ def create_app( async def handle_error_response(request: Request, exception: ErrorResponse): return response.json(exception.error_info, status=exception.status) - @app.get("/") - async def hello(request: Request): - """Check if the server is running and responds with the version.""" - return response.text("Hello from Rasa: " + rasa.__version__) + add_root_route(app) @app.get("/version") async def version(request: Request): diff --git a/tests/conftest.py b/tests/conftest.py index 4d935ed58bde..fe81c9787b51 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,7 @@ import pytest import logging +from core.run import _create_app_without_api from rasa import server from rasa.core import config from rasa.core.agent import Agent, load_agent @@ -176,6 +177,13 @@ async def rasa_server_secured(default_agent): return app +@pytest.fixture +async def rasa_server_without_api(): + app = _create_app_without_api() + channel.register([RestInput()], app, "/webhooks/") + return app + + def clean_folder(folder): import os diff --git a/tests/test_server.py b/tests/test_server.py index 5df2029497ea..909a647b40b1 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -37,6 +37,11 @@ ] +@pytest.fixture +def rasa_app_without_api(rasa_server_without_api): + return rasa_server_without_api.test_client + + @pytest.fixture def rasa_app(rasa_server): return rasa_server.test_client @@ -63,6 +68,12 @@ def test_root(rasa_app): assert response.text.startswith("Hello from Rasa:") +def test_root_without_enable_api(rasa_app_without_api): + _, response = rasa_app_without_api.get("/") + assert response.status == 200 + assert response.text.startswith("Hello from Rasa:") + + def test_root_secured(rasa_secured_app): _, response = rasa_secured_app.get("/") assert response.status == 200 From 5580c18f1fd9ad433840798ecb1863bacf4d6ace Mon Sep 17 00:00:00 2001 From: Tanja Bergmann Date: Fri, 26 Jul 2019 11:17:23 +0200 Subject: [PATCH 2/3] add changelog entry --- CHANGELOG.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f0f92839412b..949111a54350 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -12,6 +12,7 @@ This project adheres to `Semantic Versioning`_ starting with version 1.0. Added ----- +- add root route to server started without ``--enable-api`` parameter Changed ------- From b6024f26b42c1ed79eed760727cd126318fd1980 Mon Sep 17 00:00:00 2001 From: Tanja Bergmann Date: Fri, 26 Jul 2019 11:40:53 +0200 Subject: [PATCH 3/3] Fix import --- tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index fe81c9787b51..7d77284b2101 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,7 @@ import pytest import logging -from core.run import _create_app_without_api +from rasa.core.run import _create_app_without_api from rasa import server from rasa.core import config from rasa.core.agent import Agent, load_agent