Configuration File
Configuration File
The framework provides a flexible configuration system that supports several file formats for different project needs.
Core features
- Multiple formats: YAML, JSON and INI
- Flexible settings: logging, server, database and more
- Simple: load a config file with one line
- Dynamic access: read settings as object attributes
- Safe: validates file format and required keys
Overview
| Feature | Description | Benefit |
|---|---|---|
| Multiple formats | YAML, JSON, INI | Fits different team habits |
| Structured config | Nested configuration | Clear organization |
| Required key validation | Checks required settings automatically | Avoids missing-config errors |
| Dynamic access | Dot syntax for settings | Concise reads |
| Type safety | Preserves original value types | Fewer conversion errors |
Quick Start
Basic Configuration
The logger section is required. The following example uses YAML:
# Logging configuration (required)
logger:
sink: app.log # Log output destination
level: DEBUG # Log level; records DEBUG and above
diagnose: false # Emit detailed debug info (e.g. local variables)
enqueue: true # Write logs asynchronously for better performance
rotation: "500 MB" # Maximum size of a single log file before rotation
retention: "1 months" # How long to keep logs before cleanup
compression: tar # Compression format (e.g. tar) to save space
format: "<green>{time:YYYYMMDD HH:mm:ss}</green> | \
{process.name} | {thread.name} | \
<cyan>{module}</cyan>.<cyan>{function}</cyan>:<cyan>{line}</cyan> | \
<level>{level}</level>: <level>{message}</level>" # Log format templateSupported Formats
The framework supports several common configuration formats. Pick the one that fits your project and team.
YAML (Recommended)
Extension: .yaml / .yml
Characteristics:
- Highly readable
- Supports comments
- Clear hierarchy
- Supports complex data types
# config.yaml
logger:
sink: app.log
level: DEBUG
rotation: "500 MB"
server:
host: 127.0.0.1
port: 8080
debug: true
database:
host: localhost
port: 5432
name: myapp
user: admin
password: secret123JSON
Extension: .json
Characteristics:
- Standardized
- Widely supported
- Strict syntax
{
"logger": {
"sink": "app.log",
"level": "DEBUG",
"rotation": "500 MB"
},
"server": {
"host": "127.0.0.1",
"port": 8080,
"debug": true
},
"database": {
"host": "localhost",
"port": 5432,
"name": "myapp",
"user": "admin",
"password": "secret123"
}
}INI
Extension: .ini
Characteristics:
- Simple and easy to read
- Traditional configuration format
- Good for small configurations
# config.ini
[logger]
sink = app.log
level = DEBUG
rotation = 500 MB
[server]
host = 127.0.0.1
port = 8080
debug = true
[database]
host = localhost
port = 5432
name = myapp
user = admin
password = secret123Complete Example
import asyncio
from simplejrpc.app import ServerApplication
from simplejrpc.response import jsonify
# Create the app instance with the socket file path
app = ServerApplication(socket_path="app.socket",config_path="config.yaml")
@app.route(name="echo")
async def echo(message: str,lang: str):
"""Echo endpoint with validation"""
return jsonify(data=f"收到消息: {message}", msg="消息已处理")
@app.route(name="hello")
async def hello():
"""Simple greeting endpoint"""
print(app.config.logger) # Access the logger config; must be after the app is created
return jsonify(data="Hello, GMSSH!", msg="请求成功")
if __name__ == "__main__":
asyncio.run(app.run())Configuration Loading
Automatic Loading
ServerApplication can load a configuration file automatically, either from an explicit path or by searching:
from simplejrpc.app import ServerApplication
# Option 1: pass the config file path explicitly
app = ServerApplication(
socket_path="app.socket",
config_path="config.yaml" # config file path
)
# Option 2: omit it and let the framework search
app = ServerApplication(
socket_path="app.socket"
# The framework looks for config.yaml in the project root
)Notes
- Make sure the config path is correct and the file exists.
- The config file must contain the required
loggersection. - The file must be syntactically valid.
- Use different config files for development and production.
