Skip to content

Commit

Permalink
feat: allow controller to be configured with guards (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
cofin authored Oct 18, 2023
1 parent 06d924b commit 27cfd7b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
3 changes: 3 additions & 0 deletions litestar_saq/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from litestar import Litestar
from litestar.datastructures.state import State
from litestar.types.callable_types import Guard
from saq.types import Function

T = TypeVar("T")
Expand Down Expand Up @@ -92,6 +93,8 @@ class SAQConfig:
"""Location of the static files to serve for the SAQ UI"""
web_path = "/saq"
"""Base path to serve the SAQ web UI"""
web_guards: list[Guard] | None = field(default=None)
"""Guards to apply to web endpoints."""

def __post_init__(self) -> None:
if self.redis is not None and self.redis_url is not None:
Expand Down
7 changes: 4 additions & 3 deletions litestar_saq/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

if TYPE_CHECKING:
from litestar import Controller
from litestar.types.callable_types import Guard
from saq.types import QueueInfo

from litestar_saq.base import Job
Expand All @@ -21,14 +22,15 @@ async def job_info(queue: TaskQueue, job_id: str) -> Job:
return cast("Job", job)


@lru_cache
def build_controller(url_base: str = "/saq") -> type[Controller]: # noqa: C901
@lru_cache(typed=True)
def build_controller(url_base: str = "/saq", controller_guards: list[Guard] | None = None) -> type[Controller]: # noqa: C901
from litestar import Controller, MediaType, get, post
from litestar.exceptions import NotFoundException
from litestar.status_codes import HTTP_202_ACCEPTED

class SAQController(Controller):
tags = ["SAQ"]
guards = controller_guards

@get(
operation_id="WorkerQueueList",
Expand Down Expand Up @@ -133,7 +135,6 @@ async def job_abort(self, task_queues: TaskQueues, queue_id: str, job_id: str) -
operation_id="WorkerIndex",
name="worker:index",
media_type=MediaType.HTML,
cache=False,
include_in_schema=False,
)
async def index(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion litestar_saq/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def on_app_init(self, app_config: AppConfig) -> AppConfig:
opt={"exclude_from_auth": True},
),
)
app_config.route_handlers.append(build_controller(self._config.web_path))
app_config.route_handlers.append(build_controller(self._config.web_path, self._config.web_guards)) # type: ignore[arg-type]
app_config.on_startup.append(self._config.update_app_state)
app_config.signature_namespace.update(self._config.signature_namespace)
workers = self.get_workers()
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ license = {text = "MIT"}
name = "litestar-saq"
readme = "README.md"
requires-python = ">=3.8"
version = "0.1.10"
version = "0.1.11"

[project.optional-dependencies]
hiredis = ["hiredis"]
Expand Down

0 comments on commit 27cfd7b

Please sign in to comment.