Skip to content

Commit 574826a

Browse files
author
Adios Template Bot
committed
Sync template-python-fastapi-postgres from canonical templates
Template-Source-Digest: sha256:d1aefe2d11d8c28ff62fd2e6ec53e6fbf78932182152b5198dbe81a3e6b430c0
0 parents  commit 574826a

8 files changed

Lines changed: 187 additions & 0 deletions

File tree

‎.editorconfig‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
trim_trailing_whitespace = true
8+
indent_style = space
9+
indent_size = 4
10+
11+
[*.{js,jsx,ts,tsx,css,scss,less,html,json,yml,yaml}]
12+
indent_size = 2
13+
14+
[*.md]
15+
indent_size = 2
16+
trim_trailing_whitespace = false
17+
18+
[*.sh]
19+
indent_size = 2
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Validate template
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
template:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
15+
- name: Check repository contract
16+
shell: bash
17+
run: |
18+
set -euo pipefail
19+
test -s README.md
20+
test -s adios.yaml
21+
test -s .gitignore
22+
test -s .editorconfig
23+
grep -q "adios up" README.md
24+
ruby -e 'require "yaml"; content = File.read("adios.yaml"); value = begin; YAML.safe_load(content, permitted_classes: [], permitted_symbols: [], aliases: true); rescue ArgumentError; YAML.safe_load(content, [], [], true); end; abort "adios.yaml must contain a mapping" unless value.is_a?(Hash)'
25+
if git ls-files \
26+
| grep -E '(^|/)(node_modules|\.next|dist|build|bin|obj|out|vendor|\.venv|__pycache__|\.pytest_cache|\.mypy_cache|\.ruff_cache|coverage|staticfiles)(/|$)|\.py[co]$' \
27+
| grep -Ev '^bin/rails$' \
28+
| grep -q .; then
29+
echo "Generated dependencies or build output are tracked" >&2
30+
exit 1
31+
fi

‎.gitignore‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# OS files
2+
.DS_Store
3+
Thumbs.db
4+
5+
# Environment and secrets
6+
.env
7+
.env.*
8+
!.env.example
9+
*.key
10+
*.pem
11+
*.crt
12+
13+
# Logs
14+
*.log
15+
logs/
16+
npm-debug.log*
17+
yarn-debug.log*
18+
yarn-error.log*
19+
.pnpm-debug.log*
20+
21+
# Dependencies
22+
node_modules/
23+
.pnp/
24+
.pnp.*
25+
vendor/
26+
.venv/
27+
venv/
28+
env/
29+
30+
# Build output and caches
31+
.next/
32+
out/
33+
dist/
34+
build/
35+
coverage/
36+
.pytest_cache/
37+
.mypy_cache/
38+
.ruff_cache/
39+
__pycache__/
40+
*.py[cod]
41+
staticfiles/
42+
bin/
43+
obj/
44+
TestResults/
45+
*.tsbuildinfo
46+
next-env.d.ts
47+
48+
# Runtime data
49+
data/
50+
tmp/
51+
*.pid

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# FastAPI with PostgreSQL
2+
3+
Adios provisions a private PostgreSQL 16 database. For local development, set `DATABASE_URL` to a disposable PostgreSQL database. The readiness check at `/healthz` runs `SELECT 1`.
4+
5+
Deploy with `adios up`.

‎adios.yaml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
type: api
2+
runtime:
3+
name: python@3.13
4+
health_path: /healthz
5+
readiness_timeout_seconds: 120
6+
secrets:
7+
POSTGRES_PASSWORD: secret://generate:32
8+
resources:
9+
db:
10+
template: postgres:16
11+
type: database
12+
database: fastapi
13+
username: fastapi
14+
password: secret://POSTGRES_PASSWORD
15+
dependency_cmd: python -m pip install -r requirements.txt
16+
test_cmd: python -m unittest discover -s tests
17+
start_cmd: python -m uvicorn main:app --host 0.0.0.0 --port ${PORT:-8080}
18+
env:
19+
PORT: "8080"
20+
DATABASE_URL: secret://DATABASE_URL

‎main.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
3+
import psycopg
4+
from fastapi import FastAPI, HTTPException
5+
6+
app = FastAPI()
7+
8+
9+
@app.get("/")
10+
def home():
11+
return {"message": "Hello from Adios FastAPI"}
12+
13+
14+
@app.get("/healthz")
15+
def health():
16+
url = os.environ.get("DATABASE_URL")
17+
if not url:
18+
raise HTTPException(503, "Database unavailable")
19+
try:
20+
with psycopg.connect(url, connect_timeout=3) as connection:
21+
connection.execute("SELECT 1").fetchone()
22+
except psycopg.Error:
23+
raise HTTPException(503, "Database unavailable") from None
24+
return {"status": "ok"}
25+
26+
27+
@app.get("/api/config")
28+
def config():
29+
return {"database_configured": bool(os.environ.get("DATABASE_URL"))}

‎requirements.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fastapi==0.141.1
2+
uvicorn==0.52.4
3+
psycopg[binary]==3.3.5

‎tests/test_health.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import unittest
2+
from unittest.mock import patch
3+
4+
import psycopg
5+
from fastapi import HTTPException
6+
7+
from main import health
8+
9+
10+
class ReadinessTest(unittest.TestCase):
11+
@patch.dict("os.environ", {"DATABASE_URL": ""})
12+
def test_requires_binding(self):
13+
with self.assertRaises(HTTPException) as failure:
14+
health()
15+
self.assertEqual(failure.exception.status_code, 503)
16+
17+
@patch.dict("os.environ", {"DATABASE_URL": "postgresql://test.invalid/db"})
18+
@patch("main.psycopg.connect")
19+
def test_queries_database(self, connect):
20+
self.assertEqual(health(), {"status": "ok"})
21+
connect.return_value.__enter__.return_value.execute.assert_called_once_with("SELECT 1")
22+
23+
@patch.dict("os.environ", {"DATABASE_URL": "postgresql://test.invalid/db"})
24+
@patch("main.psycopg.connect", side_effect=psycopg.OperationalError("connection refused"))
25+
def test_connection_failure(self, _connect):
26+
with self.assertRaises(HTTPException) as failure:
27+
health()
28+
self.assertEqual(failure.exception.status_code, 503)
29+
self.assertEqual(failure.exception.detail, "Database unavailable")

0 commit comments

Comments
 (0)