@engjts/nexus 0.1.8 → 0.1.9
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.
- package/package.json +1 -1
- package/BENCHMARK_REPORT.md +0 -343
- package/documentation/01-getting-started.md +0 -240
- package/documentation/02-context.md +0 -335
- package/documentation/03-routing.md +0 -397
- package/documentation/04-middleware.md +0 -483
- package/documentation/05-validation.md +0 -514
- package/documentation/06-error-handling.md +0 -465
- package/documentation/07-performance.md +0 -364
- package/documentation/08-adapters.md +0 -470
- package/documentation/09-api-reference.md +0 -548
- package/documentation/10-examples.md +0 -582
- package/documentation/11-deployment.md +0 -477
- package/documentation/12-sentry.md +0 -620
- package/documentation/13-sentry-data-storage.md +0 -996
- package/documentation/14-sentry-data-reference.md +0 -457
- package/documentation/15-sentry-summary.md +0 -409
- package/documentation/16-alerts-system.md +0 -745
- package/documentation/17-alert-adapters.md +0 -696
- package/documentation/18-alerts-implementation-summary.md +0 -385
- package/documentation/19-class-based-routing.md +0 -840
- package/documentation/20-websocket-realtime.md +0 -813
- package/documentation/21-cache-system.md +0 -510
- package/documentation/22-job-queue.md +0 -772
- package/documentation/23-sentry-plugin.md +0 -551
- package/documentation/24-testing-utilities.md +0 -1287
- package/documentation/25-api-versioning.md +0 -533
- package/documentation/26-context-store.md +0 -607
- package/documentation/27-dependency-injection.md +0 -329
- package/documentation/28-lifecycle-hooks.md +0 -521
- package/documentation/29-package-structure.md +0 -196
- package/documentation/30-plugin-system.md +0 -414
- package/documentation/31-jwt-authentication.md +0 -597
- package/documentation/32-cli.md +0 -268
- package/documentation/ALERTS-COMPLETE-SUMMARY.md +0 -429
- package/documentation/ALERTS-INDEX.md +0 -330
- package/documentation/ALERTS-QUICK-REFERENCE.md +0 -286
- package/documentation/README.md +0 -178
- package/documentation/index.html +0 -34
- package/modern_framework_paper.md +0 -1870
- package/public/css/style.css +0 -87
- package/public/index.html +0 -34
- package/public/js/app.js +0 -27
- package/src/advanced/cache/InMemoryCacheStore.ts +0 -68
- package/src/advanced/cache/MultiTierCache.ts +0 -194
- package/src/advanced/cache/RedisCacheStore.ts +0 -341
- package/src/advanced/cache/index.ts +0 -5
- package/src/advanced/cache/types.ts +0 -40
- package/src/advanced/graphql/SimpleDataLoader.ts +0 -42
- package/src/advanced/graphql/index.ts +0 -22
- package/src/advanced/graphql/server.ts +0 -252
- package/src/advanced/graphql/types.ts +0 -42
- package/src/advanced/jobs/InMemoryQueueStore.ts +0 -68
- package/src/advanced/jobs/JobQueue.ts +0 -556
- package/src/advanced/jobs/RedisQueueStore.ts +0 -367
- package/src/advanced/jobs/index.ts +0 -5
- package/src/advanced/jobs/types.ts +0 -70
- package/src/advanced/observability/APMManager.ts +0 -163
- package/src/advanced/observability/AlertManager.ts +0 -109
- package/src/advanced/observability/MetricRegistry.ts +0 -151
- package/src/advanced/observability/ObservabilityCenter.ts +0 -304
- package/src/advanced/observability/StructuredLogger.ts +0 -154
- package/src/advanced/observability/TracingManager.ts +0 -117
- package/src/advanced/observability/adapters.ts +0 -304
- package/src/advanced/observability/createObservabilityMiddleware.ts +0 -63
- package/src/advanced/observability/index.ts +0 -11
- package/src/advanced/observability/types.ts +0 -174
- package/src/advanced/playground/extractPathParams.ts +0 -6
- package/src/advanced/playground/generateFieldExample.ts +0 -31
- package/src/advanced/playground/generatePlaygroundHTML.ts +0 -1956
- package/src/advanced/playground/generateSummary.ts +0 -19
- package/src/advanced/playground/getTagFromPath.ts +0 -9
- package/src/advanced/playground/index.ts +0 -8
- package/src/advanced/playground/playground.ts +0 -250
- package/src/advanced/playground/types.ts +0 -49
- package/src/advanced/playground/zodToExample.ts +0 -16
- package/src/advanced/playground/zodToParams.ts +0 -15
- package/src/advanced/postman/buildAuth.ts +0 -31
- package/src/advanced/postman/buildBody.ts +0 -15
- package/src/advanced/postman/buildQueryParams.ts +0 -27
- package/src/advanced/postman/buildRequestItem.ts +0 -36
- package/src/advanced/postman/buildResponses.ts +0 -11
- package/src/advanced/postman/buildUrl.ts +0 -33
- package/src/advanced/postman/capitalize.ts +0 -4
- package/src/advanced/postman/generateCollection.ts +0 -59
- package/src/advanced/postman/generateEnvironment.ts +0 -34
- package/src/advanced/postman/generateExampleFromZod.ts +0 -21
- package/src/advanced/postman/generateFieldExample.ts +0 -45
- package/src/advanced/postman/generateName.ts +0 -20
- package/src/advanced/postman/generateUUID.ts +0 -11
- package/src/advanced/postman/getTagFromPath.ts +0 -10
- package/src/advanced/postman/index.ts +0 -28
- package/src/advanced/postman/postman.ts +0 -156
- package/src/advanced/postman/slugify.ts +0 -7
- package/src/advanced/postman/types.ts +0 -140
- package/src/advanced/realtime/index.ts +0 -18
- package/src/advanced/realtime/websocket.ts +0 -231
- package/src/advanced/sentry/index.ts +0 -1236
- package/src/advanced/sentry/types.ts +0 -355
- package/src/advanced/static/generateDirectoryListing.ts +0 -47
- package/src/advanced/static/generateETag.ts +0 -7
- package/src/advanced/static/getMimeType.ts +0 -9
- package/src/advanced/static/index.ts +0 -32
- package/src/advanced/static/isSafePath.ts +0 -13
- package/src/advanced/static/publicDir.ts +0 -21
- package/src/advanced/static/serveStatic.ts +0 -225
- package/src/advanced/static/spa.ts +0 -24
- package/src/advanced/static/types.ts +0 -159
- package/src/advanced/swagger/SwaggerGenerator.ts +0 -66
- package/src/advanced/swagger/buildOperation.ts +0 -61
- package/src/advanced/swagger/buildParameters.ts +0 -61
- package/src/advanced/swagger/buildRequestBody.ts +0 -21
- package/src/advanced/swagger/buildResponses.ts +0 -54
- package/src/advanced/swagger/capitalize.ts +0 -5
- package/src/advanced/swagger/convertPath.ts +0 -9
- package/src/advanced/swagger/createSwagger.ts +0 -12
- package/src/advanced/swagger/generateOperationId.ts +0 -21
- package/src/advanced/swagger/generateSpec.ts +0 -105
- package/src/advanced/swagger/generateSummary.ts +0 -24
- package/src/advanced/swagger/generateSwaggerUI.ts +0 -70
- package/src/advanced/swagger/generateThemeCss.ts +0 -53
- package/src/advanced/swagger/index.ts +0 -25
- package/src/advanced/swagger/swagger.ts +0 -237
- package/src/advanced/swagger/types.ts +0 -206
- package/src/advanced/swagger/zodFieldToOpenAPI.ts +0 -94
- package/src/advanced/swagger/zodSchemaToOpenAPI.ts +0 -50
- package/src/advanced/swagger/zodToOpenAPI.ts +0 -22
- package/src/advanced/testing/factory.ts +0 -509
- package/src/advanced/testing/harness.ts +0 -612
- package/src/advanced/testing/index.ts +0 -430
- package/src/advanced/testing/load-test.ts +0 -618
- package/src/advanced/testing/mock-server.ts +0 -498
- package/src/advanced/testing/mock.ts +0 -670
- package/src/cli/bin.ts +0 -9
- package/src/cli/cli.ts +0 -158
- package/src/cli/commands/add.ts +0 -178
- package/src/cli/commands/build.ts +0 -73
- package/src/cli/commands/create.ts +0 -166
- package/src/cli/commands/dev.ts +0 -85
- package/src/cli/commands/generate.ts +0 -99
- package/src/cli/commands/help.ts +0 -95
- package/src/cli/commands/init.ts +0 -91
- package/src/cli/commands/version.ts +0 -38
- package/src/cli/index.ts +0 -6
- package/src/cli/templates/generators.ts +0 -359
- package/src/cli/templates/index.ts +0 -680
- package/src/cli/utils/exec.ts +0 -52
- package/src/cli/utils/file-system.ts +0 -78
- package/src/cli/utils/logger.ts +0 -111
- package/src/core/adapter.ts +0 -88
- package/src/core/application.ts +0 -1453
- package/src/core/context-pool.ts +0 -79
- package/src/core/context.ts +0 -856
- package/src/core/index.ts +0 -94
- package/src/core/middleware.ts +0 -272
- package/src/core/performance/buffer-pool.ts +0 -108
- package/src/core/performance/middleware-optimizer.ts +0 -162
- package/src/core/plugin/PluginManager.ts +0 -435
- package/src/core/plugin/builder.ts +0 -358
- package/src/core/plugin/index.ts +0 -50
- package/src/core/plugin/types.ts +0 -214
- package/src/core/router/file-router.ts +0 -623
- package/src/core/router/index.ts +0 -260
- package/src/core/router/radix-tree.ts +0 -242
- package/src/core/serializer.ts +0 -397
- package/src/core/store/index.ts +0 -30
- package/src/core/store/registry.ts +0 -178
- package/src/core/store/request-store.ts +0 -240
- package/src/core/store/types.ts +0 -233
- package/src/core/types.ts +0 -616
- package/src/database/adapter.ts +0 -35
- package/src/database/adapters/index.ts +0 -1
- package/src/database/adapters/mysql.ts +0 -669
- package/src/database/database.ts +0 -70
- package/src/database/dialect.ts +0 -388
- package/src/database/index.ts +0 -12
- package/src/database/migrations.ts +0 -86
- package/src/database/optimizer.ts +0 -125
- package/src/database/query-builder.ts +0 -404
- package/src/database/realtime.ts +0 -53
- package/src/database/schema.ts +0 -71
- package/src/database/transactions.ts +0 -56
- package/src/database/types.ts +0 -87
- package/src/deployment/cluster.ts +0 -471
- package/src/deployment/config.ts +0 -454
- package/src/deployment/docker.ts +0 -599
- package/src/deployment/graceful-shutdown.ts +0 -373
- package/src/deployment/index.ts +0 -56
- package/src/index.ts +0 -281
- package/src/security/adapter.ts +0 -318
- package/src/security/auth/JWTPlugin.ts +0 -234
- package/src/security/auth/JWTProvider.ts +0 -316
- package/src/security/auth/adapter.ts +0 -12
- package/src/security/auth/jwt.ts +0 -234
- package/src/security/auth/middleware.ts +0 -188
- package/src/security/csrf.ts +0 -220
- package/src/security/headers.ts +0 -108
- package/src/security/index.ts +0 -60
- package/src/security/rate-limit/adapter.ts +0 -7
- package/src/security/rate-limit/memory.ts +0 -108
- package/src/security/rate-limit/middleware.ts +0 -181
- package/src/security/sanitization.ts +0 -75
- package/src/security/types.ts +0 -240
- package/src/security/utils.ts +0 -52
- package/tsconfig.json +0 -39
|
@@ -1,477 +0,0 @@
|
|
|
1
|
-
# Deployment & Production Guide
|
|
2
|
-
|
|
3
|
-
Nexus Framework menyediakan fitur-fitur production-ready untuk deployment:
|
|
4
|
-
|
|
5
|
-
## Table of Contents
|
|
6
|
-
|
|
7
|
-
1. [Graceful Shutdown](#graceful-shutdown)
|
|
8
|
-
2. [Clustering](#clustering)
|
|
9
|
-
3. [Environment Configuration](#environment-configuration)
|
|
10
|
-
4. [Docker Support](#docker-support)
|
|
11
|
-
|
|
12
|
-
---
|
|
13
|
-
|
|
14
|
-
## Graceful Shutdown
|
|
15
|
-
|
|
16
|
-
Zero-downtime deployment dengan proper connection draining.
|
|
17
|
-
|
|
18
|
-
### Basic Usage
|
|
19
|
-
|
|
20
|
-
```typescript
|
|
21
|
-
import { createApp } from 'nexus';
|
|
22
|
-
|
|
23
|
-
const app = createApp();
|
|
24
|
-
|
|
25
|
-
// Enable graceful shutdown
|
|
26
|
-
app.gracefulShutdown({
|
|
27
|
-
timeout: 30000, // Wait max 30 seconds for connections to drain
|
|
28
|
-
verbose: true
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
// Add cleanup hooks
|
|
32
|
-
app.onShutdown('database', async () => {
|
|
33
|
-
await db.close();
|
|
34
|
-
console.log('Database connection closed');
|
|
35
|
-
}, 10); // Higher priority = runs first
|
|
36
|
-
|
|
37
|
-
app.onShutdown('cache', async () => {
|
|
38
|
-
await redis.disconnect();
|
|
39
|
-
console.log('Redis connection closed');
|
|
40
|
-
}, 5);
|
|
41
|
-
|
|
42
|
-
app.listen(3000, () => {
|
|
43
|
-
console.log('Server running on port 3000');
|
|
44
|
-
});
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
### Configuration Options
|
|
48
|
-
|
|
49
|
-
```typescript
|
|
50
|
-
app.gracefulShutdown({
|
|
51
|
-
// Maximum time to wait for ongoing requests (ms)
|
|
52
|
-
timeout: 30000,
|
|
53
|
-
|
|
54
|
-
// Stop accepting new connections immediately
|
|
55
|
-
stopAcceptingConnections: true,
|
|
56
|
-
|
|
57
|
-
// Signals to listen for
|
|
58
|
-
signals: ['SIGTERM', 'SIGINT'],
|
|
59
|
-
|
|
60
|
-
// Custom health check during shutdown
|
|
61
|
-
healthCheck: async () => ({
|
|
62
|
-
status: 'healthy',
|
|
63
|
-
details: { db: 'connected' }
|
|
64
|
-
}),
|
|
65
|
-
|
|
66
|
-
// Callbacks
|
|
67
|
-
onShutdownStart: () => console.log('Shutdown starting...'),
|
|
68
|
-
onShutdownComplete: () => console.log('Shutdown complete!'),
|
|
69
|
-
|
|
70
|
-
// Enable verbose logging
|
|
71
|
-
verbose: true
|
|
72
|
-
});
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
### Programmatic Shutdown
|
|
76
|
-
|
|
77
|
-
```typescript
|
|
78
|
-
// Trigger shutdown programmatically
|
|
79
|
-
await app.shutdown();
|
|
80
|
-
|
|
81
|
-
// Check if shutdown is in progress
|
|
82
|
-
if (app.isShuttingDown()) {
|
|
83
|
-
console.log('Server is shutting down...');
|
|
84
|
-
}
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
---
|
|
88
|
-
|
|
89
|
-
## Clustering
|
|
90
|
-
|
|
91
|
-
Multi-process deployment untuk high availability dan performance.
|
|
92
|
-
|
|
93
|
-
### Basic Usage
|
|
94
|
-
|
|
95
|
-
```typescript
|
|
96
|
-
import { createApp, runClustered } from 'nexus';
|
|
97
|
-
|
|
98
|
-
runClustered(() => {
|
|
99
|
-
const app = createApp();
|
|
100
|
-
|
|
101
|
-
app.get('/', (ctx) => ({
|
|
102
|
-
message: 'Hello from worker!',
|
|
103
|
-
workerId: process.pid
|
|
104
|
-
}));
|
|
105
|
-
|
|
106
|
-
app.listen(3000);
|
|
107
|
-
}, {
|
|
108
|
-
workers: 'auto', // Use all CPU cores
|
|
109
|
-
verbose: true
|
|
110
|
-
});
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
### Advanced Clustering
|
|
114
|
-
|
|
115
|
-
```typescript
|
|
116
|
-
import { createApp, ClusterManager } from 'nexus';
|
|
117
|
-
|
|
118
|
-
const app = createApp();
|
|
119
|
-
const cluster = app.cluster({
|
|
120
|
-
workers: 4,
|
|
121
|
-
restartStrategy: 'rolling',
|
|
122
|
-
restartDelay: 5000,
|
|
123
|
-
maxRestarts: 10,
|
|
124
|
-
|
|
125
|
-
onWorkerStart: (worker) => {
|
|
126
|
-
console.log(`Worker ${worker.id} started`);
|
|
127
|
-
},
|
|
128
|
-
|
|
129
|
-
onWorkerExit: (worker, code, signal) => {
|
|
130
|
-
console.log(`Worker ${worker.id} exited: ${code} ${signal}`);
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
cluster.start(() => {
|
|
135
|
-
app.get('/', (ctx) => ({ hello: 'world' }));
|
|
136
|
-
app.listen(3000);
|
|
137
|
-
});
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
### Cluster Management
|
|
141
|
-
|
|
142
|
-
```typescript
|
|
143
|
-
// Check if primary process
|
|
144
|
-
if (cluster.isPrimary()) {
|
|
145
|
-
// Primary-only logic (e.g., scheduling)
|
|
146
|
-
|
|
147
|
-
// Get cluster stats
|
|
148
|
-
const stats = cluster.getStats();
|
|
149
|
-
console.log(`Active workers: ${stats.activeWorkers}`);
|
|
150
|
-
|
|
151
|
-
// Rolling restart all workers
|
|
152
|
-
await cluster.rollingRestart();
|
|
153
|
-
|
|
154
|
-
// Broadcast to all workers
|
|
155
|
-
cluster.broadcast({ type: 'reload-config' });
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// In worker process
|
|
159
|
-
if (cluster.isWorker()) {
|
|
160
|
-
process.on('message', (msg) => {
|
|
161
|
-
if (msg.type === 'reload-config') {
|
|
162
|
-
// Reload configuration
|
|
163
|
-
}
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
---
|
|
169
|
-
|
|
170
|
-
## Environment Configuration
|
|
171
|
-
|
|
172
|
-
Type-safe configuration dengan environment separation.
|
|
173
|
-
|
|
174
|
-
### Define Configuration
|
|
175
|
-
|
|
176
|
-
```typescript
|
|
177
|
-
import { defineConfig } from 'nexus';
|
|
178
|
-
|
|
179
|
-
const config = defineConfig({
|
|
180
|
-
// Base configuration (all environments)
|
|
181
|
-
base: {
|
|
182
|
-
server: {
|
|
183
|
-
port: 3000,
|
|
184
|
-
host: 'localhost'
|
|
185
|
-
},
|
|
186
|
-
logging: {
|
|
187
|
-
level: 'info'
|
|
188
|
-
}
|
|
189
|
-
},
|
|
190
|
-
|
|
191
|
-
// Development overrides
|
|
192
|
-
development: {
|
|
193
|
-
logging: {
|
|
194
|
-
level: 'debug',
|
|
195
|
-
format: 'pretty'
|
|
196
|
-
}
|
|
197
|
-
},
|
|
198
|
-
|
|
199
|
-
// Production overrides
|
|
200
|
-
production: {
|
|
201
|
-
server: {
|
|
202
|
-
host: '0.0.0.0',
|
|
203
|
-
port: process.env.PORT ? parseInt(process.env.PORT) : 3000
|
|
204
|
-
},
|
|
205
|
-
logging: {
|
|
206
|
-
level: 'info',
|
|
207
|
-
format: 'json'
|
|
208
|
-
},
|
|
209
|
-
security: {
|
|
210
|
-
headers: 'strict'
|
|
211
|
-
}
|
|
212
|
-
},
|
|
213
|
-
|
|
214
|
-
// Test overrides
|
|
215
|
-
test: {
|
|
216
|
-
database: {
|
|
217
|
-
url: ':memory:'
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
// Use configuration
|
|
223
|
-
const port = config.get('server.port');
|
|
224
|
-
const dbUrl = config.get('database.url');
|
|
225
|
-
|
|
226
|
-
if (config.isProduction()) {
|
|
227
|
-
// Production-specific logic
|
|
228
|
-
}
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
### Environment Variables
|
|
232
|
-
|
|
233
|
-
```bash
|
|
234
|
-
# .env file
|
|
235
|
-
APP_SERVER_PORT=8080
|
|
236
|
-
APP_DATABASE_URL=postgres://localhost/mydb
|
|
237
|
-
APP_LOGGING_LEVEL=debug
|
|
238
|
-
```
|
|
239
|
-
|
|
240
|
-
```typescript
|
|
241
|
-
import { env, requireEnv } from 'nexus';
|
|
242
|
-
|
|
243
|
-
// Get env with default
|
|
244
|
-
const port = env('PORT', 3000);
|
|
245
|
-
|
|
246
|
-
// Require env (throws if not set)
|
|
247
|
-
const secret = requireEnv('JWT_SECRET');
|
|
248
|
-
```
|
|
249
|
-
|
|
250
|
-
### Configuration Files
|
|
251
|
-
|
|
252
|
-
```
|
|
253
|
-
project/
|
|
254
|
-
├── .env # Base environment variables
|
|
255
|
-
├── .env.development # Development-specific
|
|
256
|
-
├── .env.production # Production-specific
|
|
257
|
-
├── .env.local # Local overrides (not committed)
|
|
258
|
-
└── .env.example # Template for required vars
|
|
259
|
-
```
|
|
260
|
-
|
|
261
|
-
---
|
|
262
|
-
|
|
263
|
-
## Docker Support
|
|
264
|
-
|
|
265
|
-
Generate optimized Docker configurations.
|
|
266
|
-
|
|
267
|
-
### Generate Docker Files
|
|
268
|
-
|
|
269
|
-
```typescript
|
|
270
|
-
import { generateDockerFiles } from 'nexus';
|
|
271
|
-
|
|
272
|
-
// Generate with default options
|
|
273
|
-
generateDockerFiles();
|
|
274
|
-
|
|
275
|
-
// Generate with custom options
|
|
276
|
-
generateDockerFiles('.', {
|
|
277
|
-
baseImage: 'node:20-alpine',
|
|
278
|
-
port: 3000,
|
|
279
|
-
healthCheck: true,
|
|
280
|
-
healthEndpoint: '/__nexus/health',
|
|
281
|
-
nonRootUser: true,
|
|
282
|
-
multiStage: true
|
|
283
|
-
});
|
|
284
|
-
```
|
|
285
|
-
|
|
286
|
-
### Generated Files
|
|
287
|
-
|
|
288
|
-
```
|
|
289
|
-
project/
|
|
290
|
-
├── Dockerfile # Multi-stage optimized build
|
|
291
|
-
├── .dockerignore # Optimized ignore patterns
|
|
292
|
-
├── docker-compose.yml # Production compose file
|
|
293
|
-
└── docker-compose.dev.yml # Development compose file
|
|
294
|
-
```
|
|
295
|
-
|
|
296
|
-
### Generated Dockerfile
|
|
297
|
-
|
|
298
|
-
```dockerfile
|
|
299
|
-
# ============================================
|
|
300
|
-
# Base Stage
|
|
301
|
-
# ============================================
|
|
302
|
-
FROM node:20-alpine AS base
|
|
303
|
-
WORKDIR /app
|
|
304
|
-
|
|
305
|
-
# ============================================
|
|
306
|
-
# Dependencies Stage
|
|
307
|
-
# ============================================
|
|
308
|
-
FROM base AS deps
|
|
309
|
-
COPY package*.json ./
|
|
310
|
-
RUN npm ci --only=production
|
|
311
|
-
|
|
312
|
-
# ============================================
|
|
313
|
-
# Build Stage
|
|
314
|
-
# ============================================
|
|
315
|
-
FROM base AS build
|
|
316
|
-
COPY package*.json ./
|
|
317
|
-
RUN npm ci
|
|
318
|
-
COPY . .
|
|
319
|
-
RUN npm run build
|
|
320
|
-
|
|
321
|
-
# ============================================
|
|
322
|
-
# Production Stage
|
|
323
|
-
# ============================================
|
|
324
|
-
FROM base AS production
|
|
325
|
-
|
|
326
|
-
# Create non-root user for security
|
|
327
|
-
RUN addgroup --system --gid 1001 nodejs
|
|
328
|
-
RUN adduser --system --uid 1001 nexus
|
|
329
|
-
|
|
330
|
-
ENV NODE_ENV=production
|
|
331
|
-
|
|
332
|
-
COPY --from=deps --chown=nexus:nodejs /app/node_modules ./node_modules
|
|
333
|
-
COPY --from=build --chown=nexus:nodejs /app/dist ./dist
|
|
334
|
-
COPY --from=build --chown=nexus:nodejs /app/package.json ./
|
|
335
|
-
|
|
336
|
-
USER nexus
|
|
337
|
-
|
|
338
|
-
EXPOSE 3000
|
|
339
|
-
|
|
340
|
-
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
|
341
|
-
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1
|
|
342
|
-
|
|
343
|
-
CMD ["node", "dist/index.js"]
|
|
344
|
-
```
|
|
345
|
-
|
|
346
|
-
### Docker Commands
|
|
347
|
-
|
|
348
|
-
```bash
|
|
349
|
-
# Build image
|
|
350
|
-
docker build -t myapp .
|
|
351
|
-
|
|
352
|
-
# Run container
|
|
353
|
-
docker run -p 3000:3000 myapp
|
|
354
|
-
|
|
355
|
-
# Using docker-compose
|
|
356
|
-
docker-compose up -d
|
|
357
|
-
|
|
358
|
-
# Development with hot-reload
|
|
359
|
-
docker-compose -f docker-compose.dev.yml up
|
|
360
|
-
```
|
|
361
|
-
|
|
362
|
-
### Custom Docker Configuration
|
|
363
|
-
|
|
364
|
-
```typescript
|
|
365
|
-
import { createDocker } from 'nexus';
|
|
366
|
-
|
|
367
|
-
const docker = createDocker({
|
|
368
|
-
baseImage: 'node:20-alpine',
|
|
369
|
-
port: 8080,
|
|
370
|
-
entrypoint: 'dist/server.js',
|
|
371
|
-
env: {
|
|
372
|
-
TZ: 'UTC',
|
|
373
|
-
NODE_OPTIONS: '--max-old-space-size=512'
|
|
374
|
-
},
|
|
375
|
-
labels: {
|
|
376
|
-
'app.version': '1.0.0',
|
|
377
|
-
'app.maintainer': 'team@example.com'
|
|
378
|
-
}
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
// Generate individual files
|
|
382
|
-
const dockerfile = docker.generateDockerfile();
|
|
383
|
-
const dockerignore = docker.generateDockerignore();
|
|
384
|
-
const compose = docker.generateComposeFile({
|
|
385
|
-
services: {
|
|
386
|
-
redis: {
|
|
387
|
-
image: 'redis:alpine',
|
|
388
|
-
ports: ['6379:6379']
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
});
|
|
392
|
-
```
|
|
393
|
-
|
|
394
|
-
---
|
|
395
|
-
|
|
396
|
-
## Complete Production Setup
|
|
397
|
-
|
|
398
|
-
```typescript
|
|
399
|
-
import {
|
|
400
|
-
createApp,
|
|
401
|
-
defineConfig,
|
|
402
|
-
runClustered,
|
|
403
|
-
generateDockerFiles
|
|
404
|
-
} from 'nexus';
|
|
405
|
-
|
|
406
|
-
// Configuration
|
|
407
|
-
const config = defineConfig({
|
|
408
|
-
base: {
|
|
409
|
-
server: { port: 3000 }
|
|
410
|
-
},
|
|
411
|
-
production: {
|
|
412
|
-
server: { port: parseInt(process.env.PORT || '3000') },
|
|
413
|
-
security: { headers: 'strict' }
|
|
414
|
-
}
|
|
415
|
-
});
|
|
416
|
-
|
|
417
|
-
// Generate Docker files (run once during setup)
|
|
418
|
-
if (process.argv.includes('--docker')) {
|
|
419
|
-
generateDockerFiles('.', { port: config.get('server.port') });
|
|
420
|
-
process.exit(0);
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
// Run with clustering in production
|
|
424
|
-
runClustered(() => {
|
|
425
|
-
const app = createApp({ debug: config.isDevelopment() });
|
|
426
|
-
|
|
427
|
-
// Enable graceful shutdown
|
|
428
|
-
app.gracefulShutdown({
|
|
429
|
-
timeout: 30000,
|
|
430
|
-
verbose: config.isDevelopment()
|
|
431
|
-
});
|
|
432
|
-
|
|
433
|
-
// Add shutdown hooks
|
|
434
|
-
app.onShutdown('database', async () => {
|
|
435
|
-
await db.close();
|
|
436
|
-
});
|
|
437
|
-
|
|
438
|
-
// Enable observability
|
|
439
|
-
app.observe({
|
|
440
|
-
metrics: { enabled: true },
|
|
441
|
-
health: {
|
|
442
|
-
endpoint: '/__nexus/health',
|
|
443
|
-
checks: [
|
|
444
|
-
{ name: 'database', check: async () => ({ status: 'up' }) }
|
|
445
|
-
]
|
|
446
|
-
}
|
|
447
|
-
});
|
|
448
|
-
|
|
449
|
-
// Routes
|
|
450
|
-
app.get('/', (ctx) => ({ status: 'ok' }));
|
|
451
|
-
|
|
452
|
-
// Start server
|
|
453
|
-
app.start({
|
|
454
|
-
port: config.get('server.port'),
|
|
455
|
-
host: '0.0.0.0'
|
|
456
|
-
});
|
|
457
|
-
|
|
458
|
-
console.log(`Worker ${process.pid} listening on port ${config.get('server.port')}`);
|
|
459
|
-
|
|
460
|
-
}, {
|
|
461
|
-
workers: config.isProduction() ? 'auto' : 1,
|
|
462
|
-
verbose: config.isDevelopment()
|
|
463
|
-
});
|
|
464
|
-
```
|
|
465
|
-
|
|
466
|
-
---
|
|
467
|
-
|
|
468
|
-
## Best Practices
|
|
469
|
-
|
|
470
|
-
1. **Always use graceful shutdown** in production
|
|
471
|
-
2. **Use clustering** to utilize all CPU cores
|
|
472
|
-
3. **Separate configuration** by environment
|
|
473
|
-
4. **Use multi-stage Docker builds** for smaller images
|
|
474
|
-
5. **Run as non-root user** in containers
|
|
475
|
-
6. **Include health checks** for orchestration platforms
|
|
476
|
-
7. **Set proper timeouts** for connection draining
|
|
477
|
-
8. **Add shutdown hooks** for cleanup (DB, Redis, etc.)
|