Merge branch 'app-cleanup' into 'master'

Cleaned up main app setup

See merge request openflexure/openflexure-microscope-server!84
This commit is contained in:
Joel Collins 2020-11-17 16:58:43 +00:00
commit 427d5b0093
2 changed files with 16 additions and 22 deletions

View file

@ -29,7 +29,7 @@ from labthings import create_app
from labthings.extensions import find_extensions from labthings.extensions import find_extensions
from labthings.views import View from labthings.views import View
from openflexure_microscope.api.microscope import default_microscope as api_microscope from openflexure_microscope.microscope import Microscope
from openflexure_microscope.api.utilities import init_default_extensions, list_routes from openflexure_microscope.api.utilities import init_default_extensions, list_routes
from openflexure_microscope.api.v2 import views from openflexure_microscope.api.v2 import views
from openflexure_microscope.json import JSONEncoder from openflexure_microscope.json import JSONEncoder
@ -39,11 +39,7 @@ from openflexure_microscope.paths import (
logs_file_path, logs_file_path,
) )
# Handle logging # Log files
access_log = logging.getLogger("werkzeug")
# Block the access logs from propagating up to the root logger
access_log.propagate = False
ROOT_LOGFILE = logs_file_path("openflexure_microscope.log") ROOT_LOGFILE = logs_file_path("openflexure_microscope.log")
ACCESS_LOGFILE = logs_file_path("openflexure_microscope.access.log") ACCESS_LOGFILE = logs_file_path("openflexure_microscope.access.log")
@ -52,8 +48,12 @@ formatter = logging.Formatter(
"[%(asctime)s] [%(threadName)s] [%(levelname)s] %(message)s" "[%(asctime)s] [%(threadName)s] [%(levelname)s] %(message)s"
) )
# Our WSGI server uses Werkzeug, so use that for the access log
access_log = logging.getLogger("werkzeug")
# Block the access logs from propagating up to the root logger
access_log.propagate = False
# Create file handler # Create error log file handler
fh = logging.handlers.RotatingFileHandler( fh = logging.handlers.RotatingFileHandler(
ROOT_LOGFILE, maxBytes=1_000_000, backupCount=5 ROOT_LOGFILE, maxBytes=1_000_000, backupCount=5
) )
@ -76,8 +76,14 @@ access_log.addHandler(afh)
# Log server paths being used # Log server paths being used
logging.info("Running with data path %s", OPENFLEXURE_VAR_PATH) logging.info("Running with data path %s", OPENFLEXURE_VAR_PATH)
logging.info("Creating app") # Create the microscope object
api_microscope = Microscope()
logging.debug("Restoring captures...")
api_microscope.captures.rebuild_captures()
logging.debug("Microscope successfully attached!")
# Create flask app # Create flask app
logging.info("Creating app")
app, labthing = create_app( app, labthing = create_app(
__name__, __name__,
prefix="/api/v2", prefix="/api/v2",
@ -95,7 +101,7 @@ cors = CORS(app)
labthing.json_encoder = JSONEncoder labthing.json_encoder = JSONEncoder
app.json_encoder = JSONEncoder app.json_encoder = JSONEncoder
# Attach lab devices # Add the microscope object to LabThings so extensions can access it
labthing.add_component(api_microscope, "org.openflexure.microscope") labthing.add_component(api_microscope, "org.openflexure.microscope")
# Attach extensions # Attach extensions
@ -129,7 +135,6 @@ labthing.add_root_link(views.ConfigurationProperty, "instrumentConfiguration")
# Attach stage resources # Attach stage resources
labthing.add_view(views.StageTypeProperty, "/instrument/stage/type") labthing.add_view(views.StageTypeProperty, "/instrument/stage/type")
# Attach streams resources # Attach streams resources
labthing.add_view(views.MjpegStream, "/streams/mjpeg") labthing.add_view(views.MjpegStream, "/streams/mjpeg")
labthing.add_view(views.SnapshotStream, "/streams/snapshot") labthing.add_view(views.SnapshotStream, "/streams/snapshot")
@ -142,7 +147,7 @@ for name, action in views.enabled_root_actions().items():
rule = action["rule"] rule = action["rule"]
labthing.add_view(view_class, f"/actions{rule}") labthing.add_view(view_class, f"/actions{rule}")
# Add log file view # Add log file download view
class LogFileView(View): class LogFileView(View):
def get(self): def get(self):
""" """

View file

@ -1,11 +0,0 @@
import logging
from openflexure_microscope.microscope import Microscope
default_microscope = Microscope()
# Restore loaded capture array to camera object
logging.debug("Restoring captures...")
default_microscope.captures.rebuild_captures()
logging.debug("Microscope successfully attached!")