การจัดการตัวแปรสภาพแวดล้อมและข้อมูลลับ
เรียนรู้การกำหนดค่า FastAPI ให้เหมาะกับสภาพแวดล้อมต่าง ๆ อย่างปลอดภัยด้วยตัวแปรสภาพแวดล้อมและการจัดการข้อมูลลับ
การจัดการตัวแปรสภาพแวดล้อมและข้อมูลลับ เป็นบทเรียน FastAPI Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน FastAPI Backend Development Bootcamp และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส FastAPI Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Externalize Configuration?
Hardcoding database URLs, API keys, or debug flags into your code is dangerous and inflexible.
The 12-factor app approach stores configuration in the environment, so the same image runs in dev, staging, and production with different settings.
Reading Environment Variables
Python reads variables via os.environ or os.getenv with a default.
import os
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./dev.db")
DEBUG = os.getenv("DEBUG", "false").lower() == "true"Pydantic Settings
FastAPI projects commonly use pydantic-settings to load and validate config into a typed object.
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
database_url: str
secret_key: str
debug: bool = FalseLoading from a .env File
Configure the settings class to read a local .env file during development.
class Settings(BaseSettings):
database_url: str
secret_key: str
class Config:
env_file = ".env"Caching the Settings Object
Use lru_cache so settings are parsed once and reused as a dependency.
from functools import lru_cache
@lru_cache
def get_settings() -> Settings:
return Settings()Injecting Settings into Routes
Treat settings as a FastAPI dependency for clean access and easy testing.
from fastapi import Depends
@app.get("/info")
def info(settings: Settings = Depends(get_settings)):
return {"debug": settings.debug}Never Commit Secrets
Add .env to .gitignore. Committed secrets are extremely hard to fully remove from git history and may leak credentials.
# .gitignore
.env
*.pem
secrets/Production Secret Stores
In production, prefer a managed secret store over plain files:
- AWS Secrets Manager or Parameter Store
- HashiCorp Vault
- Docker or Kubernetes secrets
These inject secrets at runtime without writing them to disk in plain text.
Different Configs per Environment
Use a single variable like ENV to branch behavior, keeping all values in the environment.
ENV = os.getenv("ENV", "development")
if ENV == "production":
LOG_LEVEL = "warning"
else:
LOG_LEVEL = "debug"Validating Required Variables
Pydantic settings raise a clear error at startup if a required variable is missing, failing fast instead of crashing later.
try:
settings = Settings()
except Exception as e:
print("Config error:", e)
raiseTyped Lists and Nested Config
Settings can parse complex values too, like comma-separated origins into a list, keeping parsing logic out of your routes.
class Settings(BaseSettings):
cors_origins: list[str] = []
# CORS_ORIGINS=https://a.com,https://b.comQuick Check
Test your configuration knowledge.
Recap
You learned to manage configuration safely:
- Read config from the environment, not source code
- Use pydantic-settings for typed, validated config
- Never commit
.env; use a secret store in production
Solid config management keeps deployments portable and secure.
คำถามที่พบบ่อย
บทเรียน “การจัดการตัวแปรสภาพแวดล้อมและข้อมูลลับ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดการตัวแปรสภาพแวดล้อมและข้อมูลลับ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส FastAPI Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส FastAPI Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดการตัวแปรสภาพแวดล้อมและข้อมูลลับ”
เรียนรู้การกำหนดค่า FastAPI ให้เหมาะกับสภาพแวดล้อมต่าง ๆ อย่างปลอดภัยด้วยตัวแปรสภาพแวดล้อมและการจัดการข้อมูลลับ คุณปฏิบัติ FastAPI Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน FastAPI Backend Development Bootcamp หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน FastAPI Backend Development Bootcamp บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การจัดการตัวแปรสภาพแวดล้อมและข้อมูลลับ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน FastAPI Backend Development Bootcamp นี้ได้ไหม
ได้ บทเรียน FastAPI Backend Development Bootcamp ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การทำ FastAPI ให้เป็นคอนเทนเนอร์
- การนำไปใช้งานด้วย Gunicorn และ Uvicorn
- กลยุทธ์การนำไปใช้งานบนคลาวด์
- การจัดการตัวแปรสภาพแวดล้อมและข้อมูลลับ