Skip to content

Commit 0164e89

Browse files
authored
Merge pull request #24 from kc3hack/feature/issue-7-backend-setup
feat: Backend初期構成の作成(FastAPI + ディレクトリ構成)
2 parents 86c36ce + 0346b9c commit 0164e89

File tree

6 files changed

+1565
-29
lines changed

6 files changed

+1565
-29
lines changed

.github/skills/architecture/SKILL.md

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,55 @@ Frontendはユーザーインターフェースを提供し、Backendはビジ
1212

1313
## 2. Backend Architecture (Python/FastAPI)
1414

15-
Backendは、関心事の分離(Separation of Concerns)を重視し、以下のようなレイヤー構造を意識してください。
16-
17-
### Directory Structure & Responsibilities
18-
19-
- **`app/api/` (Presentation Layer)**:
20-
- ルーティング定義とリクエスト/レスポンスのハンドリングのみを行う。
21-
- ビジネスロジックをここに書かないこと。ServiceやChainを呼び出す役割に徹する。
22-
- Pydanticモデル(`schemas/`)を使用してバリデーションを行う。
23-
24-
- **`app/chains/` & `app/services/` (Business Logic Layer)**:
25-
- コアとなるビジネスロジックや、LangChain等を用いたLLMのオーケストレーションを記述する。
26-
- 特定のAPIエンドポイントに依存しない再利用可能なロジックを目指す。
27-
28-
- **`app/models/` (Data Access Layer)**:
29-
- データベースのモデル定義(SQLAlchemy等)。
30-
- DB操作の具体的な実装(CRUD)は、必要に応じて `crud/` ディレクトリ(またはRepositoryパターン)に分離することを推奨するが、規模が小さいうちはService内で完結させても良い。
31-
32-
- **`app/schemas/` (Data Transfer Object)**:
33-
- APIの入出力定義(Pydanticモデル)。
34-
- DBモデルとAPIレスポンスモデルは分離して定義すること。
35-
36-
### Key Principles
37-
38-
- **Dependency Injection (DI)**: DBセッションや設定、Serviceクラスの依存関係は、FastAPIの `Depends` を使用して注入する。
39-
- **Statelessness**: サーバーはステートレスに保ち、スケーラビリティを確保する。
15+
バックエンドは関心事の分離(Separation of Concerns)と依存性注入(Dependency Injection)を重視したレイヤー構造を意識してください。
16+
17+
### 2.1. Directory Structure
18+
19+
```
20+
backend/
21+
├── app/
22+
│ ├── api/
23+
│ │ ├── api.py # ルーター集約
24+
│ │ └── endpoints/ # 各エンドポイント
25+
│ ├── services/ # Business Logic(調整役)
26+
│ ├── chains/ # LLM実装詳細
27+
│ ├── crud/ # データアクセス
28+
│ ├── core/ # ユーティリティ
29+
│ ├── middleware/ # 横断的処理
30+
│ ├── db/ # DB接続
31+
│ ├── models/ # SQLAlchemyモデル
32+
│ ├── schemas/ # Pydantic(Request/Response)
33+
│ └── main.py
34+
├── tests/ # テストコード
35+
│ ├── test_api/
36+
│ ├── test_services/
37+
│ └── test_chains/
38+
└── pyproject.toml
39+
```
40+
41+
### 2.2. レイヤー構造
42+
43+
#### **Presentation Layer (`api/`)**
44+
- ルーティング、リクエスト/レスポンス処理
45+
- ビジネスロジックは書かず、サービス呼び出しのみ
46+
47+
#### **Business Logic Layer (`services/`, `chains/`)**
48+
- `services/`: 複数のRepository/Chainの調整(薄い層)
49+
- `chains/`: LLM実装の詳細(変更頻度が高い可能性がある)
50+
- 特定のエンドポイントに依存しない、再利用可能なロジック
51+
52+
#### **Infrastructure Layer (`crud/`, `core/`, `middleware/`, `db/`)**
53+
- `crud/`: データアクセス
54+
- `core/`: 技術的実装(JWT, ハッシュ化, 設定等)
55+
- `middleware/`: 横断的処理(認証、ログ等)
56+
- `db/`: DB接続管理
57+
58+
### 2.3. 設計原則
59+
60+
- **Dependency Injection**: `Depends`で依存注入
61+
- **Separation of Concerns**: 各層は単一責任
62+
- **Reusability**: 特定エンドポイントに依存しない
63+
- **Testability**: DIによるモック化
4064

4165
## 3. Frontend Architecture (Next.js)
4266

backend/app/__init__.py

Whitespace-only changes.

backend/app/api/api.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""ルーター集約"""
2+
3+
from fastapi import APIRouter
4+
5+
api_router = APIRouter()
6+
7+
# 将来的にエンドポイントを追加する際はここでインクルード
8+
# from app.api.endpoints import auth, quests
9+
# api_router.include_router(auth.router, prefix="/auth", tags=["auth"])
10+
# api_router.include_router(quests.router, prefix="/quests", tags=["quests"])

backend/app/core/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""アプリケーション設定"""
2+
3+
# API バージョン
4+
API_V1_STR = "/api/v1"
5+
6+
# プロジェクト情報
7+
PROJECT_NAME = "Team29 Backend"

backend/app/main.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
from fastapi import FastAPI
2+
from fastapi.middleware.cors import CORSMiddleware
23

3-
app = FastAPI()
4+
app = FastAPI(
5+
title="Team29 Backend API",
6+
description="KC3HACK2026 Team29 Backend",
7+
version="0.1.0",
8+
)
9+
10+
# CORS設定(開発環境用)
11+
app.add_middleware(
12+
CORSMiddleware,
13+
allow_origins=["*"], # 本番環境では具体的なオリジンを指定
14+
allow_credentials=True,
15+
allow_methods=["*"],
16+
allow_headers=["*"],
17+
)
418

519

620
@app.get("/")
7-
def read_root():
8-
return {"message": "Hello form FastAPI"}
21+
def health_check():
22+
"""ヘルスチェックエンドポイント"""
23+
return {"message": "Hello from Team29 Backend", "status": "ok"}
924

1025

1126
@app.get("/health")
12-
def health_check():
27+
def health():
28+
"""ヘルスチェック用エンドポイント"""
1329
return {"status": "ok"}

0 commit comments

Comments
 (0)