chore: initialize Next.js + FastAPI monorepo with Docker Compose
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
__pycache__
|
||||
*.py[cod]
|
||||
.pytest_cache
|
||||
.mypy_cache
|
||||
.venv
|
||||
venv
|
||||
@@ -0,0 +1,3 @@
|
||||
API_HOST=0.0.0.0
|
||||
API_PORT=8000
|
||||
API_CORS_ORIGINS=http://localhost:3000,http://127.0.0.1:3000
|
||||
@@ -0,0 +1,14 @@
|
||||
FROM python:3.11-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
ENV PYTHONDONTWRITEBYTECODE=1
|
||||
ENV PYTHONUNBUFFERED=1
|
||||
|
||||
COPY requirements.txt ./
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY app ./app
|
||||
|
||||
EXPOSE 8000
|
||||
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||
@@ -0,0 +1,17 @@
|
||||
# API Service
|
||||
|
||||
Python FastAPI 后端服务。
|
||||
|
||||
## 本地开发
|
||||
|
||||
```bash
|
||||
python3 -m venv .venv
|
||||
source .venv/bin/activate
|
||||
python -m pip install -r api/requirements.txt
|
||||
python -m uvicorn api.app.main:app --reload --host 0.0.0.0 --port 8000
|
||||
```
|
||||
|
||||
## 默认接口
|
||||
|
||||
- `GET /health`
|
||||
- `GET /api/v1/ping`
|
||||
@@ -0,0 +1 @@
|
||||
"""fquiz API package."""
|
||||
@@ -0,0 +1,30 @@
|
||||
from functools import lru_cache
|
||||
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
api_name: str = "fquiz-api"
|
||||
api_version: str = "0.1.0"
|
||||
api_host: str = "0.0.0.0"
|
||||
api_port: int = 8000
|
||||
api_cors_origins: str = "http://localhost:3000,http://127.0.0.1:3000"
|
||||
|
||||
model_config = SettingsConfigDict(
|
||||
env_file=".env",
|
||||
env_file_encoding="utf-8",
|
||||
case_sensitive=False,
|
||||
)
|
||||
|
||||
@property
|
||||
def cors_origins(self) -> list[str]:
|
||||
return [
|
||||
origin.strip()
|
||||
for origin in self.api_cors_origins.split(",")
|
||||
if origin.strip()
|
||||
]
|
||||
|
||||
|
||||
@lru_cache
|
||||
def get_settings() -> Settings:
|
||||
return Settings()
|
||||
@@ -0,0 +1,36 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from .config import get_settings
|
||||
|
||||
settings = get_settings()
|
||||
|
||||
app = FastAPI(
|
||||
title=settings.api_name,
|
||||
version=settings.api_version,
|
||||
)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=settings.cors_origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
def health() -> dict[str, str]:
|
||||
return {
|
||||
"status": "ok",
|
||||
"service": settings.api_name,
|
||||
"version": settings.api_version,
|
||||
}
|
||||
|
||||
|
||||
@app.get("/api/v1/ping")
|
||||
def ping() -> dict[str, str]:
|
||||
return {
|
||||
"message": "pong",
|
||||
"service": settings.api_name,
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
[project]
|
||||
name = "fquiz-api"
|
||||
version = "0.1.0"
|
||||
description = "FastAPI backend for fquiz"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
"fastapi>=0.116.0,<1.0.0",
|
||||
"uvicorn[standard]>=0.35.0,<1.0.0",
|
||||
"pydantic-settings>=2.10.0,<3.0.0",
|
||||
]
|
||||
@@ -0,0 +1,3 @@
|
||||
fastapi>=0.116.0,<1.0.0
|
||||
uvicorn[standard]>=0.35.0,<1.0.0
|
||||
pydantic-settings>=2.10.0,<3.0.0
|
||||
Reference in New Issue
Block a user