create-tigra 3.3.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (203) hide show
  1. package/README.md +81 -12
  2. package/bin/create-tigra.js +495 -472
  3. package/lib/patchers/email-verification.patcher.js +72 -10
  4. package/modules/email-verification/client/hooks/useVerification.ts +4 -1
  5. package/modules/email-verification/server/__tests__/verification.integration.test.ts +115 -0
  6. package/modules/email-verification/server/verification.controller.ts +3 -1
  7. package/modules/email-verification/server/verification.service.ts +44 -13
  8. package/package.json +3 -2
  9. package/template/.github/dependabot.yml +47 -0
  10. package/template/.github/workflows/audit.yml +36 -0
  11. package/template/.github/workflows/ci.yml +79 -0
  12. package/template/AGENTS.md +18 -9
  13. package/template/client/.dockerignore +1 -0
  14. package/template/client/.env.example.production +13 -2
  15. package/template/client/AGENTS.md +13 -5
  16. package/template/client/Dockerfile +186 -14
  17. package/template/client/next.config.ts +1 -1
  18. package/template/client/package-lock.json +15983 -14181
  19. package/template/client/package.json +56 -48
  20. package/template/client/scripts/check-sharp.mjs +53 -0
  21. package/template/client/scripts/next-dev.cjs +5 -1
  22. package/template/client/src/__tests__/proxy.test.ts +92 -0
  23. package/template/client/src/app/healthz/route.ts +16 -0
  24. package/template/client/src/app/layout.tsx +9 -3
  25. package/template/client/src/app/page.tsx +2 -2
  26. package/template/client/src/app/providers.tsx +23 -4
  27. package/template/client/src/components/layout/Header.tsx +7 -3
  28. package/template/client/src/features/admin/hooks/useAdminSessions.ts +2 -1
  29. package/template/client/src/features/admin/hooks/useAdminUsers.ts +3 -1
  30. package/template/client/src/features/admin/types/admin.types.ts +2 -3
  31. package/template/client/src/features/auth/components/AuthInitializer.tsx +35 -18
  32. package/template/client/src/features/auth/components/ForgotPasswordForm.tsx +107 -0
  33. package/template/client/src/features/auth/components/LimitNotice.tsx +28 -0
  34. package/template/client/src/features/auth/components/LoginForm.tsx +13 -2
  35. package/template/client/src/features/auth/components/RegisterForm.tsx +23 -7
  36. package/template/client/src/features/auth/hooks/useAuth.ts +11 -1
  37. package/template/client/src/features/auth/hooks/usePasswordReset.ts +7 -1
  38. package/template/client/src/features/auth/services/auth.service.ts +8 -3
  39. package/template/client/src/features/auth/store/authSlice.ts +7 -2
  40. package/template/client/src/lib/api/__tests__/axios.config.test.ts +183 -0
  41. package/template/client/src/lib/api/api.types.ts +14 -0
  42. package/template/client/src/lib/api/axios.config.ts +101 -15
  43. package/template/client/src/lib/api/hard-redirect.ts +8 -0
  44. package/template/client/src/lib/api/session-events.ts +29 -0
  45. package/template/client/src/lib/constants/routes.ts +12 -0
  46. package/template/client/src/lib/observability/sentry-scrub.ts +1 -1
  47. package/template/client/src/lib/utils/__tests__/error.test.ts +119 -0
  48. package/template/client/src/lib/utils/__tests__/form.test.ts +42 -0
  49. package/template/client/src/lib/utils/__tests__/paths.test.ts +43 -0
  50. package/template/client/src/lib/utils/__tests__/security.test.ts +40 -0
  51. package/template/client/src/lib/utils/__tests__/session-marker.test.ts +27 -0
  52. package/template/client/src/lib/utils/error.ts +81 -0
  53. package/template/client/src/lib/utils/form.ts +31 -0
  54. package/template/client/src/lib/utils/paths.ts +18 -0
  55. package/template/client/src/lib/utils/session-marker.ts +24 -0
  56. package/template/client/src/{middleware.ts → proxy.ts} +25 -11
  57. package/template/client/src/store/__tests__/store.test.ts +44 -0
  58. package/template/client/src/store/index.ts +26 -8
  59. package/template/client/vitest.config.mts +48 -0
  60. package/template/server/.env.example +280 -270
  61. package/template/server/.env.example.production +283 -242
  62. package/template/server/AGENTS.md +13 -4
  63. package/template/server/Dockerfile +176 -14
  64. package/template/server/docker-compose.yml +4 -2
  65. package/template/server/docs/deployment.md +63 -0
  66. package/template/server/docs/logging.md +52 -4
  67. package/template/server/docs/rate-limits.md +153 -0
  68. package/template/server/docs/testing.md +63 -0
  69. package/template/server/eslint.config.mjs +19 -0
  70. package/template/server/package-lock.json +8784 -8743
  71. package/template/server/package.json +79 -75
  72. package/template/server/prisma/migrations/0_init/migration.sql +82 -0
  73. package/template/server/prisma/migrations/migration_lock.toml +3 -0
  74. package/template/server/prisma/schema.prisma +0 -3
  75. package/template/server/scripts/check-heic.mjs +40 -0
  76. package/template/server/scripts/check-sharp.mjs +53 -0
  77. package/template/server/src/__tests__/routes.integration.test.ts +280 -0
  78. package/template/server/src/app.ts +248 -346
  79. package/template/server/src/config/__tests__/env.test.ts +219 -0
  80. package/template/server/src/config/__tests__/rate-limit.config.test.ts +158 -0
  81. package/template/server/src/config/env.ts +326 -182
  82. package/template/server/src/config/rate-limit.config.ts +157 -106
  83. package/template/server/src/jobs/__tests__/cleanup-deleted-accounts.job.test.ts +60 -8
  84. package/template/server/src/jobs/__tests__/cleanup-expired-auth.job.test.ts +44 -0
  85. package/template/server/src/jobs/__tests__/jobs.integration.test.ts +88 -0
  86. package/template/server/src/jobs/__tests__/schedule.test.ts +92 -0
  87. package/template/server/src/jobs/cleanup-deleted-accounts.job.ts +38 -36
  88. package/template/server/src/jobs/cleanup-expired-auth.job.ts +32 -24
  89. package/template/server/src/jobs/index.ts +12 -2
  90. package/template/server/src/jobs/refresh-cloudflare-ranges.job.ts +28 -0
  91. package/template/server/src/jobs/schedule.ts +50 -0
  92. package/template/server/src/libs/__tests__/auth.test.ts +98 -7
  93. package/template/server/src/libs/__tests__/client-ip.test.ts +257 -121
  94. package/template/server/src/libs/__tests__/cloudflare-ranges.test.ts +79 -0
  95. package/template/server/src/libs/__tests__/cookies.test.ts +53 -2
  96. package/template/server/src/libs/__tests__/email-quota.test.ts +81 -0
  97. package/template/server/src/libs/__tests__/email.test.ts +68 -0
  98. package/template/server/src/libs/__tests__/error-handler.test.ts +162 -0
  99. package/template/server/src/libs/__tests__/health-db-down.integration.test.ts +37 -0
  100. package/template/server/src/libs/__tests__/health-redis-down.integration.test.ts +45 -0
  101. package/template/server/src/libs/__tests__/health-routes.test.ts +83 -0
  102. package/template/server/src/libs/__tests__/health.integration.test.ts +118 -0
  103. package/template/server/src/libs/__tests__/http.test.ts +74 -2
  104. package/template/server/src/libs/__tests__/ip-block.test.ts +179 -62
  105. package/template/server/src/libs/__tests__/logger.test.ts +50 -0
  106. package/template/server/src/libs/__tests__/monitoring.test.ts +185 -0
  107. package/template/server/src/libs/__tests__/outbound.integration.test.ts +48 -0
  108. package/template/server/src/libs/__tests__/prisma-client.test.ts +28 -0
  109. package/template/server/src/libs/__tests__/query-counter.integration.test.ts +53 -0
  110. package/template/server/src/libs/__tests__/rate-limiting.test.ts +146 -0
  111. package/template/server/src/libs/__tests__/rate-limits.integration.test.ts +53 -0
  112. package/template/server/src/libs/__tests__/redis-required.test.ts +30 -0
  113. package/template/server/src/libs/__tests__/redis.test.ts +38 -2
  114. package/template/server/src/libs/__tests__/request-id.test.ts +25 -0
  115. package/template/server/src/libs/__tests__/requestLogger.test.ts +126 -0
  116. package/template/server/src/libs/__tests__/safe-http.test.ts +138 -0
  117. package/template/server/src/libs/__tests__/sentry-scrub.test.ts +82 -80
  118. package/template/server/src/libs/__tests__/shutdown.test.ts +89 -0
  119. package/template/server/src/libs/__tests__/url-safety.test.ts +130 -80
  120. package/template/server/src/libs/auth.ts +39 -10
  121. package/template/server/src/libs/client-ip.ts +158 -77
  122. package/template/server/src/libs/cloudflare-ranges.ts +132 -0
  123. package/template/server/src/libs/cookies.ts +53 -14
  124. package/template/server/src/libs/email-quota.ts +74 -0
  125. package/template/server/src/libs/email.ts +46 -9
  126. package/template/server/src/libs/error-handler.ts +111 -0
  127. package/template/server/src/libs/health-routes.ts +53 -0
  128. package/template/server/src/libs/http.ts +139 -47
  129. package/template/server/src/libs/ip-address.ts +37 -0
  130. package/template/server/src/libs/ip-block.ts +284 -221
  131. package/template/server/src/libs/logger.ts +41 -12
  132. package/template/server/src/libs/monitoring.ts +127 -41
  133. package/template/server/src/libs/observability/sentry.ts +1 -1
  134. package/template/server/src/libs/origin-check.ts +9 -7
  135. package/template/server/src/libs/prisma.ts +47 -4
  136. package/template/server/src/libs/query-counter.ts +71 -59
  137. package/template/server/src/libs/rate-limiting.ts +110 -0
  138. package/template/server/src/libs/redis-required.ts +28 -0
  139. package/template/server/src/libs/redis.ts +27 -1
  140. package/template/server/src/libs/request-id.ts +18 -0
  141. package/template/server/src/libs/requestLogger.ts +112 -17
  142. package/template/server/src/libs/safe-http.ts +101 -0
  143. package/template/server/src/libs/shutdown.ts +87 -0
  144. package/template/server/src/libs/storage/__tests__/fixtures/tiny-heic.ts +13 -0
  145. package/template/server/src/libs/storage/__tests__/image-optimizer.test.ts +59 -0
  146. package/template/server/src/libs/storage/__tests__/multipart-limits.test.ts +91 -0
  147. package/template/server/src/libs/storage/__tests__/upload-persistence.test.ts +52 -0
  148. package/template/server/src/libs/storage/file-storage.service.ts +94 -84
  149. package/template/server/src/libs/storage/file-validator.ts +48 -46
  150. package/template/server/src/libs/storage/image-optimizer.service.ts +16 -5
  151. package/template/server/src/libs/token-hash.ts +18 -0
  152. package/template/server/src/libs/url-safety.ts +127 -121
  153. package/template/server/src/libs/validation-errors.ts +86 -0
  154. package/template/server/src/modules/admin/__tests__/admin-actions.integration.test.ts +255 -0
  155. package/template/server/src/modules/admin/__tests__/admin.health.test.ts +82 -0
  156. package/template/server/src/modules/admin/__tests__/admin.repo.test.ts +55 -0
  157. package/template/server/src/modules/admin/__tests__/admin.schemas.test.ts +38 -0
  158. package/template/server/src/modules/admin/__tests__/admin.service.test.ts +162 -0
  159. package/template/server/src/modules/admin/admin.controller.ts +66 -49
  160. package/template/server/src/modules/admin/admin.repo.ts +49 -10
  161. package/template/server/src/modules/admin/admin.routes.ts +48 -2
  162. package/template/server/src/modules/admin/admin.schemas.ts +18 -6
  163. package/template/server/src/modules/admin/admin.service.ts +66 -4
  164. package/template/server/src/modules/auth/__tests__/auth.controller.test.ts +39 -0
  165. package/template/server/src/modules/auth/__tests__/auth.integration.test.ts +9 -3
  166. package/template/server/src/modules/auth/__tests__/auth.service.dummy-hash.test.ts +34 -0
  167. package/template/server/src/modules/auth/__tests__/auth.service.test.ts +197 -37
  168. package/template/server/src/modules/auth/__tests__/session-cleanup.test.ts +39 -0
  169. package/template/server/src/modules/auth/__tests__/sessions.integration.test.ts +246 -0
  170. package/template/server/src/modules/auth/__tests__/token-fingerprints.test.ts +119 -0
  171. package/template/server/src/modules/auth/auth.controller.ts +133 -128
  172. package/template/server/src/modules/auth/auth.repo.ts +44 -44
  173. package/template/server/src/modules/auth/auth.schemas.ts +4 -2
  174. package/template/server/src/modules/auth/auth.service.ts +167 -96
  175. package/template/server/src/modules/auth/session.repo.ts +12 -0
  176. package/template/server/src/modules/files/__tests__/files.integration.test.ts +5 -5
  177. package/template/server/src/modules/users/__tests__/avatar-upload.test.ts +120 -0
  178. package/template/server/src/modules/users/__tests__/name-rules.test.ts +46 -0
  179. package/template/server/src/modules/users/__tests__/users.integration.test.ts +295 -0
  180. package/template/server/src/modules/users/__tests__/users.repo.test.ts +37 -0
  181. package/template/server/src/modules/users/__tests__/users.service.test.ts +129 -0
  182. package/template/server/src/modules/users/users.controller.ts +9 -38
  183. package/template/server/src/modules/users/users.repo.ts +23 -7
  184. package/template/server/src/modules/users/users.routes.ts +3 -22
  185. package/template/server/src/modules/users/users.schemas.ts +15 -9
  186. package/template/server/src/modules/users/users.service.ts +105 -93
  187. package/template/server/src/scripts/make-admin.ts +48 -0
  188. package/template/server/src/server.ts +48 -26
  189. package/template/server/src/shared/errors/AppError.ts +27 -1
  190. package/template/server/src/shared/errors/errors.ts +42 -5
  191. package/template/server/src/shared/responses/successResponse.ts +22 -3
  192. package/template/server/src/shared/types/index.ts +7 -0
  193. package/template/server/src/test/__tests__/integration-services.test.ts +66 -0
  194. package/template/server/src/test/integration-helpers.ts +343 -0
  195. package/template/server/src/test/integration-services.ts +74 -0
  196. package/template/server/src/test/integration.global-setup.ts +144 -0
  197. package/template/server/src/test/integration.setup.ts +35 -158
  198. package/template/server/src/test/setup.ts +3 -10
  199. package/template/server/tsconfig.build.json +1 -1
  200. package/template/server/tsconfig.json +1 -0
  201. package/template/server/vitest.config.ts +17 -4
  202. package/template/server/vitest.integration.config.ts +18 -13
  203. package/template/server/src/libs/storage/filename-sanitizer.ts +0 -71
