diff --git a/Dockerfile b/Dockerfile index 807761fe..159fef09 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,5 +13,6 @@ WORKDIR /data ENV BIGCHAINDB_CONFIG_PATH /data/.bigchaindb ENV BIGCHAINDB_SERVER_BIND 0.0.0.0:9984 ENV BIGCHAINDB_WSSERVER_HOST 0.0.0.0 +ENV BIGCHAINDB_WSSERVER_SCHEME ws ENTRYPOINT ["bigchaindb"] CMD ["start"] diff --git a/Dockerfile-dev b/Dockerfile-dev index 16236547..8d7b8797 100644 --- a/Dockerfile-dev +++ b/Dockerfile-dev @@ -9,6 +9,7 @@ RUN apt-get update \ ENV BIGCHAINDB_SERVER_BIND 0.0.0.0:9984 ENV BIGCHAINDB_WSSERVER_HOST 0.0.0.0 +ENV BIGCHAINDB_WSSERVER_SCHEME ws ARG backend diff --git a/bigchaindb/__init__.py b/bigchaindb/__init__.py index 1c93b4f5..1bef9c17 100644 --- a/bigchaindb/__init__.py +++ b/bigchaindb/__init__.py @@ -69,6 +69,7 @@ config = { 'workers': None, # if none, the value will be cpu_count * 2 + 1 }, 'wsserver': { + 'scheme': os.environ.get('BIGCHAINDB_WSSERVER_SCHEME') or 'ws', 'host': os.environ.get('BIGCHAINDB_WSSERVER_HOST') or 'localhost', 'port': int(os.environ.get('BIGCHAINDB_WSSERVER_PORT', 9985)), }, diff --git a/bigchaindb/commands/bigchaindb.py b/bigchaindb/commands/bigchaindb.py index a46019da..146dab91 100644 --- a/bigchaindb/commands/bigchaindb.py +++ b/bigchaindb/commands/bigchaindb.py @@ -96,7 +96,7 @@ def run_configure(args, skip_if_exists=False): val = conf['server'][key] conf['server'][key] = input_on_stderr('API Server {}? (default `{}`): '.format(key, val), val) - for key in ('host', 'port'): + for key in ('scheme', 'host', 'port'): val = conf['wsserver'][key] conf['wsserver'][key] = input_on_stderr('WebSocket Server {}? (default `{}`): '.format(key, val), val) diff --git a/bigchaindb/web/views/base.py b/bigchaindb/web/views/base.py index e4ae980b..52cbc0ef 100644 --- a/bigchaindb/web/views/base.py +++ b/bigchaindb/web/views/base.py @@ -3,7 +3,7 @@ Common classes and methods for API handlers """ import logging -from flask import jsonify, request +from flask import jsonify from bigchaindb import config @@ -21,14 +21,9 @@ def make_error(status_code, message=None): return response -def base_url(): - return '%s://%s/' % (request.environ['wsgi.url_scheme'], - request.environ['HTTP_HOST']) - - def base_ws_uri(): """Base websocket uri.""" - # TODO Revisit as this is a workaround to address issue - # https://github.com/bigchaindb/bigchaindb/issues/1465. - host = request.environ['HTTP_HOST'].split(':')[0] - return 'ws://{}:{}'.format(host, config['wsserver']['port']) + scheme = config['wsserver']['scheme'] + host = config['wsserver']['host'] + port = config['wsserver']['port'] + return '{}://{}:{}'.format(scheme, host, port) diff --git a/bigchaindb/web/views/info.py b/bigchaindb/web/views/info.py index 669e6df3..d6240bdb 100644 --- a/bigchaindb/web/views/info.py +++ b/bigchaindb/web/views/info.py @@ -4,7 +4,7 @@ import flask from flask_restful import Resource import bigchaindb -from bigchaindb.web.views.base import base_url, base_ws_uri +from bigchaindb.web.views.base import base_ws_uri from bigchaindb import version from bigchaindb.web.websocket_server import EVENTS_ENDPOINT @@ -17,7 +17,7 @@ class RootIndex(Resource): ] return flask.jsonify({ 'api': { - 'v1': get_api_v1_info() + 'v1': get_api_v1_info('/api/v1/') }, 'docs': ''.join(docs_url), 'software': 'BigchainDB', @@ -29,15 +29,14 @@ class RootIndex(Resource): class ApiV1Index(Resource): def get(self): - return flask.jsonify(get_api_v1_info()) + return flask.jsonify(get_api_v1_info('/')) -def get_api_v1_info(): +def get_api_v1_info(api_prefix): """ Return a dict with all the information specific for the v1 of the api. """ - api_root = base_url() + 'api/v1/' websocket_root = base_ws_uri() + EVENTS_ENDPOINT docs_url = [ 'https://docs.bigchaindb.com/projects/server/en/v', @@ -47,9 +46,9 @@ def get_api_v1_info(): return { 'docs': ''.join(docs_url), - 'transactions': api_root + 'transactions/', - 'statuses': api_root + 'statuses/', - 'assets': api_root + 'assets/', - 'outputs': api_root + 'outputs/', + 'transactions': '{}transactions/'.format(api_prefix), + 'statuses': '{}statuses/'.format(api_prefix), + 'assets': '{}assets/'.format(api_prefix), + 'outputs': '{}outputs/'.format(api_prefix), 'streams': websocket_root } diff --git a/docs/server/source/server-reference/configuration.md b/docs/server/source/server-reference/configuration.md index 3cb62c41..04fed917 100644 --- a/docs/server/source/server-reference/configuration.md +++ b/docs/server/source/server-reference/configuration.md @@ -22,6 +22,7 @@ For convenience, here's a list of all the relevant environment variables (docume `BIGCHAINDB_SERVER_LOGLEVEL`
`BIGCHAINDB_SERVER_WORKERS`
`BIGCHAINDB_SERVER_THREADS`
+`BIGCHAINDB_WSSERVER_SCHEME`
`BIGCHAINDB_WSSERVER_HOST`
`BIGCHAINDB_WSSERVER_PORT`
`BIGCHAINDB_CONFIG_PATH`
diff --git a/tests/test_config_utils.py b/tests/test_config_utils.py index 16fd043b..72f09039 100644 --- a/tests/test_config_utils.py +++ b/tests/test_config_utils.py @@ -147,6 +147,7 @@ def test_autoconfigure_read_both_from_file_and_env(monkeypatch, request, certs_d DATABASE_PORT = 4242 DATABASE_BACKEND = request.config.getoption('--database-backend') SERVER_BIND = '1.2.3.4:56' + WSSERVER_SCHEME = 'ws' WSSERVER_HOST = '1.2.3.4' WSSERVER_PORT = 57 KEYRING = 'pubkey_0:pubkey_1:pubkey_2' @@ -169,6 +170,7 @@ def test_autoconfigure_read_both_from_file_and_env(monkeypatch, request, certs_d 'BIGCHAINDB_DATABASE_PORT': str(DATABASE_PORT), 'BIGCHAINDB_DATABASE_BACKEND': 'mongodb', 'BIGCHAINDB_SERVER_BIND': SERVER_BIND, + 'BIGCHAINDB_WSSERVER_SCHEME': WSSERVER_SCHEME, 'BIGCHAINDB_WSSERVER_HOST': WSSERVER_HOST, 'BIGCHAINDB_WSSERVER_PORT': WSSERVER_PORT, 'BIGCHAINDB_KEYRING': KEYRING, @@ -183,6 +185,7 @@ def test_autoconfigure_read_both_from_file_and_env(monkeypatch, request, certs_d 'BIGCHAINDB_DATABASE_PORT': str(DATABASE_PORT), 'BIGCHAINDB_DATABASE_BACKEND': DATABASE_BACKEND, 'BIGCHAINDB_SERVER_BIND': SERVER_BIND, + 'BIGCHAINDB_WSSERVER_SCHEME': WSSERVER_SCHEME, 'BIGCHAINDB_WSSERVER_HOST': WSSERVER_HOST, 'BIGCHAINDB_WSSERVER_PORT': WSSERVER_PORT, 'BIGCHAINDB_KEYRING': KEYRING, @@ -255,6 +258,7 @@ def test_autoconfigure_read_both_from_file_and_env(monkeypatch, request, certs_d 'workers': None, }, 'wsserver': { + 'scheme': WSSERVER_SCHEME, 'host': WSSERVER_HOST, 'port': WSSERVER_PORT, }, diff --git a/tests/web/test_info.py b/tests/web/test_info.py index 3de01092..f3a1fe76 100644 --- a/tests/web/test_info.py +++ b/tests/web/test_info.py @@ -1,31 +1,23 @@ from unittest import mock -import pytest - - -@pytest.fixture -def api_v1_info(): - docs_url = ['https://docs.bigchaindb.com/projects/server/en/vtsttst', - '/http-client-server-api.html', - ] - return { - 'docs': ''.join(docs_url), - 'transactions': 'http://localhost/api/v1/transactions/', - 'statuses': 'http://localhost/api/v1/statuses/', - 'assets': 'http://localhost/api/v1/assets/', - 'outputs': 'http://localhost/api/v1/outputs/', - 'streams': 'ws://localhost:9985/api/v1/streams/valid_transactions', - } - @mock.patch('bigchaindb.version.__short_version__', 'tst') @mock.patch('bigchaindb.version.__version__', 'tsttst') @mock.patch('bigchaindb.config', {'keyring': ['abc'], 'keypair': {'public': 'def'}}) -def test_api_root_endpoint(client, api_v1_info): +def test_api_root_endpoint(client): res = client.get('/') + docs_url = ['https://docs.bigchaindb.com/projects/server/en/vtsttst', + '/http-client-server-api.html'] assert res.json == { 'api': { - 'v1': api_v1_info + 'v1': { + 'docs': ''.join(docs_url), + 'transactions': '/api/v1/transactions/', + 'statuses': '/api/v1/statuses/', + 'assets': '/api/v1/assets/', + 'outputs': '/api/v1/outputs/', + 'streams': 'ws://localhost:9985/api/v1/streams/valid_transactions', + } }, 'docs': 'https://docs.bigchaindb.com/projects/server/en/vtsttst/', 'version': 'tsttst', @@ -37,6 +29,16 @@ def test_api_root_endpoint(client, api_v1_info): @mock.patch('bigchaindb.version.__short_version__', 'tst') @mock.patch('bigchaindb.version.__version__', 'tsttst') -def test_api_v1_endpoint(client, api_v1_info): +def test_api_v1_endpoint(client): + docs_url = ['https://docs.bigchaindb.com/projects/server/en/vtsttst', + '/http-client-server-api.html'] + api_v1_info = { + 'docs': ''.join(docs_url), + 'transactions': '/transactions/', + 'statuses': '/statuses/', + 'assets': '/assets/', + 'outputs': '/outputs/', + 'streams': 'ws://localhost:9985/api/v1/streams/valid_transactions', + } res = client.get('/api/v1') assert res.json == api_v1_info