mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Merge branch 'develop' into improve-config
This commit is contained in:
commit
ac3ce3b6fa
@ -2,6 +2,9 @@ import copy
|
|||||||
|
|
||||||
|
|
||||||
config = {
|
config = {
|
||||||
|
'server': {
|
||||||
|
'bind': '0.0.0.0:5000',
|
||||||
|
},
|
||||||
'database': {
|
'database': {
|
||||||
'host': 'localhost',
|
'host': 'localhost',
|
||||||
'port': 28015,
|
'port': 28015,
|
||||||
@ -26,3 +29,4 @@ config = {
|
|||||||
# for more info.
|
# for more info.
|
||||||
_config = copy.deepcopy(config)
|
_config = copy.deepcopy(config)
|
||||||
from bigchaindb.core import Bigchain # noqa
|
from bigchaindb.core import Bigchain # noqa
|
||||||
|
|
||||||
|
@ -59,6 +59,10 @@ def run_configure(args, skip_if_exists=False):
|
|||||||
conf['keypair']['private'], conf['keypair']['public'] = crypto.generate_key_pair()
|
conf['keypair']['private'], conf['keypair']['public'] = crypto.generate_key_pair()
|
||||||
|
|
||||||
if not args.yes:
|
if not args.yes:
|
||||||
|
for key in ('host', 'port'):
|
||||||
|
val = conf['server'][key]
|
||||||
|
conf['server'][key] = input('API Server {}? (default `{}`): '.format(key, val)) or val
|
||||||
|
|
||||||
for key in ('host', 'port', 'name'):
|
for key in ('host', 'port', 'name'):
|
||||||
val = conf['database'][key]
|
val = conf['database'][key]
|
||||||
conf['database'][key] = input('Database {}? (default `{}`): '.format(key, val)) or val
|
conf['database'][key] = input('Database {}? (default `{}`): '.format(key, val)) or val
|
||||||
|
@ -3,6 +3,7 @@ import multiprocessing as mp
|
|||||||
|
|
||||||
import rethinkdb as r
|
import rethinkdb as r
|
||||||
|
|
||||||
|
import bigchaindb
|
||||||
from bigchaindb import Bigchain
|
from bigchaindb import Bigchain
|
||||||
from bigchaindb.voter import Voter
|
from bigchaindb.voter import Voter
|
||||||
from bigchaindb.block import Block
|
from bigchaindb.block import Block
|
||||||
@ -11,6 +12,18 @@ from bigchaindb.web import server
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
BANNER = """
|
||||||
|
****************************************************************************
|
||||||
|
* *
|
||||||
|
* Initialization complete. BigchainDB is ready and waiting for events. *
|
||||||
|
* You can send events through the API documented at: *
|
||||||
|
* - http://docs.bigchaindb.apiary.io/ *
|
||||||
|
* *
|
||||||
|
* Listening to client connections on: {:<15} *
|
||||||
|
* *
|
||||||
|
****************************************************************************
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
class Processes(object):
|
class Processes(object):
|
||||||
|
|
||||||
@ -68,8 +81,8 @@ class Processes(object):
|
|||||||
block = Block(self.q_new_transaction)
|
block = Block(self.q_new_transaction)
|
||||||
|
|
||||||
# start the web api
|
# start the web api
|
||||||
webapi = server.create_app()
|
app_server = server.create_server(bigchaindb.config['server'])
|
||||||
p_webapi = mp.Process(name='webapi', target=webapi.run, kwargs={'host': 'localhost'})
|
p_webapi = mp.Process(name='webapi', target=app_server.run)
|
||||||
p_webapi.start()
|
p_webapi.start()
|
||||||
|
|
||||||
# initialize the processes
|
# initialize the processes
|
||||||
@ -92,5 +105,4 @@ class Processes(object):
|
|||||||
# start message
|
# start message
|
||||||
block.initialized.wait()
|
block.initialized.wait()
|
||||||
p_voter.initialized.wait()
|
p_voter.initialized.wait()
|
||||||
logger.info('Initialization complete. BigchainDB ready and waiting for events.')
|
logger.info(BANNER.format(bigchaindb.config['server']['bind']))
|
||||||
logger.info('You can send events through the API documented at http://docs.bigchaindb.apiary.io/')
|
|
||||||
|
@ -1,16 +1,56 @@
|
|||||||
"""This module contains basic functions to instantiate the BigchainDB API. """
|
"""This module contains basic functions to instantiate the BigchainDB API.
|
||||||
|
|
||||||
|
The application is implemented in Flask and runs using Gunicorn.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import copy
|
||||||
|
import multiprocessing
|
||||||
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
|
||||||
from bigchaindb import Bigchain
|
from bigchaindb import Bigchain
|
||||||
from bigchaindb.web import views
|
from bigchaindb.web import views
|
||||||
|
import gunicorn.app.base
|
||||||
|
|
||||||
|
|
||||||
|
class StandaloneApplication(gunicorn.app.base.BaseApplication):
|
||||||
|
"""Run a **wsgi** app wrapping it in a Gunicorn Base Application.
|
||||||
|
|
||||||
|
Adapted from:
|
||||||
|
- http://docs.gunicorn.org/en/latest/custom.html
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, app, options=None):
|
||||||
|
'''Initialize a new standalone application.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
app: A wsgi Python application.
|
||||||
|
options (dict): the configuration.
|
||||||
|
|
||||||
|
'''
|
||||||
|
self.options = options or {}
|
||||||
|
self.application = app
|
||||||
|
super(StandaloneApplication, self).__init__()
|
||||||
|
|
||||||
|
def load_config(self):
|
||||||
|
config = dict((key, value) for key, value in self.options.items()
|
||||||
|
if key in self.cfg.settings and value is not None)
|
||||||
|
|
||||||
|
for key, value in config.items():
|
||||||
|
# not sure if we need the `key.lower` here, will just keep
|
||||||
|
# keep it for now.
|
||||||
|
self.cfg.set(key.lower(), value)
|
||||||
|
|
||||||
|
def load(self):
|
||||||
|
return self.application
|
||||||
|
|
||||||
|
|
||||||
def create_app(debug=False):
|
def create_app(debug=False):
|
||||||
"""Return an instance of the Flask application.
|
"""Return an instance of the Flask application.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
debug (bool): a flag to activate the debug mode for the app (default: False).
|
debug (bool): a flag to activate the debug mode for the app
|
||||||
|
(default: False).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@ -18,3 +58,26 @@ def create_app(debug=False):
|
|||||||
app.config['bigchain'] = Bigchain()
|
app.config['bigchain'] = Bigchain()
|
||||||
app.register_blueprint(views.basic_views, url_prefix='/api/v1')
|
app.register_blueprint(views.basic_views, url_prefix='/api/v1')
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
def create_server(settings):
|
||||||
|
"""Wrap and return an application ready to be run.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
settings (dict): a dictionary containing the settings, more info
|
||||||
|
here http://docs.gunicorn.org/en/latest/settings.html
|
||||||
|
|
||||||
|
Return:
|
||||||
|
an initialized instance of the application.
|
||||||
|
"""
|
||||||
|
|
||||||
|
settings = copy.deepcopy(settings)
|
||||||
|
|
||||||
|
if not settings.get('workers'):
|
||||||
|
settings['workers'] = (multiprocessing.cpu_count() * 2) + 1
|
||||||
|
|
||||||
|
debug = settings.pop('debug', False)
|
||||||
|
app = create_app(debug)
|
||||||
|
standalone = StandaloneApplication(app, settings)
|
||||||
|
return standalone
|
||||||
|
|
||||||
|
1
setup.py
1
setup.py
@ -79,6 +79,7 @@ setup(
|
|||||||
'bitcoin==1.1.42',
|
'bitcoin==1.1.42',
|
||||||
'flask==0.10.1',
|
'flask==0.10.1',
|
||||||
'requests==2.9',
|
'requests==2.9',
|
||||||
|
'gunicorn~=19.0',
|
||||||
],
|
],
|
||||||
setup_requires=['pytest-runner'],
|
setup_requires=['pytest-runner'],
|
||||||
tests_require=tests_require,
|
tests_require=tests_require,
|
||||||
|
13
tests/web/test_server.py
Normal file
13
tests/web/test_server.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import copy
|
||||||
|
|
||||||
|
|
||||||
|
def test_settings(monkeypatch):
|
||||||
|
import bigchaindb
|
||||||
|
from bigchaindb.web import server
|
||||||
|
|
||||||
|
s = server.create_server(bigchaindb.config['server'])
|
||||||
|
|
||||||
|
# for whatever reason the value is wrapped in a list
|
||||||
|
# needs further investigation
|
||||||
|
assert s.cfg.bind[0] == bigchaindb.config['server']['bind']
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user