Skip to content

Commit

Permalink
Merge pull request #4111 from RasaHQ/rasa-server-root
Browse files Browse the repository at this point in the history
Add root route to server started without --enable-api
  • Loading branch information
tabergma committed Jul 26, 2019
2 parents ad41208 + fecc154 commit f87f51d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
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
- add ``--evaluate-model-directory`` to ``rasa test core`` to evaluate models from ``rasa train core -c <config-1> <config-2>``

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 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
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

0 comments on commit f87f51d

Please sign in to comment.