-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
417 lines (352 loc) · 10.6 KB
/
server.js
File metadata and controls
417 lines (352 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**
* CodePark v2.0.0 - Production Server
* Integrates all new features with proper error handling and monitoring
*/
require('dotenv').config({
path: `.env.${process.env.NODE_ENV || 'development'}`
})
const express = require('express')
const { getConfig } = require('./config/app-config')
// Feature modules
const RequestValidator = require('./Projects/JavaScript/RequestValidationFramework')
const CORSManager = require('./Projects/JavaScript/CORSManager')
const IPWhitelistService = require('./Projects/JavaScript/IPWhitelistingService')
const HealthCheckService = require('./Projects/JavaScript/HealthCheckService')
const LogAggregationService = require('./Projects/JavaScript/LogAggregationService')
const QueryCache = require('./Projects/JavaScript/QueryResultCaching')
const CompressionMiddleware = require('./Projects/JavaScript/CompressionMiddleware')
const GraphQLSubscriptions = require('./Projects/JavaScript/GraphQLSubscriptions')
class CodeParkServer {
constructor () {
this.config = getConfig()
this.app = express()
this.services = {}
this.setupPhase = 'initialization'
}
/**
* Initialize all services
*/
async initialize () {
try {
console.log('\n🚀 CodePark v2.0.0 Server Starting')
console.log(`Environment: ${this.config.app.environment}`)
console.log(`Port: ${this.config.server.port}\n`)
this.setupLogging()
this.setupCompression()
this.setupCORS()
this.setupValidation()
this.setupIPWhitelist()
this.setupCaching()
this.setupHealthChecks()
this.setupRoutes()
this.setupErrorHandling()
console.log('✅ All services initialized successfully\n')
return this
} catch (error) {
console.error('❌ Initialization failed:', error)
process.exit(1)
}
}
/**
* Setup Logging Service
*/
setupLogging () {
console.log('[1/7] Setting up logging...')
this.setupPhase = 'logging'
const logger = new LogAggregationService({
level: this.config.logging.level,
maxBufferSize: this.config.logging.maxBufferSize
})
// Console transport (dev/staging)
if (['development', 'staging'].includes(this.config.app.environment)) {
logger.addConsoleTransport({ colorize: true })
}
// File transport
if (this.config.logging.file) {
logger.addFileTransport(this.config.logging.file, { format: 'json' })
}
// HTTP transport (production)
if (this.config.logging.http) {
logger.addHttpTransport(this.config.logging.http)
}
this.app.use(logger.middleware())
this.services.logger = logger
global.logger = logger
logger.info('Logging service initialized', {
level: this.config.logging.level,
transports: this.config.logging.transports
})
}
/**
* Setup Compression Middleware
*/
setupCompression () {
console.log('[2/7] Setting up compression...')
this.setupPhase = 'compression'
const compression = new CompressionMiddleware({
level: this.config.compression.level,
minSize: this.config.compression.minSize
})
this.app.use(compression.middleware())
this.services.compression = compression
this.services.logger?.info('Compression middleware initialized', {
level: this.config.compression.level,
minSize: this.config.compression.minSize
})
}
/**
* Setup CORS Manager
*/
setupCORS () {
console.log('[3/7] Setting up CORS...')
this.setupPhase = 'cors'
const corsManager = new CORSManager(this.config.cors)
this.app.use(corsManager.middleware())
this.services.cors = corsManager
this.services.logger?.info('CORS manager initialized', {
origins: this.config.cors.origins.length,
credentials: this.config.cors.credentials
})
}
/**
* Setup Request Validation
*/
setupValidation () {
console.log('[4/7] Setting up request validation...')
this.setupPhase = 'validation'
const validator = new RequestValidator()
// Register default schemas
validator.registerSchema('user', {
type: 'object',
properties: {
name: { type: 'string', minLength: 1 },
email: { type: 'string', format: 'email' },
age: { type: 'integer', minimum: 0, maximum: 150 }
},
required: ['name', 'email']
})
this.services.validator = validator
this.services.logger?.info('Request validator initialized')
}
/**
* Setup IP Whitelisting
*/
setupIPWhitelist () {
console.log('[5/7] Setting up IP whitelisting...')
this.setupPhase = 'ip-whitelist'
const ipService = new IPWhitelistService(this.config.ipWhitelist)
// Add IPs from config
if (
this.config.ipWhitelist.list &&
this.config.ipWhitelist.list.length > 0
) {
this.config.ipWhitelist.list.forEach((ip) => {
ipService.addToWhitelist(ip.trim())
})
} else {
// Default: allow localhost
ipService.addToWhitelist('127.0.0.1')
ipService.addToWhitelist('::1')
}
// Only enable middleware in production/staging
if (this.config.ipWhitelist.enforceMode !== 'off') {
this.app.use(ipService.middleware())
}
this.services.ipService = ipService
this.services.logger?.info('IP whitelist service initialized', {
enforceMode: this.config.ipWhitelist.enforceMode,
whitelistCount: this.config.ipWhitelist.list?.length || 0
})
}
/**
* Setup Query Caching
*/
setupCaching () {
console.log('[6/7] Setting up query caching...')
this.setupPhase = 'caching'
const cache = new QueryCache({
ttl: this.config.cache.ttl,
maxSize: this.config.cache.maxSize
})
this.services.cache = cache
this.services.logger?.info('Query cache initialized', {
ttl: this.config.cache.ttl,
maxSize: this.config.cache.maxSize
})
}
/**
* Setup Health Checks
*/
setupHealthChecks () {
console.log('[7/7] Setting up health checks...')
this.setupPhase = 'health-checks'
const health = new HealthCheckService({
failureThreshold: 3,
checkTimeout: 5000
})
// Liveness probe
health.registerLivenessProbe(async () => {
return Promise.resolve(true)
})
// Readiness probe
health.registerReadinessProbe(async () => {
// Check all critical services
return true // Simplified for this example
})
// Memory check
health.registerCheck(
'memory',
async () => {
const used = process.memoryUsage()
const heapUsedPercent = (used.heapUsed / used.heapTotal) * 100
return heapUsedPercent < 90
},
{ critical: false }
)
// Setup endpoints
this.app.get('/health', health.healthMiddleware())
this.app.get('/health/live', health.livenessMiddleware())
this.app.get('/health/ready', health.readinessMiddleware())
this.services.health = health
this.services.logger?.info('Health check service initialized')
}
/**
* Setup Routes
*/
setupRoutes () {
// Parse JSON
this.app.use(express.json({ limit: '10mb' }))
this.app.use(express.urlencoded({ limit: '10mb', extended: true }))
// API Routes
this.app.get('/api/status', (req, res) => {
res.json({
status: 'ok',
version: this.config.app.version,
environment: this.config.app.environment,
uptime: process.uptime()
})
})
// Sample endpoint with validation
this.app.post(
'/api/users',
this.services.validator.middleware('user', 'body'),
(req, res) => {
this.services.logger?.info('User created', {
email: req.validatedData.email
})
res.json({
success: true,
data: req.validatedData
})
}
)
// Admin endpoints (dev/staging only)
if (this.config.app.environment !== 'production') {
this.app.get('/admin/config', (req, res) => {
res.json({
environment: this.config.app.environment,
services: Object.keys(this.services),
config: this.config
})
})
this.app.get('/admin/cache/stats', (req, res) => {
res.json(this.services.cache.getStats())
})
this.app.get('/admin/compression/stats', (req, res) => {
res.json(this.services.compression.getStats())
})
}
// 404 handler
this.app.use((req, res) => {
res.status(404).json({
error: 'Not Found',
path: req.path
})
})
}
/**
* Setup Error Handling
*/
setupErrorHandling () {
this.app.use((err, req, res, next) => {
const statusCode = err.statusCode || 500
const errorResponse = {
error: err.message || 'Internal Server Error',
status: statusCode
}
if (this.config.app.environment !== 'production') {
errorResponse.stack = err.stack
}
this.services.logger?.error('Request error', {
message: err.message,
statusCode,
path: req.path,
method: req.method
})
res.status(statusCode).json(errorResponse)
})
}
/**
* Start the server
*/
start () {
return new Promise((resolve, reject) => {
this.server = this.app.listen(
this.config.server.port,
this.config.server.host,
() => {
console.log(
`✅ Server listening on http://${this.config.server.host}:${this.config.server.port}`
)
console.log(
`📊 Health: http://${this.config.server.host}:${this.config.server.port}/health`
)
console.log(
`🔍 Status: http://${this.config.server.host}:${this.config.server.port}/api/status\n`
)
resolve(this.server)
}
)
this.server.on('error', reject)
})
}
/**
* Stop the server gracefully
*/
async stop () {
return new Promise((resolve) => {
if (this.server) {
this.services.logger?.info('Shutting down server...')
this.server.close(() => {
console.log('✅ Server stopped')
resolve()
})
} else {
resolve()
}
})
}
}
// Handle graceful shutdown
if (require.main === module) {
const server = new CodeParkServer()
server
.initialize()
.then(() => server.start())
.catch((error) => {
console.error('Fatal error:', error)
process.exit(1)
})
// Graceful shutdown
process.on('SIGTERM', async () => {
console.log('SIGTERM received, shutting down...')
await server.stop()
process.exit(0)
})
process.on('SIGINT', async () => {
console.log('\nSIGINT received, shutting down...')
await server.stop()
process.exit(0)
})
}
module.exports = CodeParkServer