Skip to content

Commit

Permalink
simplify & clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Apr 3, 2022
1 parent 263c78f commit e70de19
Show file tree
Hide file tree
Showing 25 changed files with 229 additions and 1,877 deletions.
61 changes: 0 additions & 61 deletions google-native-ts-k8s-python-postgresql/.pre-commit-config.yaml

This file was deleted.

37 changes: 0 additions & 37 deletions google-native-ts-k8s-python-postgresql/Makefile

This file was deleted.

3 changes: 2 additions & 1 deletion google-native-ts-k8s-python-postgresql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ pulumi stack init dev
Set the required configuration variables for this program:

```shell
pulumi config set xpresso-gke-demo:project [your-gcp-project-here]
pulumi config set xpresso-gke-demo:projectId [your-gcp-project-here]
pulumi config set xpresso-gke-demo:region us-west1 # any valid region
pulumi confit set xpresso-gke-demo:appPort 8000 # unless you change the app
```

### Deploy
Expand Down
1 change: 0 additions & 1 deletion google-native-ts-k8s-python-postgresql/VERSION.txt

This file was deleted.

1 change: 1 addition & 0 deletions google-native-ts-k8s-python-postgresql/app/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

!/app
!/requirements.txt
!/VERSION.txt
7 changes: 3 additions & 4 deletions google-native-ts-k8s-python-postgresql/app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ FROM python:3.10-slim
RUN mkdir /opt/project
WORKDIR /opt/project
COPY ./requirements.txt .
RUN pip install --no-cache-dir -U pip wheel && \
pip install --no-cache-dir -r requirements.txt && \
rm -rf requirements.txt
COPY app ./app/
RUN pip install --no-cache-dir -r requirements.txt
COPY ./app ./app/
COPY ./VERSION.txt .
ENV PYTHONPATH "${PYTHONPATH}:/opt/project"
CMD ["python", "app/main.py"]
Empty file.
1 change: 1 addition & 0 deletions google-native-ts-k8s-python-postgresql/app/VERSION.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
3 changes: 0 additions & 3 deletions google-native-ts-k8s-python-postgresql/app/app/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Literal

from pydantic import BaseSettings, SecretStr


Expand All @@ -11,4 +9,3 @@ class Config(BaseSettings):
db_host: str
db_port: int
db_database_name: str
log_level: Literal["DEBUG", "INFO"]
46 changes: 6 additions & 40 deletions google-native-ts-k8s-python-postgresql/app/app/db.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,17 @@
from typing import Annotated, AsyncGenerator
from typing import NamedTuple, Protocol

import asyncpg # type: ignore[import]
from app.config import Config
from xpresso import Depends


async def get_pool(config: Config) -> AsyncGenerator[asyncpg.Pool, None]:
# password is optional:
# - cloudsql proxy won't work with it
# - docker run postgres won't work without it
connection_kwargs = dict(
host=config.db_host,
port=config.db_port,
database=config.db_database_name,
user=config.db_username,
)
if config.db_password is not None:
connection_kwargs["password"] = config.db_password.get_secret_value()
async with asyncpg.create_pool(**connection_kwargs) as pool: # type: ignore
yield pool


InjectDBConnectionPool = Annotated[asyncpg.Pool, Depends(get_pool, scope="app")]


async def get_connection(
pool: InjectDBConnectionPool,
) -> AsyncGenerator[asyncpg.Connection, None]:
async with pool.acquire() as conn: # type: ignore
yield conn


InjectDBConnection = Annotated[asyncpg.Connection, Depends(get_connection)]
class ConnectionHealth(Protocol):
async def is_connected(self) -> bool:
...


class ConnectionHealth:
def __init__(self, pool: InjectDBConnectionPool) -> None:
self.pool = pool
class PostgresConnectionHealth(NamedTuple):
pool: asyncpg.Pool

async def is_connected(self) -> bool:
conn: asyncpg.Connection
async with self.pool.acquire() as conn: # type: ignore
return await conn.fetchval("SELECT 1") == 1 # type: ignore


def get_db_health(pool: InjectDBConnectionPool) -> ConnectionHealth:
return ConnectionHealth(pool)


InjectDBHealth = Annotated[ConnectionHealth, Depends(get_db_health, scope="app")]

This file was deleted.

11 changes: 0 additions & 11 deletions google-native-ts-k8s-python-postgresql/app/app/lifespan.py

This file was deleted.

46 changes: 0 additions & 46 deletions google-native-ts-k8s-python-postgresql/app/app/logconfig.py

This file was deleted.

40 changes: 26 additions & 14 deletions google-native-ts-k8s-python-postgresql/app/app/main.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
import asyncio

import asyncpg # type: ignore[import]
import uvicorn # type: ignore[import]
from xpresso import App

from app.config import Config
from app.lifespan import lifespan
from app.logconfig import get_json_logconfig
from app.db import ConnectionHealth, PostgresConnectionHealth
from app.routes import routes
from xpresso import App

app = App(routes=routes, lifespan=lifespan)

def create_app(conn_health: ConnectionHealth) -> App:
app = App(routes=routes, version=open("VERSION.txt").read().strip())
app.dependency_overrides[ConnectionHealth] = lambda: conn_health
return app


async def main() -> None:
config = Config() # type: ignore # values are loaded from env vars
app.dependency_overrides[Config] = lambda: config
log_config = get_json_logconfig(config.log_level)
server_config = uvicorn.Config(
app,
port=config.app_port,
host=config.app_host,
log_config=log_config,
)
server = uvicorn.Server(server_config)
await server.serve()

async with asyncpg.create_pool( # type: ignore
host=config.db_host,
port=config.db_port,
user=config.db_username,
password=config.db_password,
database=config.db_database_name,
) as pool:
conn_health = PostgresConnectionHealth(pool)
app = create_app(conn_health)
server_config = uvicorn.Config(
app,
port=config.app_port,
host=config.app_host,
)
server = uvicorn.Server(server_config)
await server.serve()


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion google-native-ts-k8s-python-postgresql/app/app/routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from app.db import ConnectionHealth
from pydantic import BaseModel
from xpresso import Path

from app.db import ConnectionHealth


class DBHealth(BaseModel):
connected: bool
Expand Down
Loading

0 comments on commit e70de19

Please sign in to comment.