| 1 | '''
|
|---|
| 2 | Copyright (C) 2019 - 2020 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| 3 |
|
|---|
| 4 | This program is free software: you can redistribute it and/or modify
|
|---|
| 5 | it under the terms of the GNU General Public License as published by
|
|---|
| 6 | the Free Software Foundation, either version 3 of the License, or
|
|---|
| 7 | (at your option) any later version.
|
|---|
| 8 |
|
|---|
| 9 | This program is distributed in the hope that it will be useful,
|
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | GNU General Public License for more details.
|
|---|
| 13 |
|
|---|
| 14 | You should have received a copy of the GNU General Public License
|
|---|
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 16 | '''
|
|---|
| 17 |
|
|---|
| 18 | import connexion
|
|---|
| 19 | import flask
|
|---|
| 20 | from flask_cors import CORS
|
|---|
| 21 | from flask_mail import Mail
|
|---|
| 22 | from sqlalchemy import event
|
|---|
| 23 | from celery import Celery
|
|---|
| 24 | from werkzeug.utils import import_string
|
|---|
| 25 | import os
|
|---|
| 26 |
|
|---|
| 27 | from .. import encoder
|
|---|
| 28 | from .database.Model import db, User, ApiKey, db_setup, load_spatialite
|
|---|
| 29 | from .ValidatorWorkaround import ValidatorWorkaround
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 | def load_config_object(config_name):
|
|---|
| 33 | return import_string(
|
|---|
| 34 | 'gnuboat_mapservice.server_impl.Config.{}Config'.format(config_name)
|
|---|
| 35 | )
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | # determine requested configuration
|
|---|
| 39 | config_file = '/etc/gnuboat/mapservice.conf'
|
|---|
| 40 | if 'FLASK_ENV' in os.environ and os.environ['FLASK_ENV']:
|
|---|
| 41 | config_object = load_config_object(os.environ['FLASK_ENV'].title())
|
|---|
| 42 | elif os.path.isfile(config_file):
|
|---|
| 43 | from . import Config as configure
|
|---|
| 44 | config_object = configure.load_from_file(config_file)
|
|---|
| 45 | else:
|
|---|
| 46 | config_object = load_config_object('Production')
|
|---|
| 47 |
|
|---|
| 48 | app = connexion.App(__name__, specification_dir='../openapi/')
|
|---|
| 49 | application = app.app
|
|---|
| 50 | CORS(application)
|
|---|
| 51 | mail = Mail()
|
|---|
| 52 | celery = Celery(__name__, broker=config_object.CELERY_BROKER_URL)
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 | @application.before_first_request
|
|---|
| 56 | def create_database():
|
|---|
| 57 | @event.listens_for(db.engine, "connect")
|
|---|
| 58 | def load_spatialite_extension(dbapi_conn, connection_record):
|
|---|
| 59 | load_spatialite(dbapi_conn, connection_record)
|
|---|
| 60 |
|
|---|
| 61 | if flask.current_app.config['CREATE_TEST_DATABASE'] == True:
|
|---|
| 62 | from .demo import DbSetup as demoDbSetup
|
|---|
| 63 | demoDbSetup.setup()
|
|---|
| 64 | else:
|
|---|
| 65 | db_setup()
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 | @application.route("/mapservice/")
|
|---|
| 69 | def index():
|
|---|
| 70 | return render_template('index.html')
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 | def setup_app(app_name, config, template_folder=None):
|
|---|
| 74 | application.json_encoder = encoder.JSONEncoder
|
|---|
| 75 | app.add_api(
|
|---|
| 76 | 'openapi.yaml',
|
|---|
| 77 | arguments={'title': app_name},
|
|---|
| 78 | validate_responses=True,
|
|---|
| 79 | strict_validation=True,
|
|---|
| 80 | validator_map={'response': ValidatorWorkaround}
|
|---|
| 81 | )
|
|---|
| 82 | application.config.from_object(config)
|
|---|
| 83 |
|
|---|
| 84 | if 'TEMPLATE_FOLDER' in application.config:
|
|---|
| 85 | application.template_folder = application.config['TEMPLATE_FOLDER']
|
|---|
| 86 | elif template_folder:
|
|---|
| 87 | application.template_folder = template_folder
|
|---|
| 88 |
|
|---|
| 89 | db.init_app(application)
|
|---|
| 90 | mail.init_app(application)
|
|---|
| 91 | celery.config_from_object(config)
|
|---|