This guide will walk you through the basics of using Coastline to manage your application configuration.
Create a Python file (e.g., config.py) and define your configuration schemas using Pydantic models with the @config decorator:
from coastline import config
from pydantic import BaseModel
@config
class Database(BaseModel):
host: str
port: int
name: str
user: str
password: str
@config
class RedisConfig(BaseModel):
host: str
port: int
db: int = 0 # Default value
@config
class AppSettings(BaseModel):
debug: bool = False
log_level: str = "INFO"
max_connections: int = 100
Create a config.yaml file in your project root:
Database:
host: localhost
port: 5432
name: myapp
user: admin
password: secret
RedisConfig:
host: localhost
port: 6379
db: 0
AppSettings:
debug: true
log_level: DEBUG
max_connections: 50
Use the ConfigLoader to load your configuration:
from coastline import ConfigLoader
loader = ConfigLoader()
config = loader.load("config.yaml")
# Access typed configuration objects
database = config["Database"]
redis = config["RedisConfig"]
settings = config["AppSettings"]
print(f"Connecting to database at {database.host}:{database.port}")
print(f"Debug mode: {settings.debug}")
If you want to use a different key name in YAML than your class name, use the name parameter:
@config(name="app-config")
class AppConfiguration(BaseModel):
version: str
environment: str
app-config:
version: "1.0.0"
environment: "production"
You can load configurations from multiple files:
from coastline import ConfigLoader
loader = ConfigLoader()
base_config = loader.load("base.yaml")
# Merge additional config
loader.load("local.yaml")
# Access all loaded configs
all_configs = loader.load("config.yaml")