@@ -1,242 +1,283 @@
1
- # ===================================================================
2
- # PRODUCTION ENVIRONMENT CONFIGURATION
3
- # ===================================================================
4
- # This file contains production-optimized settings for high-traffic
5
- # deployments (10K-100K users/day). Copy to .env and customize.
6
- #
7
- # COOLIFY DEPLOYMENT NOTE:
8
- # When adding environment variables in Coolify, do NOT check
9
- # "Available at Buildtime" for any variable unless explicitly noted.
10
- # The server Dockerfile handles build-time config internally.
11
- # Secrets (DATABASE_URL, JWT_SECRET, COOKIE_SECRET, REDIS_URL) must
12
- # NEVER be available at buildtime — they get baked into the image layer.
13
- #
14
- # ===================================================================
15
-
16
- # ===================================================================
17
- # APPLICATION
18
- # ===================================================================
19
-
20
- # COOLIFY: Do NOT check "Available at Buildtime" — the Dockerfile
21
- # sets NODE_ENV=development during build to ensure devDependencies install.
22
- NODE_ENV=production
23
- PORT=3000
24
- HOST=0.0.0.0
25
-
26
- # ===================================================================
27
- # SERVER TIMEOUTS
28
- # ===================================================================
29
-
30
- # Fastify request timeout in milliseconds (default: 30000 = 30s)
31
- # Long-running routes (LLM calls, big exports) may need 180000+ (180s).
32
- # CRITICAL: the reverse proxy (Nginx/Coolify) timeout must be raised to
33
- # match, or the proxy cuts the connection before the server does.
34
- REQUEST_TIMEOUT_MS=30000
35
-
36
- # Fastify connection timeout in milliseconds (default: 60000 = 60s)
37
- CONNECTION_TIMEOUT_MS=60000
38
-
39
- # ===================================================================
40
- # DATABASE (MySQL 8.0+)
41
- # ===================================================================
42
-
43
- # Production database connection
44
- # CRITICAL: Use secure credentials, SSL/TLS, and private network
45
- # COOLIFY: Runtime only. Do NOT check "Available at Buildtime".
46
- DATABASE_URL="mysql://prod_user:SECURE_PASSWORD@db.internal:3306/prod_db?ssl=true"
47
-
48
- # Connection pool for high traffic (10K-100K users/day)
49
- # Recommended: 20-50 connections for production
50
- # Monitor and adjust based on load testing results
51
- DATABASE_POOL_MIN=10
52
- DATABASE_POOL_MAX=50
53
-
54
- # ===================================================================
55
- # REDIS
56
- # ===================================================================
57
-
58
- # Production Redis instance
59
- # CRITICAL: Use authentication and private network
60
- # COOLIFY: Runtime only. Do NOT check "Available at Buildtime".
61
- REDIS_URL="redis://:REDIS_PASSWORD@redis.internal:6379"
62
-
63
- # Production retry settings
64
- REDIS_MAX_RETRIES=5
65
- REDIS_CONNECT_TIMEOUT=5000
66
-
67
- # ===================================================================
68
- # RATE LIMITING
69
- # ===================================================================
70
-
71
- # Always enabled in production
72
- RATE_LIMIT_ENABLED=true
73
-
74
- # Production: keep at 1 (default limits are tuned for production)
75
- RATE_LIMIT_MULTIPLIER=1
76
-
77
- # Tight limits for sensitive auth routes in production
78
- RATE_LIMIT_AUTH_LOGIN_MAX=10
79
- RATE_LIMIT_AUTH_REGISTER_MAX=5
80
-
81
- # Trust Cloudflare's CF-Connecting-IP header for the real client IP (default: false)
82
- # Used ONLY for rate-limiting and IP auto-block decisions. Behind Cloudflare,
83
- # request.ip is a CF edge IP — without this, all users behind one edge collapse
84
- # onto a single IP and can rate-limit or auto-ban each other.
85
- # COOLIFY: Runtime only. Do NOT check "Available at Buildtime".
86
- # SECURITY: the header is client-spoofable. Set true ONLY when this origin
87
- # accepts traffic exclusively via Cloudflare (direct origin access is blocked
88
- # at the firewall/network so clients cannot reach it bypassing CF).
89
- # Note: the left-most X-Forwarded-For entry is now trusted as the client IP
90
- # regardless of this flag (covers grey-cloud / DNS-only), so the origin must be
91
- # proxy-locked (firewall direct access) in production either way.
92
- TRUST_CLOUDFLARE=false
93
-
94
- # ===================================================================
95
- # IP AUTO-BLOCK
96
- # ===================================================================
97
-
98
- # Rate-limit violations before an IP is auto-blocked for every route.
99
- # Keep HIGH enough that a retry-looping legitimate client or a NAT'd
100
- # office sharing one egress IP cannot self-ban (sustained abuse only).
101
- # See src/config/rate-limit.config.ts before lowering.
102
- IP_AUTO_BLOCK_THRESHOLD=20
103
-
104
- # Sliding window for counting violations, in seconds (5 minutes)
105
- IP_AUTO_BLOCK_WINDOW_SECONDS=300
106
-
107
- # How long an auto-blocked IP stays blocked, in seconds (1 hour)
108
- IP_AUTO_BLOCK_DURATION_SECONDS=3600
109
-
110
- # ===================================================================
111
- # ACCOUNT ACTIVATION
112
- # ===================================================================
113
-
114
- # Require account verification before users can log in
115
- # Set to true in production for security
116
- REQUIRE_USER_VERIFICATION=true
117
-
118
- # ===================================================================
119
- # FILE UPLOAD
120
- # ===================================================================
121
-
122
- # Maximum file upload size in MB
123
- MAX_FILE_SIZE_MB=10
124
-
125
- # Two-tier upload storage. BOTH optional — defaults are <cwd>/uploads/public and
126
- # <cwd>/uploads/private. Keep them UNDER /app/uploads (the Coolify volume below)
127
- # so files survive redeploys.
128
- # PUBLIC tier: static files at /uploads/ — viewable by ANYONE (e.g. avatars).
129
- # PRIVATE tier: OUTSIDE the static root; served ONLY via the auth-gated,
130
- # owner-scoped route GET /api/v1/files/:filename.
131
- # UPLOAD_PUBLIC_DIR=/app/uploads/public
132
- # UPLOAD_PRIVATE_DIR=/app/uploads/private
133
-
134
- # COOLIFY PERSISTENT STORAGE (required for uploads to survive redeployments):
135
- # Go to your service in Coolify → Storages → Add Volume Mount:
136
- # Name: uploads (or <project-name>-uploads)
137
- # Source Path: (leave empty — Coolify manages the Docker volume)
138
- # Destination Path: /app/uploads
139
- # Without this, all uploaded files are lost on every redeployment.
140
-
141
- # ===================================================================
142
- # JWT AUTHENTICATION
143
- # ===================================================================
144
-
145
- # CRITICAL: Generate a cryptographically secure secret: openssl rand -hex 48
146
- # The value below is EXAMPLE TEXT, not a real secret. Every committed example
147
- # secret starts with CHANGE_ME, and the server refuses to boot with it
148
- # (src/config/env.ts), so a forgotten placeholder fails loudly.
149
- # COOLIFY: Runtime only. Do NOT check "Available at Buildtime" — this is a secret!
150
- JWT_SECRET="CHANGE_ME_generate_with_openssl_rand_hex_48"
151
-
152
- # Production token expiry (tighter security)
153
- JWT_ACCESS_EXPIRY="15m"
154
- JWT_REFRESH_EXPIRY="7d"
155
-
156
- # Cookie signing secret (separate from JWT for defense-in-depth).
157
- # EXAMPLE TEXT: generate a different value with openssl rand -hex 48.
158
- # COOLIFY: Runtime only. Do NOT check "Available at Buildtime" — this is a secret!
159
- COOKIE_SECRET="CHANGE_ME_generate_with_openssl_rand_hex_48"
160
-
161
- # ===================================================================
162
- # CORS
163
- # ===================================================================
164
-
165
- # REQUIRED in production - your frontend domain(s)
166
- # Multiple origins separated by commas
167
- CORS_ORIGIN="https://yourdomain.com,https://app.yourdomain.com"
168
-
169
- # Cookie domain for cross-origin deployments
170
- # REQUIRED when client and API are on different subdomains:
171
- # Client: https://app.example.com | API: https://api.example.com
172
- # → Set COOKIE_DOMAIN=".example.com" (leading dot = all subdomains)
173
- # Only the non-secret auth_session indicator uses this domain, so the client's
174
- # middleware can see a session exists. The access/refresh token cookies stay
175
- # host-only on the API and are never sent to other subdomains.
176
- # Without it, the client keeps redirecting signed-in users to /login.
177
- COOKIE_DOMAIN=".yourdomain.com"
178
-
179
- # ===================================================================
180
- # EMAIL (Resend)
181
- # ===================================================================
182
-
183
- # Resend API key for transactional emails
184
- # COOLIFY: Runtime only. Do NOT check "Available at Buildtime" — this is a secret!
185
- RESEND_API_KEY="re_xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
186
-
187
- # Sender email — must be from a verified domain in Resend
188
- RESEND_FROM_EMAIL="noreply@yourdomain.com"
189
-
190
- # Frontend URL for email links (password reset, verification, etc.)
191
- CLIENT_URL="https://yourdomain.com"
192
-
193
- # ===================================================================
194
- # LOGGING
195
- # ===================================================================
196
-
197
- # Production: info or warn (reduces log volume)
198
- # Use warn to reduce costs in high-traffic scenarios
199
- LOG_LEVEL=info
200
-
201
- # ===================================================================
202
- # DATABASE SEEDING
203
- # ===================================================================
204
-
205
- # DO NOT SEED PRODUCTION. The seed script (npm run prisma:seed) creates
206
- # well-known demo accounts (admin@example.com) and hard-refuses to run when
207
- # NODE_ENV=production. These variables exist only to keep .env files in sync
208
- # across environments — leave them commented out in production.
209
- # SEED_ADMIN_PASSWORD=
210
- # SEED_USER_PASSWORD=
211
-
212
- # ===================================================================
213
- # ERROR TRACKING
214
- # ===================================================================
215
-
216
- # Sentry for production error monitoring (leave unset to disable — fully inert)
217
- # Get your DSN from: https://sentry.io/settings/projects/
218
- SENTRY_DSN="https://YOUR_PUBLIC_KEY@o0.ingest.sentry.io/YOUR_PROJECT_ID"
219
-
220
- # Fraction of transactions sampled for performance tracing (0.0–1.0, default 0.1).
221
- SENTRY_TRACES_SAMPLE_RATE=0.1
222
-
223
- # Environment tag on Sentry events (defaults to NODE_ENV when unset).
224
- SENTRY_ENVIRONMENT=production
225
-
226
- # ===================================================================
227
- # PRODUCTION CHECKLIST
228
- # ===================================================================
229
- # [ ] DATABASE_URL uses secure credentials and SSL
230
- # [ ] JWT_SECRET is a strong random string (openssl rand -hex 48), not the CHANGE_ME example
231
- # [ ] COOKIE_SECRET is a different strong random string, not the CHANGE_ME example
232
- # [ ] CORS_ORIGIN is set to your exact frontend URL(s)
233
- # [ ] COOKIE_DOMAIN is set if client and API are on different subdomains
234
- # [ ] REDIS_URL uses authentication if Redis is exposed
235
- # [ ] LOG_LEVEL is set to info or warn
236
- # [ ] SENTRY_DSN is configured for error tracking
237
- # [ ] Database connection pool tested under load
238
- # [ ] All secrets are stored in environment variables, not in code
239
- # [ ] SSL/TLS certificates are configured
240
- # [ ] Firewall rules restrict access to database and Redis
241
- # [ ] In Coolify: NO secrets have "Available at Buildtime" checked
242
- # [ ] In Coolify: NODE_ENV does NOT have "Available at Buildtime" checked
1
+ # ===================================================================
2
+ # PRODUCTION ENVIRONMENT CONFIGURATION
3
+ # ===================================================================
4
+ # This file contains production-optimized settings for high-traffic
5
+ # deployments (10K-100K users/day). Copy to .env and customize.
6
+ #
7
+ # COOLIFY DEPLOYMENT NOTE:
8
+ # When adding environment variables in Coolify, do NOT check
9
+ # "Available at Buildtime" for any variable unless explicitly noted.
10
+ # The server Dockerfile handles build-time config internally.
11
+ # Secrets (DATABASE_URL, JWT_SECRET, COOKIE_SECRET, REDIS_URL) must
12
+ # NEVER be available at buildtime — they get baked into the image layer.
13
+ #
14
+ # ===================================================================
15
+
16
+ # ===================================================================
17
+ # APPLICATION
18
+ # ===================================================================
19
+
20
+ # COOLIFY: Do NOT check "Available at Buildtime" — the Dockerfile
21
+ # sets NODE_ENV=development during build to ensure devDependencies install.
22
+ NODE_ENV=production
23
+ # Keep 8000: the Dockerfile EXPOSEs it, its HEALTHCHECK and Coolify's port setting use it.
24
+ PORT=8000
25
+ HOST=0.0.0.0
26
+
27
+ # ===================================================================
28
+ # SERVER TIMEOUTS
29
+ # ===================================================================
30
+
31
+ # Fastify request timeout in milliseconds (default: 30000 = 30s)
32
+ # Long-running routes (LLM calls, big exports) may need 180000+ (180s).
33
+ # CRITICAL: the reverse proxy (Nginx/Coolify) timeout must be raised to
34
+ # match, or the proxy cuts the connection before the server does.
35
+ REQUEST_TIMEOUT_MS=30000
36
+
37
+ # Fastify connection timeout in milliseconds (default: 60000 = 60s)
38
+ CONNECTION_TIMEOUT_MS=60000
39
+
40
+ # ===================================================================
41
+ # DATABASE (MySQL 8.4 LTS; 8.0+ works)
42
+ # ===================================================================
43
+
44
+ # Production database connection.
45
+ # COOLIFY: Runtime only. Do NOT check "Available at Buildtime".
46
+ # A MySQL on Coolify's own network: use its internal host name (shown on the
47
+ # database's page in Coolify), a dedicated user and password. That network is
48
+ # private, so no TLS parameter is needed. A wrong URL fails the deploy: the
49
+ # container runs `prisma migrate deploy` before the server starts.
50
+ DATABASE_URL="mysql://app_user:CHANGE_ME_password@mysql-internal-host:3306/app_db?connection_limit=5"
51
+
52
+ # A MySQL OUTSIDE Coolify's network (another server, a managed database):
53
+ # require TLS with Prisma's sslaccept argument (and sslcert=<path> when the
54
+ # server uses its own CA). `?ssl=true` is not a Prisma argument and does
55
+ # nothing. For example:
56
+ # DATABASE_URL="mysql://app_user:CHANGE_ME_password@db.example.com:3306/app_db?connection_limit=5&sslaccept=strict"
57
+
58
+ # Connection pool: sized INSIDE DATABASE_URL (Prisma 6 has no separate pool
59
+ # variables): ?connection_limit=N&pool_timeout=S. Without connection_limit
60
+ # Prisma opens (physical CPU cores x 2) + 1 connections per server process, so
61
+ # a few apps on one shared MySQL can exhaust its max_connections; 5 is plenty
62
+ # for one app's typical load. Keep the total of all processes below the
63
+ # database's max_connections.
64
+
65
+ # ===================================================================
66
+ # REDIS
67
+ # ===================================================================
68
+
69
+ # Production Redis instance. REQUIRED: the server refuses to start in production
70
+ # without it (rate limits, login lockout and IP blocks all live in Redis), and
71
+ # also when it cannot reach it within 10 s at boot (a wrong host or password
72
+ # fails the deploy instead of running with those protections off; on Coolify
73
+ # the old container keeps serving). Outages after boot are not fatal.
74
+ # CRITICAL: Use authentication and private network
75
+ # COOLIFY: Runtime only. Do NOT check "Available at Buildtime".
76
+ REDIS_URL="redis://:REDIS_PASSWORD@redis.internal:6379"
77
+
78
+ # Production retry settings
79
+ REDIS_MAX_RETRIES=5
80
+ REDIS_CONNECT_TIMEOUT=5000
81
+
82
+ # ===================================================================
83
+ # RATE LIMITING
84
+ # ===================================================================
85
+
86
+ # Always enabled in production. On/off switches accept only true or false.
87
+ RATE_LIMIT_ENABLED=true
88
+
89
+ # Production: keep at 1 (default limits are tuned for production)
90
+ RATE_LIMIT_MULTIPLIER=1
91
+
92
+ # Change any single limit: NAME=MAX/WINDOW, comma-separated, WINDOW like 30s, 15m, 1h.
93
+ # NAME = a key of RATE_LIMITS in src/config/rate-limit.config.ts, DEFAULT (every
94
+ # route without its own limit, 200/1m) or EMAIL_PER_ADDRESS (emails to one address,
95
+ # 3/1h). A typo stops the server at boot. The multiplier still applies on top.
96
+ # All limits, the lockout, the email cap and the auto-block: docs/rate-limits.md
97
+ # Example: RATE_LIMIT_OVERRIDES=AUTH_LOGIN=200/15m,DEFAULT=300/1m
98
+ RATE_LIMIT_OVERRIDES=
99
+
100
+ # Client IP: nothing to configure. The server trusts only its own proxy (Coolify's
101
+ # Traefik) and recognises Cloudflare by its published edge ranges, so rate limits and
102
+ # IP blocks see the real visitor both behind Cloudflare and without it. A leftover
103
+ # TRUST_CLOUDFLARE variable is ignored. See docs/client-ip-behind-proxies.md.
104
+ #
105
+ # Recommended behind Cloudflare: prove requests came through YOUR zone. Add a
106
+ # Cloudflare Transform Rule (Rules > Transform Rules > Modify Request Header) that
107
+ # sets the static header X-Origin-Verify to this value for the app's hostname. Then
108
+ # Cloudflare's visitor header is believed only on requests that carry it, so another
109
+ # Cloudflare customer's Worker cannot forge visitor addresses. Generate:
110
+ # openssl rand -hex 32
111
+ # COOLIFY: Runtime only. Do NOT check "Available at Buildtime". Leave empty if unused.
112
+ CLOUDFLARE_ORIGIN_SECRET=
113
+
114
+ # ===================================================================
115
+ # IP AUTO-BLOCK
116
+ # ===================================================================
117
+
118
+ # Rate-limit violations before an IP is auto-blocked for every route.
119
+ # Keep HIGH enough that a retry-looping legitimate client or a NAT'd
120
+ # office sharing one egress IP cannot self-ban (sustained abuse only).
121
+ # See src/config/rate-limit.config.ts before lowering.
122
+ IP_AUTO_BLOCK_THRESHOLD=20
123
+
124
+ # Sliding window for counting violations, in seconds (5 minutes)
125
+ IP_AUTO_BLOCK_WINDOW_SECONDS=300
126
+
127
+ # How long an auto-blocked IP stays blocked, in seconds (1 hour)
128
+ IP_AUTO_BLOCK_DURATION_SECONDS=3600
129
+
130
+ # ===================================================================
131
+ # ACCOUNT ACTIVATION
132
+ # ===================================================================
133
+
134
+ # Require email verification before users can sign in. true only with the
135
+ # email-verification module (it switches this to true when added): without it
136
+ # there is no verify route and nobody could sign in. Needs RESEND_API_KEY.
137
+ REQUIRE_USER_VERIFICATION=false
138
+
139
+ # ===================================================================
140
+ # FILE UPLOAD
141
+ # ===================================================================
142
+
143
+ # Maximum file upload size in MB
144
+ MAX_FILE_SIZE_MB=10
145
+
146
+ # Two-tier upload storage. BOTH optional — defaults are <cwd>/uploads/public and
147
+ # <cwd>/uploads/private. Keep them UNDER /app/uploads (the Coolify volume below)
148
+ # so files survive redeploys.
149
+ # PUBLIC tier: static files at /uploads/ — viewable by ANYONE (e.g. avatars).
150
+ # PRIVATE tier: OUTSIDE the static root; served ONLY via the auth-gated,
151
+ # owner-scoped route GET /api/v1/files/:filename.
152
+ # UPLOAD_PUBLIC_DIR=/app/uploads/public
153
+ # UPLOAD_PRIVATE_DIR=/app/uploads/private
154
+
155
+ # COOLIFY PERSISTENT STORAGE (required for uploads to survive redeployments):
156
+ # Go to your service in Coolify → Storages → Add Volume Mount:
157
+ # Name: uploads (or <project-name>-uploads)
158
+ # Source Path: (leave empty — Coolify manages the Docker volume)
159
+ # Destination Path: /app/uploads
160
+ # Without this, all uploaded files are lost on every redeployment. The server
161
+ # logs a "[STORAGE] ... is not on a mounted volume" warning at boot when it
162
+ # runs in a production container without it.
163
+
164
+ # ===================================================================
165
+ # JWT AUTHENTICATION
166
+ # ===================================================================
167
+
168
+ # CRITICAL: Generate a cryptographically secure secret: openssl rand -hex 48
169
+ # The value below is EXAMPLE TEXT, not a real secret. Every committed example
170
+ # secret starts with CHANGE_ME, and the server refuses to boot with it
171
+ # (src/config/env.ts), so a forgotten placeholder fails loudly.
172
+ # COOLIFY: Runtime only. Do NOT check "Available at Buildtime" — this is a secret!
173
+ JWT_SECRET="CHANGE_ME_generate_with_openssl_rand_hex_48"
174
+
175
+ # Production token expiry (tighter security)
176
+ JWT_ACCESS_EXPIRY="15m"
177
+ JWT_REFRESH_EXPIRY="7d"
178
+
179
+ # Cookie signing secret (separate from JWT for defense-in-depth).
180
+ # EXAMPLE TEXT: generate a different value with openssl rand -hex 48.
181
+ # COOLIFY: Runtime only. Do NOT check "Available at Buildtime" — this is a secret!
182
+ COOKIE_SECRET="CHANGE_ME_generate_with_openssl_rand_hex_48"
183
+
184
+ # ===================================================================
185
+ # CORS
186
+ # ===================================================================
187
+
188
+ # REQUIRED in production - your frontend domain(s)
189
+ # Multiple origins separated by commas. Bare origins only (https://host,
190
+ # no path, no trailing slash, no *): the server refuses anything else at boot.
191
+ # Use https: the login cookies are Secure in production, and browsers drop them
192
+ # over plain http (the server warns at boot when it sees http:// here).
193
+ CORS_ORIGIN="https://yourdomain.com,https://app.yourdomain.com"
194
+
195
+ # Cookie domain when client and API run on sibling subdomains of one domain
196
+ # (the only split setup supported: the cookies stay SameSite Strict/Lax).
197
+ # REQUIRED when client and API are on different subdomains:
198
+ # Client: https://app.example.com | API: https://api.example.com
199
+ # → Set COOKIE_DOMAIN=".example.com" (leading dot = all subdomains)
200
+ # Only the non-secret auth_session indicator uses this domain, so the client's
201
+ # middleware can see a session exists. The access/refresh token cookies stay
202
+ # host-only on the API and are never sent to other subdomains.
203
+ # Without it, the client keeps redirecting signed-in users to /login.
204
+ COOKIE_DOMAIN=".yourdomain.com"
205
+
206
+ # ===================================================================
207
+ # EMAIL (Resend)
208
+ # ===================================================================
209
+
210
+ # Resend API key for transactional emails
211
+ # COOLIFY: Runtime only. Do NOT check "Available at Buildtime" — this is a secret!
212
+ RESEND_API_KEY="CHANGE_ME_your_resend_api_key"
213
+
214
+ # Sender email — must be from a verified domain in Resend
215
+ # With a Resend key set, the server refuses to start in production while the key
216
+ # or this sender is still the placeholder, and warns on Resend's test sender
217
+ # onboarding@resend.dev (it delivers only to your own Resend account's address).
218
+ RESEND_FROM_EMAIL="noreply@yourdomain.com"
219
+
220
+ # Frontend URL for email links (password reset, verification, etc.).
221
+ # Required in production when RESEND_API_KEY is set: the server refuses to start
222
+ # without it, since every email link would otherwise point at localhost.
223
+ CLIENT_URL="https://yourdomain.com"
224
+
225
+ # ===================================================================
226
+ # LOGGING
227
+ # ===================================================================
228
+
229
+ # Production: info or warn (reduces log volume)
230
+ # Use warn to reduce costs in high-traffic scenarios
231
+ LOG_LEVEL=info
232
+
233
+ # ===================================================================
234
+ # DATABASE SEEDING
235
+ # ===================================================================
236
+
237
+ # DO NOT SEED PRODUCTION. The seed script (npm run prisma:seed) creates
238
+ # well-known demo accounts (admin@example.com) and hard-refuses to run when
239
+ # NODE_ENV=production. These variables exist only to keep .env files in sync
240
+ # across environments — leave them commented out in production.
241
+ # SEED_ADMIN_PASSWORD=
242
+ # SEED_USER_PASSWORD=
243
+
244
+ # ===================================================================
245
+ # ERROR TRACKING
246
+ # ===================================================================
247
+
248
+ # Sentry for production error monitoring (leave unset to disable — fully inert)
249
+ # Get your DSN from: https://sentry.io/settings/projects/
250
+ SENTRY_DSN="https://YOUR_PUBLIC_KEY@o0.ingest.sentry.io/YOUR_PROJECT_ID"
251
+
252
+ # Fraction of transactions sampled for performance tracing (0.0–1.0, default 0.1).
253
+ SENTRY_TRACES_SAMPLE_RATE=0.1
254
+
255
+ # Environment tag on Sentry events (defaults to NODE_ENV when unset).
256
+ SENTRY_ENVIRONMENT=production
257
+
258
+ # ===================================================================
259
+ # DEPLOYMENT
260
+ # ===================================================================
261
+
262
+ # SOURCE_COMMIT: the deployed git commit, set by Coolify (a predefined
263
+ # variable). Do not set it yourself. Shown as "commit" in
264
+ # GET /api/v1/admin/health; null there when it is not set.
265
+
266
+ # ===================================================================
267
+ # PRODUCTION CHECKLIST
268
+ # ===================================================================
269
+ # [ ] DATABASE_URL uses a dedicated user, a strong password and connection_limit
270
+ # (TLS via sslaccept=strict when the database is outside Coolify's network)
271
+ # [ ] JWT_SECRET is a strong random string (openssl rand -hex 48), not the CHANGE_ME example
272
+ # [ ] COOKIE_SECRET is a different strong random string, not the CHANGE_ME example
273
+ # [ ] CORS_ORIGIN is set to your exact frontend URL(s)
274
+ # [ ] COOKIE_DOMAIN is set if client and API are on different subdomains
275
+ # [ ] REDIS_URL uses authentication if Redis is exposed
276
+ # [ ] LOG_LEVEL is set to info or warn
277
+ # [ ] SENTRY_DSN is configured for error tracking
278
+ # [ ] Database connection pool tested under load (all apps together stay below max_connections)
279
+ # [ ] All secrets are stored in environment variables, not in code
280
+ # [ ] SSL/TLS certificates are configured
281
+ # [ ] Firewall rules restrict access to database and Redis
282
+ # [ ] In Coolify: NO secrets have "Available at Buildtime" checked
283
+ # [ ] In Coolify: NODE_ENV does NOT have "Available at Buildtime" checked
@@ -1,4 +1,4 @@
1
- # Server Codex instructions
1
+ # Server agent instructions
2
2
 
