Repository navigation
chore: converge Cloudflare and Neon foundation #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Infrastructure adapters and runtime-specific boundaries.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
|
|
||
| from sqlalchemy import create_engine | ||
| from sqlalchemy.engine import Engine, make_url | ||
| from sqlalchemy.orm import sessionmaker | ||
|
|
||
|
|
||
| def normalize_sqlalchemy_postgres_url(database_url: str) -> str: | ||
| """Keep legacy local URLs working while accepting generic Postgres URLs.""" | ||
| if database_url.startswith("postgresql+"): | ||
| return database_url | ||
| if database_url.startswith("postgresql://"): | ||
| return database_url.replace("postgresql://", "postgresql+psycopg2://", 1) | ||
| if database_url.startswith("postgres://"): | ||
| return database_url.replace("postgres://", "postgresql+psycopg2://", 1) | ||
| return database_url | ||
|
|
||
|
|
||
| def detect_connection_mode(database_url: str) -> str: | ||
| return "pooled" if "-pooler." in database_url else "direct" | ||
|
|
||
|
|
||
| def detect_database_provider(database_url: str) -> str: | ||
| lowered = database_url.lower() | ||
| return "neon" if "neon.tech" in lowered else "postgres" | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class PostgresRuntimeBoundary: | ||
| application_url: str | ||
| migration_url: str | ||
| provider: str | ||
| application_connection_mode: str | ||
| migration_connection_mode: str | ||
|
|
||
| @property | ||
| def config_available(self) -> bool: | ||
| return bool(self.application_url) | ||
|
|
||
|
|
||
| def build_postgres_runtime_boundary(settings) -> PostgresRuntimeBoundary: | ||
| application_url = normalize_sqlalchemy_postgres_url(settings.database_url) | ||
| migration_url = normalize_sqlalchemy_postgres_url( | ||
| settings.database_url_unpooled or settings.database_url | ||
| ) | ||
| return PostgresRuntimeBoundary( | ||
| application_url=application_url, | ||
| migration_url=migration_url, | ||
| provider=detect_database_provider(application_url), | ||
| application_connection_mode=detect_connection_mode(application_url), | ||
| migration_connection_mode=detect_connection_mode(migration_url), | ||
| ) | ||
|
|
||
|
|
||
| def create_app_engine(boundary: PostgresRuntimeBoundary) -> Engine: | ||
| return create_engine(boundary.application_url, pool_pre_ping=True) | ||
|
|
||
|
|
||
| def create_session_local(boundary: PostgresRuntimeBoundary) -> sessionmaker: | ||
| engine = create_app_engine(boundary) | ||
| return sessionmaker(bind=engine, autoflush=False, expire_on_commit=False) | ||
|
|
||
|
|
||
| def redact_database_url(database_url: str) -> str: | ||
| return make_url(database_url).render_as_string(hide_password=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Runtime-agnostic application helpers.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| from sqlalchemy import text | ||
|
|
||
| from app.infrastructure.postgres import build_postgres_runtime_boundary | ||
|
|
||
|
|
||
| def build_health_payload(*, settings, engine, include_db_connectivity: bool = True) -> tuple[int, dict[str, Any]]: | ||
| boundary = build_postgres_runtime_boundary(settings) | ||
| payload: dict[str, Any] = { | ||
| "status": "ok", | ||
| "runtime": { | ||
| "platform": "fastapi", | ||
| "alive": True, | ||
| }, | ||
| "application": { | ||
| "boot": "ok", | ||
| }, | ||
| "database": { | ||
| "configured": boundary.config_available, | ||
| "provider": boundary.provider, | ||
| "application_connection_mode": boundary.application_connection_mode, | ||
| "migration_connection_mode": boundary.migration_connection_mode, | ||
| "connectivity_checked": include_db_connectivity, | ||
| "reachable": None, | ||
| }, | ||
| } | ||
|
|
||
| if not include_db_connectivity: | ||
| return 200, payload | ||
|
|
||
| try: | ||
| with engine.connect() as conn: | ||
| conn.execute(text("SELECT 1")) | ||
| except Exception: | ||
| payload["status"] = "unavailable" | ||
| payload["database"]["reachable"] = False | ||
| return 503, payload | ||
|
|
||
| payload["database"]["reachable"] = True | ||
| return 200, payload |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Use this file only for local `wrangler dev`. | ||
| # Do not commit real secrets. Prefer the environment variable form | ||
| # `CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE` so local creds stay | ||
| # out of source control. | ||
| PASAY_APP_ENV=local | ||
| PASAY_HEALTHCHECK_DB_CONNECTIVITY=false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Pasay Cloudflare Worker Foundation | ||
|
|
||
| This directory is the PASAY-ARCH-CF-NEON-001 foundation slice for the future | ||
| Cloudflare runtime. It intentionally exposes only a minimal `GET /health` | ||
| Worker and keeps all business logic in the existing Python codebase. | ||
|
|
||
| ## Configuration model | ||
|
|
||
| - Worker runtime: Cloudflare Workers (`wrangler.jsonc`) | ||
| - Database edge boundary: Hyperdrive binding `HYPERDRIVE` | ||
| - Local dev credentials: use | ||
| `CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE` | ||
| - App-level non-secret vars: `.dev.vars` or `wrangler.jsonc` `vars` | ||
| - Secrets: manage outside the repo with Cloudflare secrets / Hyperdrive config | ||
|
|
||
| ## Neon notes | ||
|
|
||
| - Application traffic should use the pooled Neon endpoint behind Hyperdrive. | ||
| - Migrations and admin tooling should use the direct Neon endpoint. | ||
| - This slice does not create or modify a second schema. | ||
|
|
||
| ## Commands | ||
|
|
||
| - `npm install` | ||
| - `npm run typegen` | ||
| - `npm test` | ||
| - `npx wrangler dev` | ||
|
|
||
| For remote Hyperdrive behavior, use `npx wrangler dev --remote` only after the | ||
| real Hyperdrive binding ID is provisioned outside this repository. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.