|
| 1 | +"""Copyright 2025 PythonistaGuild |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +from typing import TYPE_CHECKING, Any |
| 19 | + |
| 20 | +from litestar import Litestar |
| 21 | +from litestar.middleware.session.server_side import ServerSideSessionConfig |
| 22 | +from litestar.stores.valkey import ValkeyStore |
| 23 | + |
| 24 | +from .config import config |
| 25 | +from .controllers import * |
| 26 | + |
| 27 | + |
| 28 | +if TYPE_CHECKING: |
| 29 | + from litestar import Controller |
| 30 | + from litestar.middleware import DefineMiddleware |
| 31 | + |
| 32 | + |
| 33 | +class App(Litestar): |
| 34 | + def __init__(self, **kwargs: Any) -> None: |
| 35 | + self.config = config |
| 36 | + |
| 37 | + store = ValkeyStore.with_client(db=config["valkey"]["db"], port=config["valkey"]["port"]) |
| 38 | + stores: dict[str, ValkeyStore] = {"sessions": store} |
| 39 | + |
| 40 | + sessions = ServerSideSessionConfig(max_age=config["sessions"]["max_age"], renew_on_access=True, secure=True) |
| 41 | + middleware: list[DefineMiddleware] = [sessions.middleware] |
| 42 | + |
| 43 | + controllers: list[type[Controller]] = [APIControllerV1] |
| 44 | + super().__init__(route_handlers=controllers, stores=stores, middleware=middleware, **kwargs) # type: ignore |
0 commit comments