Added quick create_app function
This commit is contained in:
parent
829c58d3fb
commit
79abc3fee3
3 changed files with 50 additions and 21 deletions
|
|
@ -11,7 +11,6 @@ from .spec import view2path
|
|||
from .utilities import description_from_view
|
||||
|
||||
from openflexure_microscope.common.labthings_core.utilities import get_docstring
|
||||
from .exceptions import JSONExceptionHandler
|
||||
|
||||
from . import EXTENSION_NAME
|
||||
|
||||
|
|
@ -26,7 +25,6 @@ class LabThing(object):
|
|||
title: str = "",
|
||||
description: str = "",
|
||||
version: str = "0.0.0",
|
||||
handle_errors: bool = True,
|
||||
):
|
||||
self.app = app
|
||||
|
||||
|
|
@ -45,10 +43,8 @@ class LabThing(object):
|
|||
self._title = title
|
||||
self._version = version
|
||||
|
||||
if handle_errors:
|
||||
self.error_handler = JSONExceptionHandler()
|
||||
else:
|
||||
self.error_handler = None
|
||||
# Store handlers for things like errors and CORS
|
||||
self.handlers = {}
|
||||
|
||||
self.spec = APISpec(
|
||||
title=self.title,
|
||||
|
|
@ -96,10 +92,6 @@ class LabThing(object):
|
|||
app.extensions = getattr(app, "extensions", {})
|
||||
app.extensions[EXTENSION_NAME] = self
|
||||
|
||||
# Register error handler if one exists
|
||||
if self.error_handler:
|
||||
self.error_handler.init_app(self.app)
|
||||
|
||||
# Add resources, if registered before tying to a Flask app
|
||||
if len(self.resources) > 0:
|
||||
for resource, urls, endpoint, kwargs in self.resources:
|
||||
|
|
|
|||
41
openflexure_microscope/common/flask_labthings/quick.py
Normal file
41
openflexure_microscope/common/flask_labthings/quick.py
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue