|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright 2019 - Swiss Data Science Center (SDSC) |
| 4 | +# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and |
| 5 | +# Eidgenössische Technische Hochschule Zürich (ETHZ). |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +"""Renku service entry point.""" |
| 19 | +import os |
| 20 | +import uuid |
| 21 | + |
| 22 | +from flask import Flask |
| 23 | +from flask_swagger_ui import get_swaggerui_blueprint |
| 24 | + |
| 25 | +from renku.service.config import API_URL, API_VERSION, CACHE_PROJECTS_PATH, \ |
| 26 | + CACHE_UPLOADS_PATH, SWAGGER_URL, UPLOAD_FOLDER |
| 27 | +from renku.service.views.cache import clone_repository_view, \ |
| 28 | + list_projects_view, list_uploaded_files_view, upload_files_view |
| 29 | +from renku.service.views.datasets import add_file_to_dataset_view, \ |
| 30 | + create_dataset_view, list_dataset_files_view, list_datasets_view |
| 31 | +from renku.service.views.docs import api_docs_view |
| 32 | + |
| 33 | + |
| 34 | +def make_cache(): |
| 35 | + """Create cache structure.""" |
| 36 | + sub_dirs = [CACHE_UPLOADS_PATH, CACHE_PROJECTS_PATH] |
| 37 | + |
| 38 | + for subdir in sub_dirs: |
| 39 | + if not subdir.exists(): |
| 40 | + subdir.mkdir() |
| 41 | + |
| 42 | + |
| 43 | +def create_app(): |
| 44 | + """Creates a Flask app with necessary configuration.""" |
| 45 | + app = Flask(__name__) |
| 46 | + app.secret_key = os.getenv('RENKU_SVC_SERVICE_KEY', uuid.uuid4().hex) |
| 47 | + |
| 48 | + app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER.name |
| 49 | + app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 |
| 50 | + |
| 51 | + make_cache() |
| 52 | + build_routes(app) |
| 53 | + |
| 54 | + return app |
| 55 | + |
| 56 | + |
| 57 | +def build_routes(app): |
| 58 | + """Register routes to given app instance.""" |
| 59 | + swaggerui_blueprint = get_swaggerui_blueprint( |
| 60 | + SWAGGER_URL, API_URL, config={'app_name': 'RenkuSvc'} |
| 61 | + ) |
| 62 | + |
| 63 | + app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL) |
| 64 | + |
| 65 | + app.add_url_rule( |
| 66 | + '/api/{0}/spec'.format(API_VERSION), |
| 67 | + 'docs', |
| 68 | + api_docs_view, |
| 69 | + methods=['GET'] |
| 70 | + ) |
| 71 | + |
| 72 | + app.add_url_rule( |
| 73 | + '/cache/files-list', |
| 74 | + 'list_files', |
| 75 | + list_uploaded_files_view, |
| 76 | + methods=['GET'] |
| 77 | + ) |
| 78 | + |
| 79 | + app.add_url_rule( |
| 80 | + '/cache/files-upload', |
| 81 | + 'upload_files', |
| 82 | + upload_files_view, |
| 83 | + methods=['POST'] |
| 84 | + ) |
| 85 | + |
| 86 | + app.add_url_rule( |
| 87 | + '/cache/project-clone', |
| 88 | + 'clone_project', |
| 89 | + clone_repository_view, |
| 90 | + methods=['GET'] |
| 91 | + ) |
| 92 | + |
| 93 | + app.add_url_rule( |
| 94 | + '/cache/project-list', |
| 95 | + 'list_projects', |
| 96 | + list_projects_view, |
| 97 | + methods=['GET'] |
| 98 | + ) |
| 99 | + |
| 100 | + app.add_url_rule( |
| 101 | + '/datasets/list', 'list_datasets', list_datasets_view, methods=['GET'] |
| 102 | + ) |
| 103 | + |
| 104 | + app.add_url_rule( |
| 105 | + '/datasets/files', |
| 106 | + 'list_dataset_files', |
| 107 | + list_dataset_files_view, |
| 108 | + methods=['GET'] |
| 109 | + ) |
| 110 | + |
| 111 | + app.add_url_rule( |
| 112 | + '/datasets/add', |
| 113 | + 'add_dataset_file', |
| 114 | + add_file_to_dataset_view, |
| 115 | + methods=['GET'] |
| 116 | + ) |
| 117 | + |
| 118 | + app.add_url_rule( |
| 119 | + '/datasets/create', |
| 120 | + 'create_data', |
| 121 | + create_dataset_view, |
| 122 | + methods=['GET'] |
| 123 | + ) |
| 124 | + |
| 125 | + |
| 126 | +app = create_app() |
| 127 | + |
| 128 | +if __name__ == '__main__': |
| 129 | + app.run() |
0 commit comments