File tree Expand file tree Collapse file tree 3 files changed +38
-1
lines changed Expand file tree Collapse file tree 3 files changed +38
-1
lines changed Original file line number Diff line number Diff line change 1+ from __future__ import annotations
2+
3+ from typing import TYPE_CHECKING
4+
5+ import msgspec
6+ from litestar import Controller , Litestar , get
7+ from litestar .exceptions import InternalServerException
8+
9+ from litestar_asyncpg import AsyncpgConfig , AsyncpgPlugin , PoolConfig
10+
11+ if TYPE_CHECKING :
12+ from asyncpg import Connection
13+
14+
15+ class PostgresHealthCheck (msgspec .Struct ):
16+ """A new type describing a User"""
17+
18+ version : str
19+ uptime : float
20+
21+
22+ class SampleController (Controller ):
23+ @get (path = "/sample" )
24+ async def sample_route (self , db_connection : Connection ) -> PostgresHealthCheck :
25+ """Check database available and returns app config info."""
26+ result = await db_connection .fetchrow (
27+ "select version() as version, extract(epoch from current_timestamp - pg_postmaster_start_time()) as uptime" ,
28+ )
29+ if result :
30+ return PostgresHealthCheck (version = result ["version" ], uptime = result ["uptime" ])
31+ raise InternalServerException
32+
33+
34+ asyncpg = AsyncpgPlugin (config = AsyncpgConfig (pool_config = PoolConfig (dsn = "postgresql://app:app@localhost:5423/app" )))
35+ app = Litestar (plugins = [asyncpg ], route_handlers = [SampleController ])
Original file line number Diff line number Diff line change @@ -6,4 +6,6 @@ services:
66 ports :
77 - " 5423:5432" # use a non-standard port here
88 environment :
9- POSTGRES_PASSWORD : super-secret
9+ POSTGRES_PASSWORD : " app"
10+ POSTGRES_USER : " app"
11+ POSTGRES_DB : " app"
You can’t perform that action at this time.
0 commit comments