app: Simplify request handlers

There is no need to pass the auth, server, or websocket manager objects to request handlers.  They can be retreived directly from the application reference.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2020-11-10 07:44:35 -05:00
parent 7abc86847e
commit 92d1715d88
3 changed files with 29 additions and 27 deletions

View File

@ -114,10 +114,8 @@ class MoonrakerApp:
self.mutable_router = MutableRouter(self) self.mutable_router = MutableRouter(self)
app_handlers = [ app_handlers = [
(AnyMatches(), self.mutable_router), (AnyMatches(), self.mutable_router),
(r"/websocket", WebSocket, (r"/websocket", WebSocket, {'main_app': self}),
{'wsm': self.wsm, 'auth': self.auth}), (r"/api/version", EmulateOctoprintHandler, {'main_app': self})]
(r"/api/version", EmulateOctoprintHandler,
{'server': self.server, 'auth': self.auth})]
self.app = tornado.web.Application( self.app = tornado.web.Application(
app_handlers, app_handlers,
@ -137,6 +135,15 @@ class MoonrakerApp:
port, address=host, max_body_size=self.max_upload_size, port, address=host, max_body_size=self.max_upload_size,
xheaders=True) xheaders=True)
def get_server(self):
return self.server
def get_auth(self):
return self.auth
def get_websocket_manager(self):
return self.wsm
async def close(self): async def close(self):
if self.tornado_server is not None: if self.tornado_server is not None:
self.tornado_server.stop() self.tornado_server.stop()
@ -156,8 +163,7 @@ class MoonrakerApp:
f"Websocket: {', '.join(api_def.ws_methods)}") f"Websocket: {', '.join(api_def.ws_methods)}")
self.wsm.register_remote_handler(api_def) self.wsm.register_remote_handler(api_def)
params = {} params = {}
params['server'] = self.server params['main_app'] = self
params['auth'] = self.auth
params['arg_parser'] = api_def.parser params['arg_parser'] = api_def.parser
params['remote_callback'] = api_def.endpoint params['remote_callback'] = api_def.endpoint
self.mutable_router.add_handler( self.mutable_router.add_handler(
@ -174,8 +180,7 @@ class MoonrakerApp:
if "http" in protocol: if "http" in protocol:
msg += f" - HTTP: ({' '.join(request_methods)}) {uri}" msg += f" - HTTP: ({' '.join(request_methods)}) {uri}"
params = {} params = {}
params['server'] = self.server params['main_app'] = self
params['auth'] = self.auth
params['methods'] = request_methods params['methods'] = request_methods
params['arg_parser'] = api_def.parser params['arg_parser'] = api_def.parser
params['callback'] = callback params['callback'] = callback
@ -199,11 +204,11 @@ class MoonrakerApp:
logging.info(f"Invalid file path: {file_path}") logging.info(f"Invalid file path: {file_path}")
return return
logging.debug(f"Registering static file: ({pattern}) {file_path}") logging.debug(f"Registering static file: ({pattern}) {file_path}")
params = {'server': self.server, 'auth': self.auth, 'path': file_path} params = {'main_app': self, 'path': file_path}
self.mutable_router.add_handler(pattern, FileRequestHandler, params) self.mutable_router.add_handler(pattern, FileRequestHandler, params)
def register_upload_handler(self, pattern): def register_upload_handler(self, pattern):
params = {'server': self.server, 'auth': self.auth} params = {'main_app': self}
self.mutable_router.add_handler(pattern, FileUploadHandler, params) self.mutable_router.add_handler(pattern, FileUploadHandler, params)
def remove_handler(self, endpoint): def remove_handler(self, endpoint):
@ -253,8 +258,8 @@ class MoonrakerApp:
# ***** Dynamic Handlers***** # ***** Dynamic Handlers*****
class RemoteRequestHandler(AuthorizedRequestHandler): class RemoteRequestHandler(AuthorizedRequestHandler):
def initialize(self, remote_callback, server, auth, arg_parser): def initialize(self, main_app, remote_callback, arg_parser):
super(RemoteRequestHandler, self).initialize(server, auth) super(RemoteRequestHandler, self).initialize(main_app)
self.remote_callback = remote_callback self.remote_callback = remote_callback
self.query_parser = arg_parser self.query_parser = arg_parser
@ -277,9 +282,8 @@ class RemoteRequestHandler(AuthorizedRequestHandler):
self.finish({'result': result}) self.finish({'result': result})
class LocalRequestHandler(AuthorizedRequestHandler): class LocalRequestHandler(AuthorizedRequestHandler):
def initialize(self, callback, server, auth, def initialize(self, main_app, callback, methods, arg_parser):
methods, arg_parser): super(LocalRequestHandler, self).initialize(main_app)
super(LocalRequestHandler, self).initialize(server, auth)
self.callback = callback self.callback = callback
self.methods = methods self.methods = methods
self.query_parser = arg_parser self.query_parser = arg_parser
@ -339,9 +343,6 @@ class FileRequestHandler(AuthorizedFileHandler):
self.finish({'result': filename}) self.finish({'result': filename})
class FileUploadHandler(AuthorizedRequestHandler): class FileUploadHandler(AuthorizedRequestHandler):
def initialize(self, server, auth):
super(FileUploadHandler, self).initialize(server, auth)
async def post(self): async def post(self):
file_manager = self.server.lookup_plugin('file_manager') file_manager = self.server.lookup_plugin('file_manager')
try: try:

View File

@ -180,9 +180,10 @@ class Authorization:
self.prune_handler.stop() self.prune_handler.stop()
class AuthorizedRequestHandler(tornado.web.RequestHandler): class AuthorizedRequestHandler(tornado.web.RequestHandler):
def initialize(self, server, auth): def initialize(self, main_app):
self.server = server self.server = main_app.get_server()
self.auth = auth self.auth = main_app.get_auth()
self.wsm = main_app.get_websocket_manager()
def prepare(self): def prepare(self):
if not self.auth.check_authorized(self.request): if not self.auth.check_authorized(self.request):
@ -210,10 +211,10 @@ class AuthorizedRequestHandler(tornado.web.RequestHandler):
# Due to the way Python treats multiple inheritance its best # Due to the way Python treats multiple inheritance its best
# to create a separate authorized handler for serving files # to create a separate authorized handler for serving files
class AuthorizedFileHandler(tornado.web.StaticFileHandler): class AuthorizedFileHandler(tornado.web.StaticFileHandler):
def initialize(self, server, auth, path, default_filename=None): def initialize(self, main_app, path, default_filename=None):
super(AuthorizedFileHandler, self).initialize(path, default_filename) super(AuthorizedFileHandler, self).initialize(path, default_filename)
self.server = server self.server = main_app.get_server()
self.auth = auth self.auth = main_app.get_auth()
def prepare(self): def prepare(self):
if not self.auth.check_authorized(self.request): if not self.auth.check_authorized(self.request):

View File

@ -268,9 +268,9 @@ class WebsocketManager:
self.websockets = {} self.websockets = {}
class WebSocket(WebSocketHandler): class WebSocket(WebSocketHandler):
def initialize(self, wsm, auth): def initialize(self, main_app):
self.wsm = wsm self.auth = main_app.get_auth()
self.auth = auth self.wsm = main_app.get_websocket_manager()
self.rpc = self.wsm.rpc self.rpc = self.wsm.rpc
self.uid = id(self) self.uid = id(self)