3
3
  ## Architecture and naming
4
4
 
@@ -6,23 +6,32 @@
6
6
  - Domain modules live in `src/modules/<domain>/` with `<domain>.routes.ts`, `.controller.ts`, `.service.ts`, `.repo.ts`, and `.schemas.ts`.
7
7
  - All API routes use the `/api/v1` prefix. Use the shared `successResponse`, `paginatedResponse`, and typed `AppError` contracts rather than custom response/error shapes.
8
8
  - Use the shared logger and outbound HTTP client. Never add ad-hoc `console.log`, raw `fetch`, or per-call HTTP clients that bypass redaction, timeouts, and error mapping.
9
+ - Fetch a URL that came from a user (a link, webhook, import or avatar URL) only with `safeHttpClient` (`src/libs/safe-http.ts`), never `httpClient` or `fetch`: it refuses internal addresses on every hop.
10
+ - Recurring work (cleanups, syncs, reminders) goes through `startRecurringJob` (`src/jobs/schedule.ts`), registered in `src/jobs/index.ts`, never a bare `setInterval`: it first runs about a minute after every start, so frequent deploys never postpone it, and a run never overlaps the previous one.
9
11
 
10
12
  ## Sources of truth
11
13
 
12
- - `prisma/schema.prisma` owns models, relations, indexes, and database naming. Change schema through reviewed Prisma migrations; production is forward-only with `prisma migrate deploy`.
14
+ - `prisma/schema.prisma` owns models, relations, indexes, and database naming. Change schema through reviewed Prisma migrations; production is forward-only with `prisma migrate deploy`. `prisma/migrations/0_init` (shipped with the template) creates the first tables; never edit or delete an applied migration, add a new one with `npm run prisma:migrate:dev`. Every deploy applies new migrations while the old version is still serving (`docs/deployment.md`): never drop, rename, retype or make a column required in the same deploy as the code that stops using it. Prisma writes a renamed field as drop + add, which loses the data; to rename only in the code, keep the database name with `@map` / `@@map`.
13
15
  - `src/config/env.ts` owns runtime configuration validation. Keep `.env.example` and `.env.example.production` synchronized, but keep real secrets out of the repository and image.
