|
| 1 | +# Copyright 2016, 2023 John J. Rofrano. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
1 | 15 | """ |
2 | | -Package: service |
3 | 16 | Package for the application models and service routes |
4 | | -This module creates and configures the Flask app and sets up the logging |
5 | | -and SQL database |
6 | 17 | """ |
7 | | -import sys |
8 | 18 | from flask import Flask |
| 19 | +from flask_redis import FlaskRedis |
9 | 20 | from service import config |
10 | | -from .common import log_handlers |
11 | | - |
12 | | -# Create Flask application |
13 | | -app = Flask(__name__) |
14 | | -app.config.from_object(config) |
15 | | - |
16 | | -# Dependencies require we import the routes AFTER the Flask app is created |
17 | | -# pylint: disable=wrong-import-position, wrong-import-order |
18 | | -from service import routes |
19 | | -# flake8: noqa: F401 |
20 | | -# pylint: disable=wrong-import-position |
21 | | -from .common import error_handlers |
22 | | -from .models import Counter |
23 | | - |
24 | | -# Set up logging for production |
25 | | -log_handlers.init_logging(app, "gunicorn.error") |
26 | | - |
27 | | -app.logger.info(70 * "*") |
28 | | -app.logger.info(" S E R V I C E R U N N I N G ".center(70, "*")) |
29 | | -app.logger.info(70 * "*") |
30 | | - |
31 | | -try: |
32 | | - Counter.init_db(app) # Initialize Redis |
33 | | -except Exception as error: # pylint: disable=broad-except |
34 | | - app.logger.critical("%s: Cannot continue", error) |
35 | | - # gunicorn requires exit code 4 to stop spawning workers when they die |
36 | | - sys.exit(4) |
37 | | - |
38 | | -app.logger.info("Service initialized!") |
| 21 | +from service.common import log_handlers |
| 22 | + |
| 23 | +# Globally accessible libraries |
| 24 | +# redis = FlaskRedis() |
| 25 | + |
| 26 | + |
| 27 | +############################################################ |
| 28 | +# Initialize the Flask instance |
| 29 | +############################################################ |
| 30 | +def init_app(): |
| 31 | + """Initialize the core application.""" |
| 32 | + app = Flask(__name__) |
| 33 | + app.config.from_object(config) |
| 34 | + |
| 35 | + # Initialize Plugins |
| 36 | + # redis.init_app(app) |
| 37 | + |
| 38 | + with app.app_context(): |
| 39 | + # Include our Routes |
| 40 | + |
| 41 | + # pylint: disable=import-outside-toplevel, unused-import |
| 42 | + from service import routes, models |
| 43 | + from service.common import error_handlers |
| 44 | + |
| 45 | + # Set up logging for production |
| 46 | + log_handlers.init_logging(app, "gunicorn.error") |
| 47 | + |
| 48 | + app.logger.info(70 * "*") |
| 49 | + app.logger.info(" H I T C O U N T E R S E R V I C E ".center(70, "*")) |
| 50 | + app.logger.info(70 * "*") |
| 51 | + |
| 52 | + app.logger.info("Service initialized!") |
| 53 | + |
| 54 | + # Initialize the database |
| 55 | + try: |
| 56 | + app.logger.info("Initializing the Redis database") |
| 57 | + models.Counter.connect(app.config['DATABASE_URI']) |
| 58 | + app.logger.info("Connected!") |
| 59 | + except models.DatabaseConnectionError as err: |
| 60 | + app.logger.error(str(err)) |
| 61 | + |
| 62 | + return app |
0 commit comments