Skip to content

Backend Guideline

Jeongcc edited this page Feb 18, 2021 · 3 revisions

Backend Guide πŸ“‹

Docker Containers

λ ˆν¬μ§€ν† λ¦¬λ₯Ό ν΄λ‘ ν•˜μ—¬ docker-compose up -d둜 μ„œλΉ„μŠ€λ₯Ό κ΅¬λ™ν•˜λ©΄ λ„€ 가지 μ»¨ν…Œμ΄λ„ˆκ°€ μ‹€ν–‰ 쀑인 것을 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

# 경둜: skku-coding-platform/

> docker-compose up -d
Creating coding-platform ... done
Creating judge-server ... done
Creating oj-postgres  ... done
Creating oj-redis     ... done

> docker ps
CONTAINER ID   IMAGE                     COMMAND                  CREATED          STATUS                    PORTS                                         NAMES
4cbee61583f6   skkunpc/coding-platform   "/bin/sh -c /app/dep…"   21 seconds ago   Up 20 seconds (healthy)   0.0.0.0:443->1443/tcp, 0.0.0.0:80->8000/tcp   coding-platform
db71de323b0c   skkunpc/judge-server      "/bin/sh -c /code/en…"   22 seconds ago   Up 21 seconds (healthy)   8080/tcp                                      judge-server
86d3d2a3a311   postgres:10-alpine        "docker-entrypoint.s…"   22 seconds ago   Up 21 seconds             5432/tcp                                      oj-postgres
542a774f06c2   redis:4.0-alpine          "docker-entrypoint.s…"   22 seconds ago   Up 21 seconds             6379/tcp                                      oj-redis

각 μ»¨ν…Œμ΄λ„ˆμ˜ λ‚΄μš©μ΄ κΆκΈˆν•˜λ‹€λ©΄ Docker Guide 글을 μ°Έκ³ ν•΄μ£Όμ„Έμš”.


개발 ν™˜κ²½ μ„ΈνŒ…

파이썬 κ°€μƒν™˜κ²½

pip둜 νŒ¨ν‚€μ§€λ₯Ό μ„€μΉ˜λ₯Ό ν•˜λ©΄, μ„€μΉ˜λœ νŒ¨ν‚€μ§€λ“€μ€ 파이썬 μ„€μΉ˜ 디렉토리 μ•ˆμ— μ €μž₯λ©λ‹ˆλ‹€. λ”°λΌμ„œ pip둜 μ„€μΉ˜ν•œ νŒ¨ν‚€μ§€λ“€μ€ ν•΄λ‹Ή νŒ¨ν‚€μ§€κ°€ ν•„μš”ν•œ 파일 뿐만 μ•„λ‹ˆλΌ, λ‹€λ₯Έ λͺ¨λ“  파이썬 μŠ€ν¬λ¦½νŠΈμ—μ„œ μ‚¬μš©ν•  수 μžˆκ²Œλ©λ‹ˆλ‹€. ν•˜λ‚˜μ˜ ν”„λ‘œμ νŠΈλ₯Ό μ§„ν–‰ν•˜λŠ” κ²½μš°μ—λŠ” λ¬Έμ œκ°€ μ—†μ§€λ§Œ, μ—¬λŸ¬ ν”„λ‘œμ νŠΈλ₯Ό μ§„ν–‰ν•˜λŠ” κ²½μš°μ—λŠ” νŒ¨ν‚€μ§€ κ°„μ˜ 버전 좩돌 λ¬Έμ œκ°€ λ°œμƒν•  수 μžˆμŠ΅λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄, μ½”λ”© ν”Œλž«νΌμ—μ„œ μ‚¬μš©ν•˜λŠ” Django의 버전은 3.0인데 λ‹€λ₯Έ μ§„ν–‰μ€‘μ΄λ˜ ν”„λ‘œμ νŠΈμ—μ„œλŠ” 2.0을 μ‚¬μš©ν•΄μ•Ό ν•˜λŠ” 경우, Django 2.0κ³Ό Django 3.0이 ν˜Έν™˜λ˜μ§€ μ•ŠλŠ” λΆ€λΆ„μ—μ„œ μ—¬λŸ¬ λ¬Έμ œκ°€ μƒκΉλ‹ˆλ‹€.

