Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/storage/api/server.py
# Copyright (C) 2015-2018 The Software Heritage developers | # Copyright (C) 2015-2018 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import functools | |||||
import logging | import logging | ||||
import click | import click | ||||
from flask import request | from flask import request | ||||
from swh.core import config | from swh.core import config | ||||
from swh.core.api import (SWHServerAPIApp, decode_request, | from swh.core.api import (SWHServerAPIApp, decode_request, | ||||
error_handler, | error_handler, | ||||
encode_data_server as encode_data) | encode_data_server as encode_data) | ||||
from swh.indexer.storage import get_indexer_storage, INDEXER_CFG_KEY | from swh.indexer.storage import get_indexer_storage, INDEXER_CFG_KEY | ||||
from .. import IndexerStorage | |||||
DEFAULT_CONFIG_PATH = 'storage/indexer' | DEFAULT_CONFIG_PATH = 'storage/indexer' | ||||
DEFAULT_CONFIG = { | DEFAULT_CONFIG = { | ||||
INDEXER_CFG_KEY: ('dict', { | INDEXER_CFG_KEY: ('dict', { | ||||
'cls': 'local', | 'cls': 'local', | ||||
'args': { | 'args': { | ||||
'db': 'dbname=softwareheritage-indexer-dev', | 'db': 'dbname=softwareheritage-indexer-dev', | ||||
}, | }, | ||||
Show All 18 Lines | def get_storage(): | ||||
return storage | return storage | ||||
@app.route('/') | @app.route('/') | ||||
def index(): | def index(): | ||||
return 'SWH Indexer Storage API server' | return 'SWH Indexer Storage API server' | ||||
@app.route('/check_config', methods=['POST']) | # See client.py in the same directory to understand this magic. | ||||
def check_config(): | for (_name, _meth) in IndexerStorage.__dict__.items(): | ||||
return encode_data(get_storage().check_config(**decode_request(request))) | if hasattr(_meth, '_endpoint_path'): | ||||
def _closure(name, meth): | |||||
@app.route('/'+meth._endpoint_path, methods=['POST']) | |||||
@app.route('/content_mimetype/add', methods=['POST']) | @functools.wraps(meth) # Copy signature and doc | ||||
def content_mimetype_add(): | def _f(): | ||||
return encode_data( | # Call the actual code | ||||
get_storage().content_mimetype_add(**decode_request(request))) | return encode_data( | ||||
getattr(get_storage(), name)(**decode_request(request))) | |||||
globals()[name] = _f | |||||
@app.route('/content_mimetype/missing', methods=['POST']) | _closure(_name, _meth) | ||||
def content_mimetype_missing(): | |||||
return encode_data( | |||||
get_storage().content_mimetype_missing(**decode_request(request))) | |||||
@app.route('/content_mimetype', methods=['POST']) | |||||
def content_mimetype_get(): | |||||
return encode_data( | |||||
get_storage().content_mimetype_get(**decode_request(request))) | |||||
@app.route('/content_language/add', methods=['POST']) | |||||
def content_language_add(): | |||||
return encode_data( | |||||
get_storage().content_language_add(**decode_request(request))) | |||||
@app.route('/content_language/missing', methods=['POST']) | |||||
def content_language_missing(): | |||||
return encode_data( | |||||
get_storage().content_language_missing(**decode_request(request))) | |||||
@app.route('/content_language', methods=['POST']) | |||||
def content_language_get(): | |||||
return encode_data( | |||||
get_storage().content_language_get(**decode_request(request))) | |||||
@app.route('/content/ctags/add', methods=['POST']) | |||||
def content_ctags_add(): | |||||
return encode_data( | |||||
get_storage().content_ctags_add(**decode_request(request))) | |||||
@app.route('/content/ctags/search', methods=['POST']) | |||||
def content_ctags_search(): | |||||
return encode_data( | |||||
get_storage().content_ctags_search(**decode_request(request))) | |||||
@app.route('/content/ctags/missing', methods=['POST']) | |||||
def content_ctags_missing(): | |||||
return encode_data( | |||||
get_storage().content_ctags_missing(**decode_request(request))) | |||||
@app.route('/content/ctags', methods=['POST']) | |||||
def content_ctags_get(): | |||||
return encode_data( | |||||
get_storage().content_ctags_get(**decode_request(request))) | |||||
@app.route('/content/fossology_license/add', methods=['POST']) | |||||
def content_fossology_license_add(): | |||||
return encode_data( | |||||
get_storage().content_fossology_license_add(**decode_request(request))) | |||||
@app.route('/content/fossology_license', methods=['POST']) | |||||
def content_fossology_license_get(): | |||||
return encode_data( | |||||
get_storage().content_fossology_license_get(**decode_request(request))) | |||||
@app.route('/indexer_configuration/data', methods=['POST']) | |||||
def indexer_configuration_get(): | |||||
return encode_data(get_storage().indexer_configuration_get( | |||||
**decode_request(request))) | |||||
@app.route('/indexer_configuration/add', methods=['POST']) | |||||
def indexer_configuration_add(): | |||||
return encode_data(get_storage().indexer_configuration_add( | |||||
**decode_request(request))) | |||||
@app.route('/content_metadata/add', methods=['POST']) | |||||
def content_metadata_add(): | |||||
return encode_data( | |||||
get_storage().content_metadata_add(**decode_request(request))) | |||||
del _name, _meth | |||||
@app.route('/content_metadata/missing', methods=['POST']) | |||||
def content_metadata_missing(): | |||||
return encode_data( | |||||
get_storage().content_metadata_missing(**decode_request(request))) | |||||
@app.route('/content_metadata', methods=['POST']) | |||||
def content_metadata_get(): | |||||
return encode_data( | |||||
get_storage().content_metadata_get(**decode_request(request))) | |||||
@app.route('/revision_metadata/add', methods=['POST']) | |||||
def revision_metadata_add(): | |||||
return encode_data( | |||||
get_storage().revision_metadata_add(**decode_request(request))) | |||||
@app.route('/revision_metadata/missing', methods=['POST']) | |||||
def revision_metadata_missing(): | |||||
return encode_data( | |||||
get_storage().revision_metadata_missing(**decode_request(request))) | |||||
@app.route('/revision_metadata', methods=['POST']) | |||||
def revision_metadata_get(): | |||||
return encode_data( | |||||
get_storage().revision_metadata_get(**decode_request(request))) | |||||
def run_from_webserver(environ, start_response, | def run_from_webserver(environ, start_response, | ||||
config_path=DEFAULT_CONFIG_PATH): | config_path=DEFAULT_CONFIG_PATH): | ||||
"""Run the WSGI app from the webserver, loading the configuration.""" | """Run the WSGI app from the webserver, loading the configuration.""" | ||||
cfg = config.load_named_config(config_path, DEFAULT_CONFIG) | cfg = config.load_named_config(config_path, DEFAULT_CONFIG) | ||||
app.config.update(cfg) | app.config.update(cfg) | ||||
handler = logging.StreamHandler() | handler = logging.StreamHandler() | ||||
Show All 18 Lines |