Skip to content

Commit

Permalink
server.legacy: handler forwards reqest to service endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
deeenes committed Jan 28, 2025
1 parent 49735ad commit 20ab7c6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
15 changes: 12 additions & 3 deletions omnipath_server/server/_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# https://www.gnu.org/licenses/gpl-3.0.txt
#

from sanic import Sanic, response
from sanic import Sanic, Request, response

from ..service import LegacyService

Expand All @@ -28,9 +28,18 @@ def create_server(**kwargs):
legacy_server.ctx.service = LegacyService(**kwargs)

@legacy_server.route('/<path:path>')
async def legacy_handler(request, path):
async def legacy_handler(request: Request, path: str):

return response.text(f'Legacy: {path}')
if (
not path.startswith('_') and
# TODO: maintain a registry of endpoints,
# don't rely on this getattr
(endpoint := getattr(request.ctx.service, path, None))
):

return response.text(endpoint(**request.args))

return response.text(f'No such path: {path}', status = 404)


return legacy_server
6 changes: 3 additions & 3 deletions omnipath_server/server/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# https://www.gnu.org/licenses/gpl-3.0.txt
#

from sanic import Sanic, response
from sanic import Sanic, Request, response

__all__ = [
'DOMAINS',
Expand All @@ -32,7 +32,7 @@


@main_server.middleware('request')
async def route_requests(request):
async def route_requests(request: Request):

request.ctx.server = None

Expand All @@ -50,7 +50,7 @@ async def route_requests(request):
'/<path:path>',
methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
)
async def handle_request(request, path):
async def handle_request(request: Request, path: str):

if (server := request.ctx.server) is not None:

Expand Down

0 comments on commit 20ab7c6

Please sign in to comment.