A Tiny Nim Microservice, Three Real Bugs, and What They Taught Me About My Own Mummy Fork
说明与用途
使用Mummy分支的Nim微服务实现POST /api/contact,包含验证、限流、CORS和通过Resend发送邮件。解决了三个问题(HttpHeaders类型不一致、gcsafe误用、缺少OPTIONS路由),使服务可在Render上部署。
问题
Implement a simple POST /api/contact endpoint that validates input, enforces rate limits, handles CORS, and sends an email, while dealing with deployment constraints and framework quirks.
结果
The microservice now validates input, enforces rate limits, handles CORS preflight, logs requests, serves OpenAPI docs, and reliably sends emails via Resend, with all three discovered bugs fixed and deployment constraints addressed.
工作流
- Define ContactBody type and validation logic
- Add explicit field checks (non-empty, 2000-char limit, email format)
- Configure Mummy middleware: logging, CORS, rate limiter, error handler
- Serve OpenAPI docs at /docs and /openapi.json
- Implement email sending via std/smtp, then switch to Resend API
- Deploy to Render free tier, discover SMTP port block
- Add OPTIONS /api/contact route to enable CORS preflight
- Resolve type mismatch by importing std/httpcore except HttpHeaders
- Wrap global read in cast(gcsafe) to satisfy compiler
- Test edge cases: disallowed origin, malformed email, 2000-char boundary, rapid submissions, health checks
证据
Two HttpHeaders types that look identical and aren't.
gcsafe doesn't care that your global never actually changes.
An unregistered route means your middleware never runs — including for the request that most needs it.
- 记录日期: 2026-09-20T04:00:35.227Z