FastAPI 기반의 알림 관리 시스템입니다. MySQL 데이터베이스를 사용하여 알림 CRUD 기능을 제공합니다.
- FastAPI: 웹 프레임워크
- SQLAlchemy 2.0: ORM
- MySQL: 데이터베이스
- PyMySQL: MySQL 드라이버
- Pydantic: 데이터 검증
notification/
├── main.py # FastAPI 애플리케이션 진입점
├── requirements.txt # 의존성 목록
├── .env.example # 환경변수 예시
├── api/ # API 라우터
│ └── notification_router.py
├── app/ # 비즈니스 로직
│ └── notification/
│ ├── model/ # SQLAlchemy 모델
│ │ └── notification.py
│ ├── schema/ # Pydantic 스키마
│ │ └── notification_schema.py
│ ├── repository/ # 데이터 접근 레이어
│ │ └── notification_repository.py
│ └── service/ # 비즈니스 로직 레이어
│ └── notification_service.py
└── core/ # 핵심 설정
└── database.py # 데이터베이스 설정
# 가상환경이 이미 있는 경우
source .venv/bin/activate # Mac/Linux
# .venv\Scripts\activate # Windows
# 의존성 설치
pip install -r requirements.txt.env.example을 참고하여 .env 파일을 생성합니다:
cp .env .env.env 파일에서 데이터베이스 연결 정보를 수정합니다:
DATABASE_URL=mysql+pymysql://user:password@localhost:3306/notification_db
CREATE DATABASE notification_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;# main.py 실행
python main.py
# 또는 uvicorn 직접 실행
uvicorn main:app --reload --host 0.0.0.0 --port 8000서버가 실행되면 다음 URL에서 확인할 수 있습니다:
- API 문서: http://localhost:8000/docs
- 대체 문서: http://localhost:8000/redoc
POST /notifications/
Content-Type: application/json
{
"writer_id": "user123",
"title": "알림 제목",
"content": "알림 내용"
}GET /notifications/{notification_id}GET /notifications/?skip=0&limit=100GET /notifications/writer/{writer_id}?skip=0&limit=100PUT /notifications/{notification_id}
Content-Type: application/json
{
"title": "수정된 제목",
"content": "수정된 내용"
}DELETE /notifications/{notification_id}# 전체 개수
GET /notifications/stats/count
# 작성자별 개수
GET /notifications/stats/count?writer_id=user123| 컬럼명 | 타입 | 설명 |
|---|---|---|
| notification_id | BIGINT | 알림 ID (Primary Key, Auto Increment) |
| writer_id | VARCHAR(255) | 작성자 ID |
| title | VARCHAR(255) | 알림 제목 |
| content | TEXT | 알림 내용 |
| created_at | DATETIME | 생성 시간 (자동) |
| updated_at | DATETIME | 수정 시간 (자동) |
API 문서 페이지(http://localhost:8000/docs)에서 대화형으로 테스트할 수 있습니다.
또는 curl을 사용한 예시:
# 알림 생성
curl -X POST "http://localhost:8000/notifications/" \
-H "Content-Type: application/json" \
-d '{
"writer_id": "user123",
"title": "테스트 알림",
"content": "테스트 내용입니다."
}'
# 알림 조회
curl -X GET "http://localhost:8000/notifications/1"- Python 3.13+
- Type hints 사용
- SQLAlchemy 2.0+ 스타일 (Mapped, mapped_column)
- API Layer (api/): HTTP 요청/응답 처리
- Service Layer (service/): 비즈니스 로직
- Repository Layer (repository/): 데이터베이스 접근
- Model Layer (model/): 데이터베이스 모델
MIT