source: src/mapservice/server_impl/__init__.py@ 7be94aa

debian/bullseye
Last change on this file since 7be94aa was 7be94aa, checked in by Joachim Langenbach <joachim.langenbach@…>, 7 years ago
  • Added yapf style config (src/python-style.yapf)
    • Updated all files to the yapf formatted version
  • Property mode set to 100644
File size: 2.9 KB
Line 
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
18import connexion
19import flask
20from flask_cors import CORS
21from flask_mail import Mail
22from sqlalchemy import event
23from celery import Celery
24from werkzeug.utils import import_string
25import os
26
27from .. import encoder
28from .database.Model import db, User, ApiKey, db_setup, load_spatialite
29from .ValidatorWorkaround import ValidatorWorkaround
30
31
32def 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
39config_file = '/etc/gnuboat/mapservice.conf'
40if 'FLASK_ENV' in os.environ and os.environ['FLASK_ENV']:
41 config_object = load_config_object(os.environ['FLASK_ENV'].title())
42elif os.path.isfile(config_file):
43 from . import Config as configure
44 config_object = configure.load_from_file(config_file)
45else:
46 config_object = load_config_object('Production')
47
48app = connexion.App(__name__, specification_dir='../openapi/')
49application = app.app
50CORS(application)
51mail = Mail()
52celery = Celery(__name__, broker=config_object.CELERY_BROKER_URL)
53
54
55@application.before_first_request
56def 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/")
69def index():
70 return render_template('index.html')
71
72
73def 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)
Note: See TracBrowser for help on using the repository browser.