14
16
  - Zod route schemas own external input validation. Services own authorization and business invariants; repositories must not decide access policy.
17
+ - Wrong input has one answer: 422 `VALIDATION_FAILED` with `error.fields` (`src/libs/validation-errors.ts`, applied by `src/libs/error-handler.ts` to route schemas and `ZodError`). Validate inside a handler with `parseInput(schema, data)`, never a hand-made `ValidationError(issues[0].message)`. Messages describe the rule; never put a submitted value into one.
15
18
 
16
19
  ## Security and deployment
17
20
 
18
21
  - Preserve httpOnly cookie auth, production CORS allowlists, state-changing request origin checks, rate limiting, safe client IP handling, generic 500 responses, and server-side-only diagnostic logging.
22
+ - Rate limits have one source, `src/config/rate-limit.config.ts` (`docs/rate-limits.md` explains all of them). A new route gets the 200/min default automatically; give it its own `RATE_LIMITS` entry only when it needs another number. Every "slow down" answer must be a `TooManyRequestsError` (or `IP_BLOCKED`) carrying `retryAfterSeconds`. Any new flow that sends email anyone can trigger calls `consumeEmailQuota(email)` before looking up the account (public counter); only flows that prove the owner (e.g. a correct password) may use their own counter.
19
23
  - Use Prisma parameterization and explicit safe projections; never expose password hashes, tokens, internal errors, or secret-bearing upstream payloads.
