Skip to content

Commit 4c8c42f

Browse files
authored
Merge pull request #152 from Pseudo-Lab/feat/get-closer-backend-init
feat: getcloser initialize backend project
2 parents cdda69f + b18a70d commit 4c8c42f

File tree

10 files changed

+172
-0
lines changed

10 files changed

+172
-0
lines changed

getcloser/backend/Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM python:3.10-slim
2+
3+
WORKDIR /app
4+
5+
# 시스템 의존성 설치
6+
RUN apt-get update && apt-get install -y build-essential libpq-dev gcc --no-install-recommends && rm -rf /var/lib/apt/lists/*
7+
8+
COPY src/requirements.txt .
9+
RUN pip install --no-cache-dir -r requirements.txt
10+
11+
COPY ./src ./src
12+
13+
EXPOSE 8000
14+
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000"]

getcloser/backend/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
### Backend 서버 세팅
3+
1. docker 설치
4+
2. docker 빌드: `docker-compose -f docker-compose.yaml up --build`
5+
6+
3. API 서버 접속:
7+
`http://localhost:8000/docs`
8+
9+
4. 초기 구조:
10+
- FastAPI 기반 REST API 서버
11+
- PostgreSQL 연결 및 ORM 지원
12+
- `/test/ping_db` 엔드포인트를 통한 DB 연결 상태 확인
13+
14+
15+
### 폴더 구조
16+
```
17+
.
18+
├── docker-compose.yaml
19+
├── Dockerfile
20+
├── README.md
21+
└── src
22+
├── __init__.py
23+
├── assets
24+
├── config.py
25+
├── database.py
26+
├── main.py
27+
├── models
28+
├── requirements.txt
29+
├── routers
30+
│ └── test_db.py
31+
├── services
32+
└── utils
33+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: "3.9"
2+
3+
services:
4+
web:
5+
build: .
6+
container_name: getcloser_backend
7+
ports:
8+
- "8000:8000"
9+
depends_on:
10+
- db
11+
env_file:
12+
- .env
13+
restart: unless-stopped
14+
15+
db:
16+
image: postgres:15
17+
container_name: postgres_db
18+
environment:
19+
POSTGRES_USER: user
20+
POSTGRES_PASSWORD: password
21+
POSTGRES_DB: app_db
22+
volumes:
23+
- postgres_data:/var/lib/postgresql/data
24+
ports:
25+
- "5432:5432"
26+
27+
volumes:
28+
postgres_data:

getcloser/backend/src/__init__.py

Whitespace-only changes.

getcloser/backend/src/config.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import os
2+
from pydantic_settings import BaseSettings
3+
4+
class Settings(BaseSettings):
5+
DATABASE_URL: str = os.getenv("DATABASE_URL", "postgresql+psycopg2://user:password@db:5432/app_db")
6+
SECRET_KEY: str = os.getenv("SECRET_KEY", "change-me-in-prod")
7+
ALGORITHM: str = "HS256"
8+
ACCESS_TOKEN_EXPIRE_MINUTES: int = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", "60"))
9+
10+
settings = Settings()

getcloser/backend/src/database.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from sqlalchemy import create_engine
2+
from sqlalchemy.orm import sessionmaker, declarative_base
3+
from .config import settings
4+
5+
engine = create_engine(settings.DATABASE_URL, echo=False, future=True)
6+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine, future=True)
7+
Base = declarative_base()
8+
9+
# 간단한 의존성
10+
def get_db():
11+
db = SessionLocal()
12+
try:
13+
yield db
14+
finally:
15+
db.close()

getcloser/backend/src/main.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from fastapi import FastAPI
2+
from fastapi.middleware.cors import CORSMiddleware
3+
from dotenv import load_dotenv
4+
import os
5+
6+
from .database import engine, Base
7+
from .routers import test_db
8+
9+
10+
# .env 파일 로드
11+
load_dotenv()
12+
13+
# 간단히 앱 시작 시 테이블 생성 (개발용)
14+
Base.metadata.create_all(bind=engine)
15+
16+
# FastAPI 앱 생성
17+
app = FastAPI(
18+
title="Devfactory 친해지길바라",
19+
description="Devfactory 친해지길바라 API 서버",
20+
version="1.0.0",
21+
docs_url="/docs",
22+
redoc_url="/redoc",
23+
)
24+
25+
# CORS 미들웨어 설정
26+
origins = os.getenv("CORS_ORIGINS", "").split(",")
27+
app.add_middleware(
28+
CORSMiddleware,
29+
allow_origins=origins,
30+
allow_credentials=True,
31+
allow_methods=["*"],
32+
allow_headers=["*"],
33+
)
34+
35+
app.include_router(test_db.test_router)
36+
37+
38+
@app.get("/")
39+
async def read_root():
40+
"""루트 엔드포인트"""
41+
return {
42+
"message": "Devfactory 친해지길바라 API 서버",
43+
"version": "1.0.0",
44+
"status": "running"
45+
}
46+
47+
@app.get("/health")
48+
async def health_check():
49+
"""헬스 체크 엔드포인트"""
50+
return {"status": "healthy"}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fastapi
2+
uvicorn[standard]
3+
SQLAlchemy
4+
pydantic
5+
pydantic_settings
6+
psycopg2-binary
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from fastapi import FastAPI, Depends, APIRouter
2+
from sqlalchemy.exc import SQLAlchemyError
3+
from sqlalchemy import text
4+
from ..database import get_db
5+
6+
test_router = APIRouter(prefix="/test", tags=["test"])
7+
8+
@test_router.get("/ping_db")
9+
def ping_db(db=Depends(get_db)):
10+
try:
11+
# 단순 쿼리 실행 (PostgreSQL 연결 확인)
12+
db.execute(text("SELECT 1"))
13+
return {"status": "ok", "message": "PostgreSQL 연결 성공"}
14+
except SQLAlchemyError as e:
15+
return {"status": "error", "message": str(e)}

getcloser/frontend/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)