핫트레이딩

AI 기반 자동 프로그래밍 시스템 – 전체 설계도 (5) 본문

IT 정보/인공지능 Ai

AI 기반 자동 프로그래밍 시스템 – 전체 설계도 (5)

HOTT 2025. 8. 26. 18:53

초보 개발자를 위해 FastAPI 백엔드 서버 기본 구조를 아주 쉽게 설명드리겠습니다.
이 서버는 개발자 노트를 저장하고, 프론트엔드와 통신하는 역할을 합니다.


1. FastAPI란?

  • Python으로 만든 웹 서버 프레임워크
  • 간단한 API(백엔드 서버)를 빠르게 개발 가능
  • 초보자에게 인기 있음

2. 기능 목표

  • 개발자 노트 저장 (POST /notes)
  • 저장된 노트 목록 확인 (GET /notes)
  • (추후) AI 요청을 처리하는 엔드포인트 추가 예정

3. 폴더 구조 예시

 
backend/
├─ main.py # 서버 실행 파일
├─ database.json # 노트를 저장하는 간단한 파일(초기에는 DB 대신 JSON 사용)
├─ requirements.txt # 필요한 패키지 목록

4. 코드 예시

(1) 패키지 설치

 
bash
mkdir backend
cd backend
python -m venv venv
source venv/bin/activate   # 윈도우는 venv\Scripts\activate
pip install fastapi uvicorn

(2) main.py

 
python
from fastapi import FastAPI
from pydantic import BaseModel
import json
import os

app = FastAPI()

# 데이터 저장 파일
DB_FILE = "database.json"

# 데이터 모델 정의 (프론트에서 오는 JSON 구조와 맞춤)
class DeveloperNote(BaseModel):
    projectName: str
    mustHave: list[str]
    mustNotHave: list[str]
    reviewRequired: list[str]

# 데이터 저장 함수
def save_note_to_file(note: dict):
    if not os.path.exists(DB_FILE):
        with open(DB_FILE, "w") as f:
            json.dump([], f)

    with open(DB_FILE, "r") as f:
        data = json.load(f)

    data.append(note)

    with open(DB_FILE, "w") as f:
        json.dump(data, f, indent=2, ensure_ascii=False)

# 데이터 불러오기 함수
def load_notes_from_file():
    if not os.path.exists(DB_FILE):
        return []
    with open(DB_FILE, "r") as f:
        return json.load(f)

# 기본 테스트용 엔드포인트
@app.get("/")
def read_root():
    return {"message": "FastAPI 백엔드 서버 동작 중"}

# 노트 저장 엔드포인트 (POST)
@app.post("/notes")
def create_note(note: DeveloperNote):
    save_note_to_file(note.dict())
    return {"message": "노트가 저장되었습니다.", "data": note}

# 노트 목록 조회 엔드포인트 (GET)
@app.get("/notes")
def get_notes():
    return load_notes_from_file()

(3) 서버 실행

bash

uvicorn main:app --reload
 

→ 실행 후 http://127.0.0.1:8000/docs 접속하면 API 테스트 화면 확인 가능


5. 동작 예시

1) 노트 저장 (POST /notes)

json
{
  "projectName": "Ai-programimng-UITOOL",
  "mustHave": ["로그인", "수조 모니터링"],
  "mustNotHave": ["외부 데이터 전송"],
  "reviewRequired": ["DB 스키마 변경"]
}
 
응답 :
json
{
  "message": "노트가 저장되었습니다.",
  "data": {
    "projectName": "Ai-programimng-UITOOL",
    "mustHave": ["로그인", "수조 모니터링"],
    "mustNotHave": ["외부 데이터 전송"],
    "reviewRequired": ["DB 스키마 변경"]
  }
}

2) 노트 조회 (GET /notes)

json
[
  {
    "projectName": "Ai-programimng-UITOOL",
    "mustHave": ["로그인", "수조 모니터링"],
    "mustNotHave": ["외부 데이터 전송"],
    "reviewRequired": ["DB 스키마 변경"]
  }
]
 

 


6. 다음 단계

이제:

  • React 프론트엔드에서 입력한 노트를 이 FastAPI 서버에 보내는 연결 작업을 하면
    개발자 노트 작성 UI → 백엔드 저장이 완성됩니다.
 
반응형