20
24
  - Keep public uploads under the static public root. Private uploads stay outside it and are served only through authenticated, owner-scoped routes; preserve filename/path validation.
21
- - The production image runs non-root, applies `prisma migrate deploy` before `node dist/server.js`, and probes unauthenticated `GET /api/v1/live`. Preserve writable ownership for runtime storage and add explicit Alpine packages for new native runtime dependencies.
25
+ - The production image runs non-root, refuses to start without any migration, applies `prisma migrate deploy` before `node dist/server.js`, and probes unauthenticated `GET /api/v1/live`. In production the server also refuses to start when Redis is not reachable within 10 s (`src/server.ts`): keep that refusal, since rate limits, login lockout and IP blocks would otherwise be off. Preserve writable ownership for runtime storage and add explicit Alpine packages for new native runtime dependencies.
26
+
27
+ ## Tests
28
+
29
+ - Integration tests run the real app against MySQL and Redis (see the root `AGENTS.md`, Verification, for what they need). Build them from `src/test/integration-helpers.ts` (`buildTestApp`, `call`, `registerUser`, `registerAdmin`, `startEmailStub`, …) instead of new setup code.
30
+ - Every API route needs its contract in `src/__tests__/routes.integration.test.ts`: who may call it (401 anonymous, 403 the wrong user or role), wrong input (422 `VALIDATION_FAILED`) and its success answer. Its last test fails, naming the route, when a registered route is never called there; a route that another file tests goes in its `COVERED_ELSEWHERE` list with that file's name.
22
31
 
23
32
  ## Mandatory security completion gate
24
33
 
25
- - Follow the root `AGENTS.md` security workflow for every meaningful server change: use `security-audit` guidance during implementation and run `codex-security:security-diff-scan` on the resulting patch when the Codex Security plugin is available.
34
+ - Follow the root `AGENTS.md` security workflow for every meaningful server change: use `security-audit` guidance during implementation, then review the resulting patch (`git diff HEAD` plus untracked files) before completion, with `codex-security:security-diff-scan` in Codex or the bundled `security-audit` skill in Claude Code. Not `/security-review`: it reviews only commits since `origin/HEAD` and never sees uncommitted work.
26
35
  - Review the concrete trust boundary, attacker-controlled input, affected principal/resource, authorization decision, validation point, and observable impact. Missing best practices without a reachable boundary failure are hardening notes, not confirmed vulnerabilities.
27
36
  - Add or update focused regression coverage for changed authorization, validation, origin/CORS, cookie/session, upload/path, outbound-request, and data-isolation behavior. Never rely on client checks as security evidence.
28
37
  - If a required security check cannot run, state the exact blocker and safe follow-up instead of reporting the server change as fully verified.