Pydantic Field : 데이터 유효성 검사
·
파이썬
Pydantic의 Field는 모델 필드를 정의할 때 사용되는 핵심 기능입니다. Field를 통해 데이터 유효성 검사, 기본값 설정, 제약 조건 등을 세밀하게 제어할 수 있습니다.기본 Field 매개변수from pydantic import BaseModel, Fieldfrom typing import Optionalclass User(BaseModel): # 기본적인 Field 사용 name: str = Field( default="John Doe", # 기본값 설정 min_length=2, # 최소 길이 max_length=50, # 최대 길이 description="사..
FastAPI 프로젝트 배포 자동화 가이드 Part 3: 무중단 배포와 모니터링
·
파이썬/Fast API
이번 파트에서는 무중단 배포 구성과 모니터링 시스템 구축에 대해 알아보겠습니다.무중단 배포 설정Blue-Green 배포 방식을 구현하기 위한 스크립트입니다.# scripts/blue_green_deploy.pyimport dockerimport osimport timeclass BlueGreenDeployer: def __init__(self): self.client = docker.from_env() self.blue_port = 8000 self.green_port = 8001 def get_current_deployment(self): containers = self.client.containers.list( filte..
FastAPI 프로젝트 배포 자동화 가이드 Part 2: CD 파이프라인과 서버 배포
·
파이썬/Fast API
Part 1에서 구성한 자동화된 빌드 환경을 바탕으로, 이제 실제 리눅스 서버에 배포하는 과정을 자동화해보겠습니다.CD 워크플로우 구성# .github/workflows/cd.ymlname: CD Pipelineon: workflow_run: workflows: ["CI Pipeline"] types: - completed branches: [main]jobs: deploy: runs-on: ubuntu-latest if: ${{ github.event.workflow_run.conclusion == 'success' }} steps: - uses: actions/checkout@v2 - name: Get Python Version id: ..
nohup : 백그라운드 프로세스 실행
·
Linux
nohup은 'no hang up'의 약자로, 터미널 세션이 종료되어도 프로세스가 계속 실행되도록 하는 Linux/Unix 명령어입니다. 서버 운영에서 매우 중요한 이 명령어의 사용법에 대해 자세히 알아보겠습니다.기본 사용법# 기본 형식nohup command [arguments] &# 예시: Python 스크립트 실행nohup python3 app.py &# 출력 리다이렉션nohup python3 app.py > app.log 2>&1 &출력 관리nohup은 기본적으로 현재 디렉토리에 'nohup.out' 파일을 생성합니다.# 특정 파일로 출력 리다이렉션nohup ./my_script.sh > output.log &# 표준 에러도 함께 리다이렉션nohup ./my_script.sh > output.lo..
프로젝트 배포 자동화 가이드 Part 1: 개발 환경 구성
·
파이썬/Fast API
윈도우에서 개발한 FastAPI 프로젝트를 리눅스 환경에서 배포하는 과정을 자동화해보겠습니다. 특히 Python 버전과 의존성 관리를 자동화하여 편리하게 배포할 수 있도록 구성하겠습니다.프로젝트 구조 설정my-fastapi-project/├── app/│ ├── api/│ ├── core/│ ├── models/│ └── main.py├── tests/├── .github/│ └── workflows/│ ├── ci.yml│ └── cd.yml├── scripts/│ ├── generate_requirements.py│ ├── get_python_version.py│ └── start.sh├── Dockerfile├── docker-compose.yml└..
Sudoers : 권한 관리
·
Linux
sudoers 파일은 Linux 시스템에서 sudo 명령어의 권한을 관리하는 핵심 설정 파일입니다. 이 파일을 통해 어떤 사용자가 어떤 명령어를 실행할 수 있는지 상세하게 제어할 수 있습니다.sudoers 파일 위치와 기본 구조# sudoers 파일 위치/etc/sudoers# 안전하게 편집하기sudo visudo기본 문법 구조# 사용자 권한 설정user HOST=(USER:GROUP) COMMANDS# 예시john ALL=(ALL:ALL) ALLmary ALL=(root) /usr/bin/apt-get주요 설정 예시# root 사용자 설정root ALL=(ALL:ALL) ALL# sudo 그룹 설정%sudo ALL=(ALL:ALL) ALL# 특정..
FastAPI-Cache로 구현하는 효율적인 API 캐싱
·
파이썬/Fast API
FastAPI-Cache 소개와 설치FastAPI-Cache는 FastAPI 애플리케이션에서 손쉽게 캐싱을 구현할 수 있게 해주는 라이브러리입니다. Redis, Memcached 등 다양한 백엔드를 지원합니다.pip install fastapi-cache2[redis] # Redis 백엔드 사용시기본 설정FastAPI 애플리케이션에 캐시를 설정하는 방법입니다.from fastapi import FastAPIfrom fastapi_cache import FastAPICachefrom fastapi_cache.backends.redis import RedisBackendfrom redis import asyncio as aioredisapp = FastAPI()@app.on_event("startup")a..
Router Tags 활용
·
파이썬/Fast API
FastAPI의 include_router에서 사용되는 tags는 OpenAPI(Swagger) 문서화와 API 그룹화에 매우 유용합니다. 특히 Enum을 활용하면 일관성 있는 태그 관리가 가능합니다.기본적인 태그 사용법가장 단순한 형태의 태그 사용입니다.from fastapi import APIRouter, FastAPIapp = FastAPI()router = APIRouter()# 문자열 리스트로 태그 지정app.include_router( router, prefix="/users", tags=["users", "auth"] # 여러 태그 지정 가능)Enum을 활용한 태그 관리대규모 애플리케이션에서는 Enum을 사용하여 태그를 체계적으로 관리할 수 있습니다.from enum imp..