source: src/mapservice/server_impl/__init__.py@ f61d16d

debian/bullseye
Last change on this file since f61d16d was 5c9c247, checked in by Joachim Langenbach <joachim.langenbach@…>, 7 years ago

To find the template files, when started per installed package and per module from src-gen

  • Folder for templates is now set (in the descending priority)
    • per Config option TEMPLATE_FOLDER
    • per template_folder optional argument to setup_app(). The option is set by main.py (see mustache)

For tests the template_folder is set by init.py from test folder (s. inittest.mustache)

  • Property mode set to 100644
File size: 2.8 KB
RevLine 
[a88607b]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
[5ec7c42]28from .database.Model import db, User, ApiKey, db_setup, load_spatialite
[a88607b]29from .ValidatorWorkaround import ValidatorWorkaround
30
[096625e]31def load_config_object(config_name):
[adda9f2]32 return import_string('gnuboat_mapservice.server_impl.Config.{}Config'.format(config_name))
[096625e]33
[a88607b]34# determine requested configuration
[096625e]35config_file = '/etc/gnuboat/mapservice.conf'
[a88607b]36if 'FLASK_ENV' in os.environ and os.environ['FLASK_ENV']:
[096625e]37 config_object = load_config_object(os.environ['FLASK_ENV'].title())
38elif os.path.isfile(config_file):
39 from . import Config as configure
40 config_object = configure.load_from_file(config_file)
[a88607b]41else:
[096625e]42 config_object = load_config_object('Production')
[a88607b]43
44app = connexion.App(__name__, specification_dir='../openapi/')
45application = app.app
46CORS(application)
47mail = Mail()
[096625e]48celery = Celery(__name__, broker=config_object.CELERY_BROKER_URL)
[a88607b]49
50@application.before_first_request
51def create_database():
52 @event.listens_for(db.engine, "connect")
[5ec7c42]53 def load_spatialite_extension(dbapi_conn, connection_record):
54 load_spatialite(dbapi_conn, connection_record)
[ae2ff15]55
[a88607b]56
57 if flask.current_app.config['CREATE_TEST_DATABASE'] == True:
[186affa]58 from .demo import DbSetup as demoDbSetup
59 demoDbSetup.setup()
[a88607b]60 else:
61 db_setup()
62
63@application.route("/mapservice/")
64def index():
65 return render_template('index.html')
66
[5c9c247]67def setup_app(app_name, config, template_folder = None):
[a88607b]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)
[5c9c247]71
72 if 'TEMPLATE_FOLDER' in application.config:
73 application.template_folder = application.config['TEMPLATE_FOLDER']
74 elif template_folder:
75 application.template_folder = template_folder
[a88607b]76
77 db.init_app(application)
78 mail.init_app(application)
79 celery.config_from_object(config)
Note: See TracBrowser for help on using the repository browser.