wsgi.py 5.92 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Import API Service
# Copyright (C) 2020-2021 Memobase Project
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
Jonas Waeber's avatar
Jonas Waeber committed
16
17
18
19
20
21
22
23
24
25
26
27
28
import logging
import os
from os import environ

from flasgger import Swagger
from flask import send_from_directory, redirect
from flask.logging import default_handler
from flask_restful import Api
from kubernetes import config
from kubernetes.config import ConfigException

from import_api_app.app import app
from import_api_app.helpers.error import ImportApiError
Jonas Waeber's avatar
Jonas Waeber committed
29
30
31
32
33
34
35
36
37
38
from import_api_app.resources.clear_cache import ClearCache
from import_api_app.resources.delete_institution import DeleteInstitution
from import_api_app.resources.delete_record import DeleteRecord
from import_api_app.resources.delete_recordset import DeleteRecordset
from import_api_app.resources.fetch_mapping_file import FetchMappingFile
from import_api_app.resources.import_process_start import ImportProcessStart
from import_api_app.resources.update_institution import UpdateInstitution
from import_api_app.resources.update_record_set import UpdateRecordSet
from import_api_app.resources.write_documents_reports import WriteJobResultToDrupal
from import_api_app.resources.write_groups_reports import WriteTypeReportToDrupal
Jonas Waeber's avatar
Jonas Waeber committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

try:
    # to be used when inside a kubernetes cluster
    config.load_incluster_config()
except ConfigException:
    try:
        # use .kube directory
        # for local development
        config.load_kube_config()
    except ConfigException:
        app.logger.error('No configuration for any Kubernetes Cluster detected.')
        raise ImportApiError('No configuration for any Kubernetes Cluster detected.')

environ['HELM_EXPERIMENTAL_OCI'] = '1'

# If app is started via gunicorn
if __name__ != '__main__':
    gunicorn_logger = logging.getLogger('gunicorn.error')
    app.logger.handlers = gunicorn_logger.handlers
    app.logger.setLevel(gunicorn_logger.level)
    app.logger.removeHandler(default_handler)
    app.logger.info('Starting production server')

api = Api(app)

try:
    app.config['import-api-url'] = os.environ['IMPORT_API_URL']
    app.config['kafka-broker-url'] = os.environ['KAFKA_BOOTSTRAP_SERVERS']
    app.config['drupal-user'] = os.environ['DRUPAL_USERNAME']
    app.config['drupal-password'] = os.environ['DRUPAL_PASSWORD']
    app.config['drupal-api-url'] = os.environ['DRUPAL_API_URL']
    app.config['drupal-api-key'] = os.environ['DRUPAL_API_KEY']
    app.config['clear-cache-url'] = os.environ['CLEAR_CACHE_URL']
    app.config['topic-configs'] = os.environ['TOPIC_CONFIGS']
    app.config['topic-drupal-export'] = os.environ['TOPIC_DRUPAL_EXPORT']
    app.config['sftp_host'] = os.environ['SFTP_HOST']
    app.config['sftp_port'] = os.environ['SFTP_PORT']
    app.config['sftp_user'] = os.environ['SFTP_USER']
    app.config['sftp_password'] = os.environ['SFTP_PASSWORD']
Jonas Waeber's avatar
Jonas Waeber committed
78
    app.config['sftp_base_path'] = os.environ['SFTP_BASE_PATH']
Jonas Waeber's avatar
Jonas Waeber committed
79
80
81
82
83
    app.config['tfv-kafka-configs'] = os.environ['TFV_KAFKA_SERVER_CONFIGS']
    app.config['tfv-sftp-configs'] = os.environ['TFV_SFTP_CONFIGS']
    app.config['tfv-topic-name'] = os.environ['TFV_TOPIC_NAME']
    app.config['tfv-reporting-topic-name'] = os.environ['TFV_REPORTING_TOPIC_NAME']
    app.config['env'] = os.environ['ENV']
Jonas Waeber's avatar
Jonas Waeber committed
84
    app.config['NAMESPACE'] = os.environ['NAMESPACE']
Jonas Waeber's avatar
Jonas Waeber committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

    app.config['SWAGGER'] = {
        'title': 'Memobase Import API',
        'version': 'dev',
        'uiversion': 3,
        'termsOfService': 'http://memobase.ch/de/disclaimer',
        'description': 'API to start, stop, manage import processes for '
                       'memobase. Will be used in the Admin Interface (Drupal).',
        'contact': {
            'name': 'UB Basel',
            'url': 'https://ub.unibas.ch',
            'email': 'swissbib-ub@unibas.ch'},
        'favicon': '/favicon.ico'}
    Swagger(app)
except KeyError as ex:
    raise ImportApiError(f'Environment variable {ex} is missing.')


@app.route("/")
def home():
    return redirect("/apidocs")


@app.route('/favicon.ico')
def favicon():
    return send_from_directory(
        os.path.join(
            app.root_path,
            'assets'),
        'favicon.ico',
        mimetype='image/vnd.microsoft.icon')


api.add_resource(
    ImportProcessStart,
    '/v1/importprocess/<institution_id>/<record_set_id>/start'
)

api.add_resource(
    FetchMappingFile,
    '/v1/FetchMappingFile/<recordset_id>/<session_id>'
)
api.add_resource(WriteJobResultToDrupal, '/v1/drupal/<job_drupal_uuid>/<job_log_drupal_uuid>')
api.add_resource(WriteTypeReportToDrupal, '/v1/drupal/WriteElementReport')
api.add_resource(UpdateInstitution, '/v1/drupal/institution/<institution_drupal_uuid>')
api.add_resource(UpdateRecordSet, '/v1/drupal/recordset/<record_set_drupal_uuid>')
api.add_resource(
    DeleteRecord,
    '/v1/drupal/delete/record/<session_id>',
    '/v1/drupal/delete/record/<session_id>/<dryrun>'
)
api.add_resource(
    DeleteRecordset,
    '/v1/drupal/delete/recordset/<session_id>',
    '/v1/drupal/delete/recordset/<session_id>/<dryrun>'
)
api.add_resource(
    DeleteInstitution,
    '/v1/drupal/delete/institution/<session_id>',
    '/v1/drupal/delete/institution/<session_id>/<dryrun>'
)
api.add_resource(ClearCache, '/v1/drupal/clearcache')

# If app is started with Flask
if __name__ == '__main__':
    logging.basicConfig(format='%(levelname)-8s [%(filename)s:%(lineno)d] %(message)s')
    app.logger.info('Starting development server')
    app.run(host='0.0.0.0', port=5000, debug=True)