Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add root route to server started without --enable-api #4111

Merged
merged 4 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down
11 changes: 9 additions & 2 deletions rasa/core/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions rasa/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]] = "*",
Expand Down Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down