| 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 | def load_config_object(config_name):
|
|---|
| 32 | return import_string('gnuboat_mapservice.server_impl.Config.{}Config'.format(config_name))
|
|---|
| 33 |
|
|---|
| 34 | # determine requested configuration
|
|---|
| 35 | config_file = '/etc/gnuboat/mapservice.conf'
|
|---|
| 36 | if 'FLASK_ENV' in os.environ and os.environ['FLASK_ENV']:
|
|---|
| 37 | config_object = load_config_object(os.environ['FLASK_ENV'].title())
|
|---|
| 38 | elif os.path.isfile(config_file):
|
|---|
| 39 | from . import Config as configure
|
|---|
| 40 | config_object = configure.load_from_file(config_file)
|
|---|
| 41 | else:
|
|---|
| 42 | config_object = load_config_object('Production')
|
|---|
| 43 |
|
|---|
| 44 | app = connexion.App(__name__, specification_dir='../openapi/')
|
|---|
| 45 | application = app.app
|
|---|
| 46 | CORS(application)
|
|---|
| 47 | mail = Mail()
|
|---|
| 48 | celery = Celery(__name__, broker=config_object.CELERY_BROKER_URL)
|
|---|
| 49 |
|
|---|
| 50 | @application.before_first_request
|
|---|
| 51 | def create_database():
|
|---|
| 52 | @event.listens_for(db.engine, "connect")
|
|---|
| 53 | def load_spatialite_extension(dbapi_conn, connection_record):
|
|---|
| 54 | load_spatialite(dbapi_conn, connection_record)
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | if flask.current_app.config['CREATE_TEST_DATABASE'] == True:
|
|---|
| 58 | from .demo import DbSetup as demoDbSetup
|
|---|
| 59 | demoDbSetup.setup()
|
|---|
| 60 | else:
|
|---|
| 61 | db_setup()
|
|---|
| 62 |
|
|---|
| 63 | @application.route("/mapservice/")
|
|---|
| 64 | def index():
|
|---|
| 65 | return render_template('index.html')
|
|---|
| 66 |
|
|---|
| 67 | def setup_app(app_name, config):
|
|---|
| 68 | application.json_encoder = encoder.JSONEncoder
|
|---|
| 69 | app.add_api('openapi.yaml', arguments={'title': app_name}, validate_responses=True, strict_validation=True, validator_map={'response': ValidatorWorkaround})
|
|---|
| 70 | application.config.from_object(config)
|
|---|
| 71 |
|
|---|
| 72 | db.init_app(application)
|
|---|
| 73 | mail.init_app(application)
|
|---|
| 74 | celery.config_from_object(config)
|
|---|