|
1 | | -# 01: Python |
| 1 | +# 01: Python Backend Development |
2 | 2 |
|
3 | | -## 시나리오 |
| 3 | +## Scenario |
4 | 4 |
|
5 | | -Contoso 아웃도어 컴파니의 마케팅 팀에서는 제품 홍보를 위한 마이크로 소셜 미디어 웹사이트를 빠르게 론칭하고 싶어 합니다. 개발팀의 Python 개발자인 당신은 GitHub Codespaces 안에서 GitHub Copilot을 이용해 간단한 백엔드 API를 만들어 달라는 요청을 받았습니다. 이에 따라 FastAPI를 사용하여 SNS 기능을 구현하고, 간단히 SQLite 데이터베이스를 연동해 CRUD를 수행하려고 합니다. |
| 5 | +Contoso is a company that sells products for various outdoor activities. A marketing department of Contoso would like to launch a micro social media website to promote their products for existing and potential customers. |
6 | 6 |
|
7 | | -## 사전 준비사항 |
| 7 | +As a Python developer, you're going to build a Python backend app using FastAPI. For now, you're using the in-memory feature of SQLite. |
8 | 8 |
|
9 | | -[README](../README.md) 문서를 참조하여 개발 환경을 준비합니다. |
| 9 | +## Prerequisites |
10 | 10 |
|
11 | | -## 순서 |
| 11 | +Refer to the [README](../README.md) doc for preparation. |
12 | 12 |
|
13 | | -- [개발 과정 프롬프트](#개발-과정-프롬프트) |
14 | | - - [FastAPI 앱 프로젝트 준비](#fastapi-앱-프로젝트-준비) |
15 | | - - [CRUD API 구현](#crud-api-구현) |
16 | | - - [가상환경으로 앱 실행](#가상환경으로-앱-실행) |
17 | | - - [API 확인](#api-확인) |
18 | | - - [기본 데이터베이스 테이블 생성](#기본-데이터베이스-테이블-생성) |
19 | | -- [서비스 종료](#서비스-종료) |
| 13 | +## Getting Started |
20 | 14 |
|
21 | | -## 개발 과정 프롬프트 |
| 15 | + - [Check GitHub Copilot Agent Mode](#check-github-copilot-agent-mode) |
| 16 | + - [Prepare Custom Instructions](#prepare-custom-instructions) |
| 17 | + - [Prepare Virtual Environment](#prepare-virtual-environment) |
| 18 | + - [Build FastAPI Backend App](#build-fastapi-backend-app) |
22 | 19 |
|
23 | | -아래는 FastAPI 앱을 개발하여 간단한 SNS 기능을 구현하는 과정입니다. |
| 20 | +### Check GitHub Copilot Agent Mode |
24 | 21 |
|
25 | | -- FastAPI 앱 프로젝트 준비 |
26 | | -- CRUD API 구현 |
27 | | -- 가상환경으로 앱 실행 |
28 | | -- API 확인 |
29 | | -- 기본 데이터베이스 테이블 생성 |
| 22 | +1. Click the GitHub Copilot icon on the top of GitHub Codespace or VS Code and open GitHub Copilot window. |
30 | 23 |
|
31 | | -### FastAPI 앱 프로젝트 준비 |
| 24 | +  |
32 | 25 |
|
33 | | -1. 가상환경 생성 |
| 26 | +1. If you're asked to login or sign up, do it. It's free of charge. |
| 27 | +1. Make sure you're using GitHub Copilot Agent Mode. |
34 | 28 |
|
35 | | - Python 프로젝트 디렉토리를 생성하고, 다음 명령어로 가상환경(venv)을 만듭니다: |
| 29 | +  |
36 | 30 |
|
37 | | - ```bash |
38 | | - python -m venv .venv |
39 | | - ``` |
| 31 | +1. Select model to either `GPT-4.1` or `Claude Sonnet 4`. |
40 | 32 |
|
41 | | -1. 가상환경 활성화 및 FastAPI 설치 |
| 33 | +### Prepare Custom Instructions |
42 | 34 |
|
43 | | - **Windows** |
| 35 | +1. Set the environment variable of `$REPOSITORY_ROOT`. |
44 | 36 |
|
45 | | - ```pwsh |
46 | | - .venv/Scripts/activate |
47 | | - ``` |
| 37 | + ```bash |
| 38 | + # bash/zsh |
| 39 | + REPOSITORY_ROOT=$(git rev-parse --show-toplevel) |
| 40 | + ``` |
48 | 41 |
|
49 | | - **MacOS**, **Linux** 또는 **GitHub Codespaces** |
| 42 | + ```powershell |
| 43 | + # PowerShell |
| 44 | + $REPOSITORY_ROOT = git rev-parse --show-toplevel |
| 45 | + ``` |
50 | 46 |
|
51 | | - ```bash |
52 | | - source .venv/bin/activate |
53 | | - ``` |
| 47 | +1. Copy custom instructions. |
54 | 48 |
|
55 | 49 | ```bash |
56 | | - pip install fastapi uvicorn |
| 50 | + # bash/zsh |
| 51 | + cp -r $REPOSITORY_ROOT/docs/custom-instructions/python/. \ |
| 52 | + $REPOSITORY_ROOT/.github/ |
57 | 53 | ``` |
58 | 54 |
|
59 | | -1. main.py 빈 파일 생성 |
60 | | - |
61 | | -### CRUD API 구현 |
62 | | - |
63 | | -FastAPI를 활용하여 소셜 네트워크 서비스의 모든 핵심 기능을 REST API로 구현합니다. |
64 | | - |
65 | | -```text |
66 | | -FastAPI를 사용하여 openapi.yaml 명세를 기반으로 소셜 네트워크 서비스의 백엔드 API를 구현해. openapi.yaml의 정의에 따라 포스트, 댓글, 좋아요 기능을 포함하는 모든 엔드포인트를 구현한다. 명세에 맞게 적절한 HTTP 메소드, 상태 코드, 요청/응답 형식을 구현하낟. Pydantic 모델을 사용하여 데이터 검증을 구현하고, 예외 처리도 포함해주세요. 데이터는 데이터베이스 없이 간단한 메모리로 관리한다. main.py 파일 하나에 모두 구현한다. |
67 | | -``` |
68 | | - |
69 | | -### 가상환경으로 앱 실행 |
70 | | - |
71 | | -```bash |
72 | | -source .venv/bin/activate |
73 | | -``` |
74 | | - |
75 | | -```bash |
76 | | -uvicorn python.main:app --reload |
77 | | -``` |
78 | | - |
79 | | -### API 확인 |
80 | | - |
81 | | -브라우저에서 http://127.0.0.1:8000/docs 접속 |
82 | | - |
83 | | -### 기본 데이터베이스 테이블 생성 |
84 | | - |
85 | | -애플리케이션에 필요한 데이터를 저장할 SQLite 데이터베이스 스키마를 구성합니다. |
| 55 | + ```powershell |
| 56 | + # PowerShell |
| 57 | + Copy-Item -Path $REPOSITORY_ROOT/docs/custom-instructions/python/* ` |
| 58 | + -Destination $REPOSITORY_ROOT/.github/ -Recurse -Force |
| 59 | + ``` |
86 | 60 |
|
87 | | -```text |
88 | | -SQLite 데이터베이스를 사용하여 openapi.yaml 명세를 참고하여 현재 정의된 API를 구현한다. 애플리케이션 시작 시 자동으로 데이터베이스와 테이블을 생성하는 코드를 구현하되, 테이블이 이미 존재하는 경우 다시 생성하지 않도록 'CREATE TABLE IF NOT EXISTS' 구문을 사용하세요. |
89 | | -``` |
| 61 | +### Prepare Virtual Environment |
90 | 62 |
|
91 | | -서버를 재시작한 후에도 SQLite 데이터베이스에 저장된 포스트 데이터가 유지되는지 확인합니다. |
| 63 | +1. Make sure that you're using GitHub Copilot Agent Mode with the model of `Claude Sonnet 4` or `GPT-4.1`. |
| 64 | +1. Use prompt like below to prepare virtual environment for Python app development. |
92 | 65 |
|
93 | | -## 서비스 종료 |
| 66 | + ```text |
| 67 | + I'd like to write a Python application. But before that, I need to set up a virtual environment. Follow the instructions below. |
| 68 | + |
| 69 | + - Your working directory is `python`. |
| 70 | + - Identify all the steps first, which is you're going to do. |
| 71 | + - Use `.venv` for the virtual environment. |
| 72 | + - Use `uv` as the Python package manager. |
| 73 | + ``` |
94 | 74 |
|
95 | | -FastAPI 서버 등 현재 실행 중인 백엔드 서비스를 종료하려면, 터미널에서 **Ctrl + C**를 누르세요. |
96 | | -이렇게 하면 서버가 정상적으로 종료되어 포트(예: 8000번)를 다른 백엔드 서비스가 사용할 수 있습니다. |
| 75 | +### Build FastAPI Backend App |
| 76 | +
|
| 77 | +1. Make sure that you're using GitHub Copilot Agent Mode with the model of `Claude Sonnet 4` or `GPT-4.1`. |
| 78 | +1. Add [`product-requirements.md`](../product-requirements.md) and [`openapi.yaml`](../openapi.yaml) to GitHub Copilot. |
| 79 | +1. Use prompt like below to build a FastAPI backend application. |
| 80 | + |
| 81 | + ```text |
| 82 | + I'd like to build a FastAPI application as a backend API. Carefully read the entire PRD and `openapi.yaml`. Then, follow the instructions below. |
| 83 | + |
| 84 | + - Your working directory is `python`. |
| 85 | + - Identify all the steps first, which is you're going to do. |
| 86 | + - Use FastAPI as the API app framework. |
| 87 | + - Use SQLite as the database. |
| 88 | + - Use in-memory feature of SQLite. |
| 89 | + - The database should always be initialized whenever starting the app. |
| 90 | + - Use `openapi.yaml` that describes all the endpoints and data schema. |
| 91 | + - Use the port number of `5050`. |
| 92 | + - Entrypoint is `main.py`. |
| 93 | + - The API application should render Swagger UI page through a default endpoint. |
| 94 | + - The API application should render exactly the same OpenAPI document through a default endpoint. |
| 95 | + - DO NOT add anything not defined in `openapi.yaml`. |
| 96 | + - DO NOT modify anything defined in `openapi.yaml`. |
| 97 | + ``` |
97 | 98 |
|
98 | | -만약 서버가 백그라운드에서 실행 중이거나 포트가 여전히 점유 중이라면, 아래 명령어로 프로세스를 확인하고 종료할 수 있습니다. |
| 99 | +1. Once the application is built, verify if it's written properly or not. |
99 | 100 |
|
100 | | -```bash |
101 | | -lsof -i :8000 |
102 | | -kill <PID> |
103 | | -``` |
| 101 | + ```text |
| 102 | + Run the Python app and verify if the app is properly running. Also verify the OpenAPI endpoint renders exactly the same content as `openapi.yaml`. |
104 | 103 |
|
105 | | -lsof 명령어를 수행하면 아래처럼 나옵니다. "--reload" 옵션으로 서비스를 띄웠기 때문에 코드 변경을 감시하는 서버가 하나 더 띄워져서 두 개가 나옵니다. |
| 104 | + If app running fails, analyze the issues and fix them. |
| 105 | + ``` |
106 | 106 |
|
107 | | -```bash |
108 | | -(base) tykimos@MacBook-Air-2 crewai % lsof -i :8000 |
109 | | -COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME |
110 | | -Python 12707 tykimos 3u IPv4 0x9fddcce1c1588271 0t0 TCP localhost:irdmi (LISTEN) |
111 | | -Python 12710 tykimos 3u IPv4 0x9fddcce1c1588271 0t0 TCP localhost:irdmi (LISTEN) |
112 | | -``` |
| 107 | +1. Open a web browser and navigate to `http://localhost:5050`. |
113 | 108 |
|
114 | | -이 경우 코드 변경 감시 서버만 종료하면 되지만 확인이 힘들다면 둘 다 종료시킵니다. |
| 109 | +1. Click the `[keep]` button of GitHub Copilot to take the changes. |
115 | 110 |
|
116 | | -```bash |
117 | | -kill 12707 |
118 | | -kill 12710 |
119 | | -``` |
| 111 | +--- |
120 | 112 |
|
121 | | -그럼 다른 백엔드 서비스를 동일한 포트에서 실행할 수 있습니다. |
| 113 | +OK. You've completed the "Python" step. Let's move onto [STEP 02: JavaScript Frontend Development](./02-javascript.md). |
0 commit comments