ideogram-multi-env-setup
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseIdeogram Multi-Environment Setup
Ideogram 多环境配置
Overview
概述
Configure Ideogram across development, staging, and production environments.
在开发、预发布(staging)和生产环境中配置Ideogram。
Prerequisites
前置条件
- Separate Ideogram accounts or API keys per environment
- Secret management solution (Vault, AWS Secrets Manager, etc.)
- CI/CD pipeline with environment variables
- Environment detection in application
- 每个环境独立的Ideogram账号或API密钥
- 密钥管理解决方案(Vault、AWS Secrets Manager等)
- 支持环境变量的CI/CD流水线
- 应用中的环境检测能力
Environment Strategy
环境策略
| Environment | Purpose | API Keys | Data |
|---|---|---|---|
| Development | Local dev | Test keys | Sandbox |
| Staging | Pre-prod validation | Staging keys | Test data |
| Production | Live traffic | Production keys | Real data |
| 环境 | 用途 | API密钥 | 数据 |
|---|---|---|---|
| 开发环境 | 本地开发 | 测试密钥 | 沙箱数据 |
| 预发布环境(Staging) | 生产前验证 | 预发布密钥 | 测试数据 |
| 生产环境 | 线上流量 | 生产密钥 | 真实数据 |
Configuration Structure
配置结构
config/
├── ideogram/
│ ├── base.json # Shared config
│ ├── development.json # Dev overrides
│ ├── staging.json # Staging overrides
│ └── production.json # Prod overridesconfig/
├── ideogram/
│ ├── base.json # Shared config
│ ├── development.json # Dev overrides
│ ├── staging.json # Staging overrides
│ └── production.json # Prod overridesbase.json
base.json
json
{
"timeout": 30000,
"retries": 3,
"cache": {
"enabled": true,
"ttlSeconds": 60
}
}json
{
"timeout": 30000,
"retries": 3,
"cache": {
"enabled": true,
"ttlSeconds": 60
}
}development.json
development.json
json
{
"apiKey": "${IDEOGRAM_API_KEY}",
"baseUrl": "https://api-sandbox.ideogram.com",
"debug": true,
"cache": {
"enabled": false
}
}json
{
"apiKey": "${IDEOGRAM_API_KEY}",
"baseUrl": "https://api-sandbox.ideogram.com",
"debug": true,
"cache": {
"enabled": false
}
}staging.json
staging.json
json
{
"apiKey": "${IDEOGRAM_API_KEY_STAGING}",
"baseUrl": "https://api-staging.ideogram.com",
"debug": false
}json
{
"apiKey": "${IDEOGRAM_API_KEY_STAGING}",
"baseUrl": "https://api-staging.ideogram.com",
"debug": false
}production.json
production.json
json
{
"apiKey": "${IDEOGRAM_API_KEY_PROD}",
"baseUrl": "https://api.ideogram.com",
"debug": false,
"retries": 5
}json
{
"apiKey": "${IDEOGRAM_API_KEY_PROD}",
"baseUrl": "https://api.ideogram.com",
"debug": false,
"retries": 5
}Environment Detection
环境检测
typescript
// src/ideogram/config.ts
import baseConfig from '../../config/ideogram/base.json';
type Environment = 'development' | 'staging' | 'production';
function detectEnvironment(): Environment {
const env = process.env.NODE_ENV || 'development';
const validEnvs: Environment[] = ['development', 'staging', 'production'];
return validEnvs.includes(env as Environment)
? (env as Environment)
: 'development';
}
export function getIdeogramConfig() {
const env = detectEnvironment();
const envConfig = require(`../../config/ideogram/${env}.json`);
return {
...baseConfig,
...envConfig,
environment: env,
};
}typescript
// src/ideogram/config.ts
import baseConfig from '../../config/ideogram/base.json';
type Environment = 'development' | 'staging' | 'production';
function detectEnvironment(): Environment {
const env = process.env.NODE_ENV || 'development';
const validEnvs: Environment[] = ['development', 'staging', 'production'];
return validEnvs.includes(env as Environment)
? (env as Environment)
: 'development';
}
export function getIdeogramConfig() {
const env = detectEnvironment();
const envConfig = require(`../../config/ideogram/${env}.json`);
return {
...baseConfig,
...envConfig,
environment: env,
};
}Secret Management by Environment
按环境管理密钥
Local Development
本地开发
bash
undefinedbash
undefined.env.local (git-ignored)
.env.local (git-ignored)
IDEOGRAM_API_KEY=sk_test_dev_***
undefinedIDEOGRAM_API_KEY=sk_test_dev_***
undefinedCI/CD (GitHub Actions)
CI/CD(GitHub Actions)
yaml
env:
IDEOGRAM_API_KEY: ${{ secrets.IDEOGRAM_API_KEY_${{ matrix.environment }} }}yaml
env:
IDEOGRAM_API_KEY: ${{ secrets.IDEOGRAM_API_KEY_${{ matrix.environment }} }}Production (Vault/Secrets Manager)
生产环境(Vault/密钥管理器)
bash
undefinedbash
undefinedAWS Secrets Manager
AWS Secrets Manager
aws secretsmanager get-secret-value --secret-id ideogram/production/api-key
aws secretsmanager get-secret-value --secret-id ideogram/production/api-key
GCP Secret Manager
GCP Secret Manager
gcloud secrets versions access latest --secret=ideogram-api-key
gcloud secrets versions access latest --secret=ideogram-api-key
HashiCorp Vault
HashiCorp Vault
vault kv get -field=api_key secret/ideogram/production
undefinedvault kv get -field=api_key secret/ideogram/production
undefinedEnvironment Isolation
环境隔离
typescript
// Prevent production operations in non-prod
function guardProductionOperation(operation: string): void {
const config = getIdeogramConfig();
if (config.environment !== 'production') {
console.warn(`[ideogram] ${operation} blocked in ${config.environment}`);
throw new Error(`${operation} only allowed in production`);
}
}
// Usage
async function deleteAllData() {
guardProductionOperation('deleteAllData');
// Dangerous operation here
}typescript
// Prevent production operations in non-prod
function guardProductionOperation(operation: string): void {
const config = getIdeogramConfig();
if (config.environment !== 'production') {
console.warn(`[ideogram] ${operation} blocked in ${config.environment}`);
throw new Error(`${operation} only allowed in production`);
}
}
// Usage
async function deleteAllData() {
guardProductionOperation('deleteAllData');
// Dangerous operation here
}Feature Flags by Environment
按环境配置功能开关
typescript
const featureFlags: Record<Environment, Record<string, boolean>> = {
development: {
newFeature: true,
betaApi: true,
},
staging: {
newFeature: true,
betaApi: false,
},
production: {
newFeature: false,
betaApi: false,
},
};typescript
const featureFlags: Record<Environment, Record<string, boolean>> = {
development: {
newFeature: true,
betaApi: true,
},
staging: {
newFeature: true,
betaApi: false,
},
production: {
newFeature: false,
betaApi: false,
},
};Instructions
操作步骤
Step 1: Create Config Structure
步骤1:创建配置结构
Set up the base and per-environment configuration files.
搭建基础配置文件和各环境专属配置文件。
Step 2: Implement Environment Detection
步骤2:实现环境检测逻辑
Add logic to detect and load environment-specific config.
添加用于检测并加载对应环境配置的逻辑。
Step 3: Configure Secrets
步骤3:配置密钥
Store API keys securely using your secret management solution.
使用密钥管理解决方案安全存储API密钥。
Step 4: Add Environment Guards
步骤4:添加环境防护机制
Implement safeguards for production-only operations.
为仅允许在生产环境执行的操作添加防护措施。
Output
输出结果
- Multi-environment config structure
- Environment detection logic
- Secure secret management
- Production safeguards enabled
- 多环境配置结构
- 环境检测逻辑
- 安全的密钥管理方案
- 已启用的生产环境防护措施
Error Handling
错误处理
| Issue | Cause | Solution |
|---|---|---|
| Wrong environment | Missing NODE_ENV | Set environment variable |
| Secret not found | Wrong secret path | Verify secret manager config |
| Config merge fails | Invalid JSON | Validate config files |
| Production guard triggered | Wrong environment | Check NODE_ENV value |
| 问题 | 原因 | 解决方案 |
|---|---|---|
| 环境识别错误 | 未设置NODE_ENV | 设置环境变量 |
| 密钥未找到 | 密钥路径错误 | 验证密钥管理器配置 |
| 配置合并失败 | JSON格式无效 | 验证配置文件格式 |
| 生产环境防护触发 | 环境设置错误 | 检查NODE_ENV的值 |
Examples
示例
Quick Environment Check
快速环境检查
typescript
const env = getIdeogramConfig();
console.log(`Running in ${env.environment} with ${env.baseUrl}`);typescript
const env = getIdeogramConfig();
console.log(`Running in ${env.environment} with ${env.baseUrl}`);Resources
参考资源
Next Steps
下一步
For observability setup, see .
ideogram-observability如需搭建可观测性系统,请查看。
ideogram-observability