diff --git a/swh/deposit/server.py b/swh/deposit/server.py index f358aecc..1c43d728 100644 --- a/swh/deposit/server.py +++ b/swh/deposit/server.py @@ -1,119 +1,133 @@ # Copyright (C) 2017 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import asyncio import aiohttp.web import click import jinja2 import json from swh.core import config from swh.core.config import SWHConfig from swh.core.api_async import SWHRemoteAPI from swh.deposit.backend import DepositBackend -DEFAULT_CONFIG_SERVER = { +DEFAULT_CONFIG_PATH = 'deposit/server' +DEFAULT_CONFIG = { 'host': ('str', '0.0.0.0'), 'port': ('int', 5012), } def encode_data(data, template_name=None, **kwargs): return aiohttp.web.Response( body=data, headers={'Content-Type': 'application/xml'}, **kwargs ) class DepositWebServer(SWHConfig): """Base class to define endpoints route. """ - CONFIG_BASE_FILENAME = 'deposit/server' + CONFIG_BASE_FILENAME = DEFAULT_CONFIG_PATH DEFAULT_CONFIG = { 'max_upload_size': ('int', 200 * 1024 * 1024), - 'deposit_db': ('str', 'dbname=deposit-db'), + 'deposit_db': ('str', 'dbname=softwareheritage-deposit-dev'), } def __init__(self, config=None): if config: self.config = config else: self.config = self.parse_config_file() template_loader = jinja2.FileSystemLoader( searchpath=["swh/deposit/templates"]) self.template_env = jinja2.Environment(loader=template_loader) self.backend = DepositBackend() @asyncio.coroutine def index(self, request): return aiohttp.web.Response(text='SWH Deposit Server') @asyncio.coroutine def service_document(self, request): tpl = self.template_env.get_template('service_document.xml') output = tpl.render( noop=True, verbose=False, max_upload_size=self.config['max_upload_size']) return encode_data(data=output) @asyncio.coroutine def create_document(self, request): pass @asyncio.coroutine def update_document(self, request): pass @asyncio.coroutine def status_operation(self, request): pass @asyncio.coroutine def delete_document(self, request): raise ValueError('Not implemented') @asyncio.coroutine def client_get(self, request): clients = self.backend.client_list() return aiohttp.web.Response( body=json.dumps(clients), headers={'Content-Type': 'application/json'}) def make_app(config, **kwargs): + """Initialize server application. + + Returns: + Application ready for running and serving api endpoints. + + """ app = SWHRemoteAPI(**kwargs) server = DepositWebServer() app.router.add_route('GET', '/', server.index) app.router.add_route('GET', '/api/1/deposit/', server.service_document) app.router.add_route('GET', '/api/1/status/', server.status_operation) app.router.add_route('POST', '/api/1/deposit/', server.create_document) app.router.add_route('PUT', '/api/1/deposit/', server.update_document) app.router.add_route('DELETE', '/api/1/deposit/', server.delete_document) app.router.add_route('GET', '/api/1/client/', server.client_get) app.update(config) return app +def make_app_from_configfile(config_path=DEFAULT_CONFIG_PATH, **kwargs): + """Initialize server application from configuration file. + + Returns: + Application ready for running and serving api endpoints. + + """ + return make_app(config.read(config_path, DEFAULT_CONFIG), **kwargs) + + @click.command() @click.argument('config-path', required=1) @click.option('--host', default='0.0.0.0', help="Host to run the server") @click.option('--port', default=5012, type=click.INT, help="Binding port of the server") @click.option('--debug/--nodebug', default=True, help="Indicates if the server should run in debug mode") def launch(config_path, host, port, debug): - cfg = config.read(config_path, DEFAULT_CONFIG_SERVER) - port = port if port else cfg['port'] - host = host if host else cfg['host'] - app = make_app(cfg, debug=bool(debug)) + app = make_app_from_configfile(config_path, debug=bool(debug)) aiohttp.web.run_app(app, host=host, port=port) if __name__ == '__main__': launch()