使用 Fiber 的邮箱验证服务
一个基于整洁架构的邮箱验证服务,用于生成和验证验证码。
特性
- 整洁架构实现
- 内存中存储验证码
- 集成 SMTP 邮件服务
- 代码生成和哈希
- 可配置的代码过期时间
- 线程安全操作
项目结构
email-verification/
├── api/
│ └── handlers/ # HTTP handlers
├── application/ # Application business logic
├── domain/ # Domain models and interfaces
├── infrastructure/ # External implementations
│ ├── code/ # Code generation
│ ├── email/ # SMTP service
│ └── repository/ # Data storage
└── config/ # Configuration
配置
使用你的 SMTP 设置更新 config/config.go
func GetConfig() *Config {
return &Config{
SMTPHost: "smtp.gmail.com",
SMTPPort: 587,
SMTPUser: "your-email@gmail.com",
SMTPPassword: "your-app-password",
CodeExpiration: time.Minute * 1,
}
}
API 端点
方法 | URL | 描述 |
---|---|---|
POST | /verify/send/:email | 发送验证码 |
POST | /verify/check/:email/:code | 验证收到的代码 |
使用示例
- 发送验证码
curl -X POST http://localhost:3000/verify/send/user@example.com
- 验证代码
curl -X POST http://localhost:3000/verify/check/user@example.com/123456
响应示例
成功
{
"message": "Code verified successfully"
}
错误
{
"error": "invalid code"
}
如何运行
- 在
config/config.go
中配置 SMTP 设置 - 运行应用
go run main.go
依赖
- Fiber v2
- Go 1.21+