λ”°λΌμ„œ ν•˜λ‚˜μ˜ λ…λ¦½λœ 곡간을 λ§Œλ“€μ–΄μ£ΌλŠ” '가상 ν™˜κ²½'μ΄λΌλŠ” 것을 μ‚¬μš©ν•©λ‹ˆλ‹€. 즉 μœ„μ˜ κ²½μš°μ—μ„œλŠ”, μ½”λ”© ν”Œλž«νΌμšΈ μœ„ν•œ κ°€μƒν™˜κ²½ A와, κ·Έ μ™Έμ˜ λ‹€λ₯Έ ν”„λ‘œμ νŠΈλ₯Ό μœ„ν•œ κ°€μƒν™˜κ²½ B(ν”„λ‘œμ νŠΈκ°€ μ—¬λŸ¬κ°œλ©΄ μ—¬λŸ¬κ°œμ˜ κ°€μƒν™˜κ²½ μ‚¬μš©)λ₯Ό λ§Œλ“€μ–΄ μ€λ‹ˆλ‹€. 그리고 κ°€μƒν™˜κ²½ λ‚΄μ—μ„œ pip둜 νŒ¨ν‚€μ§€λ₯Ό μ„€μΉ˜λ₯Ό ν•˜λ©΄ ν•΄λ‹Ή κ°€μƒν™˜κ²½ 디렉토리 내에 νŒ¨ν‚€μ§€κ°€ μ„€μΉ˜κ°€ λ©λ‹ˆλ‹€. λ”°λΌμ„œ κ°€μƒν™˜κ²½ Aμ—λŠ” Django 3.0을 μ„€μΉ˜ν•˜κ³  Bμ—λŠ” Django 2.0을 μ„€μΉ˜ν•΄μ„œ νŒ¨ν‚€μ§€ κ°„μ˜ 버전 좩돌이 μΌμ–΄λ‚˜λŠ” 문제λ₯Ό ν•΄κ²°ν•©λ‹ˆλ‹€.

λ”°λΌμ„œ 항상 μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜κ±°λ‚˜ μˆ˜μ •ν•  λ•ŒλŠ” κ°€μƒν™˜κ²½μ΄ ν™œμ„±ν™”λœ μƒνƒœμ—¬μ•Ό ν•©λ‹ˆλ‹€.

파이썬 κ°€μƒν™˜κ²½μ΄ λ‚―μ„  뢄듀은 λ‹€μŒ 글을 μ°Έκ³ ν•΄μ£Όμ„Έμš”.
https://docs.python.org/ko/3/library/venv.html

파이썬 κ°€μƒν™˜κ²½(venv) μ„ΈνŒ…

λ‹€μŒ λͺ…λ Ήμ–΄λ‘œ 파이썬 κ°€μƒν™˜κ²½μ„ μ„€μ •ν•˜κ³ , ν•„μš”ν•œ 라이브러리λ₯Ό μ„€μΉ˜ν•©λ‹ˆλ‹€.

# 경둜: skku-coding-platform/

> sudo apt install -y python3-venv
> python3 -m venv venv
> source venv/bin/activate
> pip3 install -r backend/deploy/requirements.txt

개발용 DB 생성

도컀 μ»¨ν…Œμ΄λ„ˆκ°€ μ•„λ‹Œ, λ‘œμ»¬μ—μ„œ testλ₯Ό μ§„ν–‰ν•˜λ €λ©΄ 개발용 DBλ₯Ό λ„μ›Œμ•Ό ν•©λ‹ˆλ‹€. init_db.sh λ₯Ό μ‹€ν–‰ν•˜λ©΄ oj-postgres-dev 와 oj-redis-dev 두 개의 DBκ°€ 도컀 μ»¨ν…Œμ΄λ„ˆλ‘œ μƒμ„±λ˜λŠ” 것을 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

# 경둜: skku-coding-platform/backend/

> ./init_db.sh
+ [[ ! -f manage.py ]]
+ sleep 2
+ docker rm -f oj-postgres-dev oj-redis-dev
Error: No such container: oj-postgres-dev
Error: No such container: oj-redis-dev
+ docker run -it -d -e POSTGRES_DB=onlinejudge -e POSTGRES_USER=onlinejudge -e POSTGRES_PASSWORD=onlinejudge -p 127.0.0.1:5435:5432 --name oj-postgres-dev postgres:10
+ docker run -it -d -p 127.0.0.1:6380:6379 --name oj-redis-dev redis:4.0-alpine
5753d5d9e89eb6c2351aa82d026695d22697785d99285ef9f7efd7570dddd163

> docker ps
CONTAINER ID   IMAGE                 COMMAND                   CREATED           STATUS              PORTS                          NAMES
5753d5d9e89e   redis:4.0-alpine      "docker-entrypoint.s…"    20 seconds ago    Up 19 seconds       127.0.0.1:6380->6379/tcp       oj-redis-dev
e2eb7154aaa3   postgres:10           "docker-entrypoint.s…"    21 seconds ago    Up 19 seconds       127.0.0.1:5435->5432/tcp       oj-postgres-dev

Backend Test

# 경둜: skku-coding-platform/backend/
> ./run_test.py

Django의 test λͺ¨λ“ˆμ„ μ΄μš©ν•˜μ—¬ ν…ŒμŠ€νŠΈν•©λ‹ˆλ‹€. 각각의 App (account, announcement λ“±) 은 testcaseκ°€ μ •μ˜λ˜μ–΄ μžˆλŠ” test.pyλ₯Ό ν¬ν•¨ν•©λ‹ˆλ‹€. ./run_test.py μ‹œ μ •μ˜λœ testcaseκ°€ μˆ˜ν–‰λ˜λ©°, 우리 ν”„λ‘œμ νŠΈμ—μ„œλŠ” master branch의 .github/workflowsλ₯Ό ν¬ν•¨ν•˜μ—¬ pull request μ‹œ Testκ°€ μžλ™μœΌλ‘œ μ§„ν–‰λ©λ‹ˆλ‹€.