Skip to content

Commit

Permalink
Add wrap_loader param to Server.serve (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
gi0baro committed Mar 14, 2023
1 parent 6b3d076 commit 8d8bd7f
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions granian/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from functools import partial
from pathlib import Path
from typing import Any, Dict, List, Optional
from typing import Any, Callable, Dict, List, Optional

import watchfiles

Expand Down Expand Up @@ -343,17 +343,26 @@ def _serve_with_reloader(self, spawn_target, target_loader):

self.shutdown()

def serve(self, spawn_target = None, target_loader = None):
def serve(
self,
spawn_target: Optional[Callable[..., None]] = None,
target_loader: Optional[Callable[..., Callable[..., Any]]] = None,
wrap_loader: bool = True
):
default_spawners = {
Interfaces.ASGI: self._spawn_asgi_worker,
Interfaces.RSGI: self._spawn_rsgi_worker,
Interfaces.WSGI: self._spawn_wsgi_worker
}
target_loader = target_loader or load_target
if target_loader:
if wrap_loader:
target_loader = partial(target_loader, self.target)
else:
target_loader = partial(load_target, self.target)
spawn_target = spawn_target or default_spawners[self.interface]

serve_method = (
self._serve_with_reloader if self.reload_on_changes else
self._serve
)
serve_method(spawn_target, partial(target_loader, self.target))
serve_method(spawn_target, target_loader)

0 comments on commit 8d8bd7f

Please sign in to comment.