Initial scaffold: FastAPI micro-service with Docker, SQLite, tests
This commit is contained in:
@@ -0,0 +1 @@
|
||||
# micro-api
|
||||
@@ -0,0 +1,15 @@
|
||||
"""Application configuration — env-driven, zero secrets hardcoded."""
|
||||
|
||||
import os
|
||||
|
||||
|
||||
class Settings:
|
||||
APP_NAME: str = "micro-api"
|
||||
VERSION: str = "0.1.0"
|
||||
DEBUG: bool = os.getenv("DEBUG", "false").lower() == "true"
|
||||
DATABASE_URL: str = os.getenv("DATABASE_URL", "sqlite:///data/app.db")
|
||||
HOST: str = os.getenv("HOST", "0.0.0.0")
|
||||
PORT: int = int(os.getenv("PORT", "8000"))
|
||||
|
||||
|
||||
settings = Settings()
|
||||
@@ -0,0 +1,30 @@
|
||||
"""Database utilities — SQLite for local/dev, prepared for Postgres."""
|
||||
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
DB_PATH = Path(__file__).resolve().parent.parent / "data" / "app.db"
|
||||
|
||||
|
||||
def get_connection() -> sqlite3.Connection:
|
||||
"""Return a new SQLite connection with WAL mode and foreign keys enabled."""
|
||||
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
|
||||
conn = sqlite3.connect(str(DB_PATH))
|
||||
conn.execute("PRAGMA journal_mode=WAL")
|
||||
conn.execute("PRAGMA foreign_keys=ON")
|
||||
conn.row_factory = sqlite3.Row
|
||||
return conn
|
||||
|
||||
|
||||
def init_db():
|
||||
"""Initialize database schema."""
|
||||
conn = get_connection()
|
||||
conn.executescript("""
|
||||
CREATE TABLE IF NOT EXISTS items (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
""")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
"""FastAPI micro-service — entry point."""
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from app.routes import router
|
||||
|
||||
app = FastAPI(
|
||||
title="Micro-API",
|
||||
description="Micro-services API — lightweight, scalable foundation.",
|
||||
version="0.1.0",
|
||||
)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
app.include_router(router)
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"service": "micro-api", "status": "running"}
|
||||
@@ -0,0 +1,18 @@
|
||||
"""Pydantic models for request/response schemas."""
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
|
||||
class ItemCreate(BaseModel):
|
||||
name: str
|
||||
|
||||
|
||||
class ItemResponse(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
name: str
|
||||
created_at: Optional[datetime] = None
|
||||
@@ -0,0 +1,18 @@
|
||||
"""API routes."""
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter(prefix="/api")
|
||||
|
||||
|
||||
@router.get("/health")
|
||||
async def health():
|
||||
"""Health check endpoint."""
|
||||
return {
|
||||
"status": "healthy",
|
||||
"service": "micro-api",
|
||||
"version": "0.1.0",
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
}
|
||||
Reference in New Issue
Block a user