|
30 | 30 | # The Flask app must be created |
31 | 31 | # BEFORE you import modules that depend on it !!! |
32 | 32 |
|
33 | | -# Create the Flask app |
34 | | -app = Flask(__name__) |
35 | | -app.config.from_object(config) |
36 | | - |
37 | | -# Turn off strict slashes because it violates best practices |
38 | | -app.url_map.strict_slashes = False |
39 | | - |
40 | 33 | # Document the type of authorization required |
41 | 34 | authorizations = { |
42 | 35 | "apikey": { |
43 | | - "type": "apiKey", |
44 | | - "in": "header", |
| 36 | + "type": "apiKey", |
| 37 | + "in": "header", |
45 | 38 | "name": "X-Api-Key" |
46 | 39 | } |
47 | 40 | } |
48 | 41 |
|
49 | | -###################################################################### |
50 | | -# Configure Swagger before initializing it |
51 | | -###################################################################### |
52 | | -api = Api( |
53 | | - app, |
54 | | - version="1.0.0", |
55 | | - title="Pet Demo REST API Service", |
56 | | - description="This is a sample server Pet store server.", |
57 | | - default="pets", |
58 | | - default_label="Pet shop operations", |
59 | | - doc="/apidocs", # default also could use doc='/apidocs/' |
60 | | - authorizations=authorizations, |
61 | | - prefix="/api", |
62 | | -) |
63 | | - |
64 | | - |
65 | | -# Import the routes After the Flask app is created |
66 | | -# pylint: disable=wrong-import-position, wrong-import-order, cyclic-import |
67 | | -from service import routes, models # noqa: F401, E402 |
68 | | -from service.common import error_handlers |
69 | | - |
70 | | -# Set up logging for production |
71 | | -log_handlers.init_logging(app, "gunicorn.error") |
72 | | - |
73 | | -app.logger.info(70 * "*") |
74 | | -app.logger.info(" P E T S E R V I C E R U N N I N G ".center(70, "*")) |
75 | | -app.logger.info(70 * "*") |
76 | | - |
77 | | -app.logger.info("Service initialized!") |
78 | | - |
79 | | -# If an API Key was not provided, autogenerate one |
80 | | -if not app.config["API_KEY"]: |
81 | | - app.config["API_KEY"] = routes.generate_apikey() |
82 | | - app.logger.info("Missing API Key! Autogenerated: %s", app.config["API_KEY"]) |
83 | | - |
84 | | -try: |
85 | | - models.Pet.init_db(app.config["CLOUDANT_DBNAME"]) |
86 | | -except Exception as error: # pylint: disable=broad-except |
87 | | - app.logger.critical("%s: Cannot continue", error) |
88 | | - # gunicorn requires exit code 4 to stop spawning workers when they die |
89 | | - sys.exit(4) |
| 42 | +# Will be initialize when app is created |
| 43 | +api = None # pylint: disable=invalid-name |
| 44 | + |
| 45 | + |
| 46 | +############################################################ |
| 47 | +# Initialize the Flask instance |
| 48 | +############################################################ |
| 49 | +def create_app(): |
| 50 | + """Initialize the core application.""" |
| 51 | + |
| 52 | + # Create the Flask app |
| 53 | + app = Flask(__name__) |
| 54 | + app.config.from_object(config) |
| 55 | + |
| 56 | + # Turn off strict slashes because it violates best practices |
| 57 | + app.url_map.strict_slashes = False |
| 58 | + |
| 59 | + ###################################################################### |
| 60 | + # Configure Swagger before initializing it |
| 61 | + ###################################################################### |
| 62 | + global api |
| 63 | + api = Api( |
| 64 | + app, |
| 65 | + version="1.0.0", |
| 66 | + title="Pet Demo REST API Service", |
| 67 | + description="This is a sample server Pet store server.", |
| 68 | + default="pets", |
| 69 | + default_label="Pet shop operations", |
| 70 | + doc="/apidocs", # default also could use doc='/apidocs/' |
| 71 | + authorizations=authorizations, |
| 72 | + prefix="/api", |
| 73 | + ) |
| 74 | + |
| 75 | + with app.app_context(): |
| 76 | + # Import the routes After the Flask app is created |
| 77 | + # pylint: disable=import-outside-toplevel |
| 78 | + from service import routes, models # noqa: F401, E402 |
| 79 | + from service.common import error_handlers # pylint: disable=unused-import |
| 80 | + |
| 81 | + try: |
| 82 | + models.Pet.init_db(app.config["CLOUDANT_DBNAME"]) |
| 83 | + except Exception as error: # pylint: disable=broad-except |
| 84 | + app.logger.critical("%s: Cannot continue", error) |
| 85 | + # gunicorn requires exit code 4 to stop spawning workers when they die |
| 86 | + sys.exit(4) |
| 87 | + |
| 88 | + # Set up logging for production |
| 89 | + log_handlers.init_logging(app, "gunicorn.error") |
| 90 | + |
| 91 | + app.logger.info(70 * "*") |
| 92 | + app.logger.info(" P E T S E R V I C E R U N N I N G ".center(70, "*")) |
| 93 | + app.logger.info(70 * "*") |
| 94 | + |
| 95 | + # If an API Key was not provided, autogenerate one |
| 96 | + if not app.config["API_KEY"]: |
| 97 | + app.config["API_KEY"] = routes.generate_apikey() |
| 98 | + app.logger.info("Missing API Key! Autogenerated: %s", app.config["API_KEY"]) |
| 99 | + |
| 100 | + app.logger.info("Service initialized!") |
| 101 | + |
| 102 | + return app |
0 commit comments