功能配置
集中式功能配置和模块化架构
功能配置
LaunchSaaS 使用集中式功能配置系统,允许你轻松启用、禁用或切换不同的服务提供商,而无需修改环境变量或整个应用程序中的代码。
配置文件
所有功能配置都在 src/configuration/features.ts 中管理:
import { Features } from "@/schemas/site-configuration";
export const features: Features = {
email: {
provider: "resend",
},
newsletter: {
provider: "resend",
},
storage: {
provider: "disabled",
},
authentication: {
magicLink: true,
captcha: {
enabled: true,
provider: "cloudflare-turnstile",
},
oauths: {
google: true,
github: true,
},
},
payment: {
provider: "stripe",
hooks: {
github: true,
email: true,
},
},
};功能模块
电子邮件
配置用于事务性邮件的邮件服务提供商。
可用提供商:
resend- Resend 邮件服务disabled- 无操作提供商(记录到控制台)
配置:
email: {
provider: "resend", // 或 "disabled"
}了解更多:电子邮件文档
新闻通讯
配置用于管理订阅的新闻通讯服务提供商。
可用提供商:
resend- Resend Audiencesdisabled- 无操作提供商(记录到控制台)
配置:
newsletter: {
provider: "resend", // 或 "disabled"
}了解更多:新闻通讯文档
存储
配置文件上传的存储提供商。
可用提供商:
s3- S3 兼容存储(AWS S3、Cloudflare R2、MinIO)disabled- 无操作提供商(记录到控制台)
配置:
storage: {
provider: "s3", // 或 "disabled"
}了解更多:存储文档
支付
配置支付提供商和支付后钩子。
可用提供商:
stripe- Stripe 支付creem- Creem 支付disabled- 无操作提供商(记录到控制台)
配置:
payment: {
provider: "stripe", // "stripe" | "creem" | "disabled"
hooks: {
github: true, // 启用 GitHub 仓库访问
email: true, // 启用支付确认邮件
},
}支付钩子
支付钩子允许你在支付完成后运行自定义逻辑:
github- 自动将客户添加为 GitHub 协作者email- 发送支付确认邮件
了解更多:支付文档
身份验证
配置身份验证方法和提供商。
配置:
authentication: {
magicLink: true, // 启用无密码登录
captcha: {
enabled: true,
provider: "cloudflare-turnstile", // 或 "hcaptcha"、"captchafox"
},
oauths: {
google: true, // 启用 Google OAuth
github: true, // 启用 GitHub OAuth
},
}了解更多:身份验证文档
架构优势
1. 集中式配置
所有功能开关都在一个地方,便于一目了然地查看启用的功能。
2. 提供商灵活性
通过更改单个配置值来切换提供商:
// 从 Stripe 切换到 Creem
payment: {
provider: "creem", // 从 "stripe" 更改
}3. 工厂模式
每个模块使用工厂模式来实例化正确的提供商:
// 示例:电子邮件提供商工厂
export class EmailProviderFactory {
static getProvider(): Provider {
const type = features.email.provider;
switch (type) {
case "resend":
return ResendProvider.create();
case "disabled":
return new NoOpEmailProvider();
}
}
}4. 无操作提供商
当功能被禁用时,使用无操作提供商:
- 将操作记录到控制台(用于调试)
- 不需要环境变量
- 允许在没有外部服务的情况下进行开发
5. 类型安全
Features 类型确保编译时验证:
export interface Features {
email: {
provider: "resend" | "disabled";
};
newsletter: {
provider: "resend" | "disabled";
};
storage: {
provider: "s3" | "disabled";
};
payment: {
provider: "stripe" | "creem" | "disabled";
hooks: {
github: boolean;
email: boolean;
};
};
authentication: {
magicLink: boolean;
captcha: {
enabled: boolean;
provider: "cloudflare-turnstile" | "hcaptcha" | "captchafox";
};
oauths: {
google: boolean;
github: boolean;
};
};
}添加自定义提供商
要向任何模块添加自定义提供商:
1. 实现提供商接口
创建一个实现模块提供商接口的新提供商类:
// 示例:自定义电子邮件提供商
export class CustomEmailProvider implements Provider {
async sendEmail(options: SendEmailOptions): Promise<SendEmailResult> {
// 你的实现
}
}2. 在工厂中注册
将你的提供商添加到工厂:
// 在 src/lib/email/factory.ts 中
case "custom":
return new CustomEmailProvider();3. 更新类型定义
将你的提供商添加到 Features 类型:
// 在 src/schemas/site-configuration.ts 中
email: {
provider: "resend" | "custom" | "disabled";
}4. 配置
启用你的自定义提供商:
// 在 src/configuration/features.ts 中
email: {
provider: "custom",
}最佳实践
开发环境 vs 生产环境
为不同的环境使用不同的配置:
const isDevelopment = process.env.NEXT_PUBLIC_APP_ENV === "local";
export const features: Features = {
email: {
provider: isDevelopment ? "disabled" : "resend",
},
// ... 其他功能
};功能标志
使用配置作为功能标志:
// 检查功能是否启用
if (features.payment.hooks.github) {
// GitHub 集成逻辑
}环境验证
提供商在启动时验证所需的环境变量:
export class ResendProvider implements Provider {
constructor() {
if (!env.RESEND_API_KEY) {
throw new Error("RESEND_API_KEY is required");
}
}
}这确保你可以及早发现配置错误。