Added quick create_app function
This commit is contained in:
parent
829c58d3fb
commit
79abc3fee3
3 changed files with 50 additions and 21 deletions
|
|
@ -19,7 +19,7 @@ from openflexure_microscope.config import (
|
||||||
USER_EXTENSIONS_PATH,
|
USER_EXTENSIONS_PATH,
|
||||||
)
|
)
|
||||||
|
|
||||||
from openflexure_microscope.common.flask_labthings.labthing import LabThing
|
from openflexure_microscope.common.flask_labthings.quick import create_app
|
||||||
from openflexure_microscope.common.flask_labthings.extensions import find_extensions
|
from openflexure_microscope.common.flask_labthings.extensions import find_extensions
|
||||||
|
|
||||||
from openflexure_microscope.api.microscope import default_microscope as api_microscope
|
from openflexure_microscope.api.microscope import default_microscope as api_microscope
|
||||||
|
|
@ -56,20 +56,16 @@ else:
|
||||||
|
|
||||||
|
|
||||||
# Create flask app
|
# Create flask app
|
||||||
app = Flask(__name__)
|
app, labthing = create_app(
|
||||||
app.url_map.strict_slashes = False
|
__name__,
|
||||||
|
prefix="/api/v2",
|
||||||
|
description="Test LabThing-based API for OpenFlexure Microscope",
|
||||||
|
title=f"OpenFlexure Microscope {api_microscope.name}",
|
||||||
|
)
|
||||||
|
|
||||||
# Use custom JSON encoder
|
# Use custom JSON encoder
|
||||||
app.json_encoder = JSONEncoder
|
app.json_encoder = JSONEncoder
|
||||||
|
|
||||||
# Enable CORS everywhere
|
|
||||||
CORS(app, resources=r"*")
|
|
||||||
|
|
||||||
# Build a labthing
|
|
||||||
labthing = LabThing(app, prefix="/api/v2")
|
|
||||||
labthing.description = "Test LabThing-based API for OpenFlexure Microscope"
|
|
||||||
labthing.title = f"OpenFlexure Microscope {api_microscope.name}"
|
|
||||||
|
|
||||||
# Attach lab devices
|
# Attach lab devices
|
||||||
labthing.register_device(api_microscope, "openflexure_microscope")
|
labthing.register_device(api_microscope, "openflexure_microscope")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@ from .spec import view2path
|
||||||
from .utilities import description_from_view
|
from .utilities import description_from_view
|
||||||
|
|
||||||
from openflexure_microscope.common.labthings_core.utilities import get_docstring
|
from openflexure_microscope.common.labthings_core.utilities import get_docstring
|
||||||
from .exceptions import JSONExceptionHandler
|
|
||||||
|
|
||||||
from . import EXTENSION_NAME
|
from . import EXTENSION_NAME
|
||||||
|
|
||||||
|
|
@ -26,7 +25,6 @@ class LabThing(object):
|
||||||
title: str = "",
|
title: str = "",
|
||||||
description: str = "",
|
description: str = "",
|
||||||
version: str = "0.0.0",
|
version: str = "0.0.0",
|
||||||
handle_errors: bool = True,
|
|
||||||
):
|
):
|
||||||
self.app = app
|
self.app = app
|
||||||
|
|
||||||
|
|
@ -45,10 +43,8 @@ class LabThing(object):
|
||||||
self._title = title
|
self._title = title
|
||||||
self._version = version
|
self._version = version
|
||||||
|
|
||||||
if handle_errors:
|
# Store handlers for things like errors and CORS
|
||||||
self.error_handler = JSONExceptionHandler()
|
self.handlers = {}
|
||||||
else:
|
|
||||||
self.error_handler = None
|
|
||||||
|
|
||||||
self.spec = APISpec(
|
self.spec = APISpec(
|
||||||
title=self.title,
|
title=self.title,
|
||||||
|
|
@ -96,10 +92,6 @@ class LabThing(object):
|
||||||
app.extensions = getattr(app, "extensions", {})
|
app.extensions = getattr(app, "extensions", {})
|
||||||
app.extensions[EXTENSION_NAME] = self
|
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
|
# Add resources, if registered before tying to a Flask app
|
||||||
if len(self.resources) > 0:
|
if len(self.resources) > 0:
|
||||||
for resource, urls, endpoint, kwargs in self.resources:
|
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