Added quick create_app function

This commit is contained in:
jtc42 2020-01-06 15:09:29 +00:00
parent 829c58d3fb
commit 79abc3fee3
3 changed files with 50 additions and 21 deletions

View file

@ -0,0 +1,41 @@
from flask import Flask
from flask_cors import CORS
from .labthing import LabThing
from .exceptions import JSONExceptionHandler
def create_app(
import_name,
prefix: str = "",
title: str = "",
description: str = "",
version: str = "0.0.0",
handle_errors: bool = True,
handle_cors: bool = True,
flask_kwargs: dict = {},
):
app = Flask(import_name, **flask_kwargs)
app.url_map.strict_slashes = False
# Handle CORS
if handle_cors:
cors_handler = CORS(app, resources=r"{prefix}/*")
# Handle errors
if handle_errors:
error_handler = JSONExceptionHandler()
error_handler.init_app(app)
# Create a LabThing
labthing = LabThing(
app, prefix=prefix, title=title, description=description, version=version
)
# Store references to added-in handlers
if cors_handler:
labthing.handlers["cors"] = cors_handler
if error_handler:
labthing.handlers["error"] = error_handler
return app, labthing