add single entrypoint to server to simplify deployment

This commit is contained in:
Yeicor
2024-02-17 10:38:50 +01:00
parent 7f7ff86f9f
commit 92c917b230
3 changed files with 25 additions and 4 deletions

1
src/misc/network.ts Normal file
View File

@@ -0,0 +1 @@

View File

@@ -1,7 +1,13 @@
// These are the default values for the settings, which are overridden below // These are the default values for the settings, which are overridden below
export const settings = { export const settings = {
preloadModels: [
// @ts-ignore // @ts-ignore
preloadModels: [new URL('../../assets/fox.glb', import.meta.url).href, new URL('../../assets/logo.glbs', import.meta.url).href, "ws://localhost:8080"], new URL('../../assets/fox.glb', import.meta.url).href,
// @ts-ignore
new URL('../../assets/logo.glbs', import.meta.url).href,
// Websocket URLs automatically listen for new models from the python backend
"ws://localhost:8080/"
],
// ModelViewer settings // ModelViewer settings
autoplay: true, autoplay: true,
arModes: 'webxr scene-viewer quick-look', arModes: 'webxr scene-viewer quick-look',

View File

@@ -57,6 +57,8 @@ class Server:
# - APIs # - APIs
self.app.router.add_route('GET', f'{UPDATES_API_PATH}', self._api_updates) self.app.router.add_route('GET', f'{UPDATES_API_PATH}', self._api_updates)
self.app.router.add_route('GET', f'{OBJECTS_API_PATH}/{{name}}', self._api_object) self.app.router.add_route('GET', f'{OBJECTS_API_PATH}/{{name}}', self._api_object)
# - Single websocket/objects/frontend entrypoint to ease client configuration
self.app.router.add_get('/', self._entrypoint)
# - Static files from the frontend # - Static files from the frontend
self.app.router.add_get('/{path:(.*/|)}', _index_handler) # Any folder -> index.html self.app.router.add_get('/{path:(.*/|)}', _index_handler) # Any folder -> index.html
self.app.router.add_static('/', path=FRONTEND_BASE_PATH, name='static_frontend') self.app.router.add_static('/', path=FRONTEND_BASE_PATH, name='static_frontend')
@@ -104,6 +106,16 @@ class Server:
# print('Shutting down server...') # print('Shutting down server...')
await runner.cleanup() await runner.cleanup()
async def _entrypoint(self, request: web.Request) -> web.StreamResponse:
"""Main entrypoint to the server, which automatically serves the frontend/updates/objects"""
if request.headers.get('Upgrade', '').lower() == 'websocket': # WebSocket -> updates API
return await self._api_updates(request)
elif request.query.get('api_object', '') != '': # ?api_object={name} -> object API
request.match_info['name'] = request.query['api_object']
return await self._api_object(request)
else: # Anything else -> frontend index.html
return await _index_handler(request)
async def _api_updates(self, request: web.Request) -> web.WebSocketResponse: async def _api_updates(self, request: web.Request) -> web.WebSocketResponse:
"""Handles a publish-only websocket connection that send show_object events along with their hashes and URLs""" """Handles a publish-only websocket connection that send show_object events along with their hashes and URLs"""
ws = web.WebSocketResponse() ws = web.WebSocketResponse()
@@ -201,8 +213,10 @@ class Server:
with logging_redirect_tqdm(tqdm_class=tqdm.asyncio.tqdm): with logging_redirect_tqdm(tqdm_class=tqdm.asyncio.tqdm):
if logger.isEnabledFor(logging.INFO): if logger.isEnabledFor(logging.INFO):
# noinspection PyTypeChecker # noinspection PyTypeChecker
export_data = tqdm.asyncio.tqdm(export_data, total=total_parts) export_data_iter = tqdm.asyncio.tqdm(export_data, total=total_parts)
async for chunk in glb_sequence_to_glbs(export_data, total_parts): else:
export_data_iter = export_data
async for chunk in glb_sequence_to_glbs(export_data_iter, total_parts):
await response.write(chunk) await response.write(chunk)
finally: finally:
# Close the export data subscription # Close the export data subscription