-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
293 lines (263 loc) · 12.4 KB
/
index.ts
File metadata and controls
293 lines (263 loc) · 12.4 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
// server/index.ts – Express bootstrap for CodePatchwork with Winston logging
import dotenv from "dotenv";
dotenv.config();
import fs from "fs";
import path from "path";
import admin from "firebase-admin";
import express, { Request, Response, NextFunction } from "express";
import helmet from "helmet";
import camelCase from "camelcase";
import { registerRoutes } from "./routes";
import { setupVite, serveStatic } from "./vite";
import logger from "./logger";
/* ────────────────────────────────────────────────────────────────── */
/* 0. Winston test log – confirms logger is active */
/* ────────────────────────────────────────────────────────────────── */
logger.info("✅ Winston logger loaded from ./logger.ts");
logger.info("🧪 Logger test: Express server startup log");
/* ────────────────────────────────────────────────────────────────── */
/* 1. Verify & load your service-account JSON */
/* ────────────────────────────────────────────────────────────────── */
const svcPath = path.resolve(
process.cwd(),
process.env.GOOGLE_APPLICATION_CREDENTIALS!
);
logger.info(`→ SERVICE ACCOUNT path: ${svcPath}`);
logger.info(`→ Exists on disk? ${fs.existsSync(svcPath)}`);
if (!fs.existsSync(svcPath)) {
logger.error("❌ service account JSON not found. Aborting.");
process.exit(1);
}
const serviceAccount = JSON.parse(
fs.readFileSync(svcPath, { encoding: "utf-8" })
);
/* ────────────────────────────────────────────────────────────────── */
/* 2. Initialize Firebase Admin */
/* ────────────────────────────────────────────────────────────────── */
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
/* ────────────────────────────────────────────────────────────────── */
/* 3. Express setup */
/* ────────────────────────────────────────────────────────────────── */
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
/* ────────────────────────────────────────────────────────────────── */
/* 4. Helmet security headers */
/* ────────────────────────────────────────────────────────────────── */
app.use(
helmet.crossOriginOpenerPolicy({ policy: "unsafe-none" }),
helmet.crossOriginEmbedderPolicy({ policy: "unsafe-none" }),
helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'"],
scriptSrc: [
"'self'",
"'unsafe-inline'",
"'unsafe-eval'",
"https://www.gstatic.com",
"https://apis.google.com",
"https://*.firebaseio.com",
"https://*.firebaseapp.com",
],
styleSrc: ["'self'", "'unsafe-inline'", "https://fonts.googleapis.com"],
fontSrc: ["'self'", "https://fonts.gstatic.com"],
connectSrc: [
"'self'",
"https://identitytoolkit.googleapis.com",
"https://securetoken.googleapis.com",
"https://www.googleapis.com",
"https://*.firebaseio.com",
"https://*.firebaseapp.com",
],
frameSrc: [
"https://accounts.google.com",
`https://${process.env.VITE_FIREBASE_AUTH_DOMAIN}`,
"https://*.firebaseapp.com",
],
imgSrc: ["'self'", "data:", "https://*.googleusercontent.com"],
},
})
);
/* ────────────────────────────────────────────────────────────────── */
/* 5. API middleware */
/* ────────────────────────────────────────────────────────────────── */
app.use('/api', (req, res, next) => {
logger.info(`[API] ${req.method} ${req.path} - Request received`);
res.setHeader('Content-Type', 'application/json');
const originalSend = res.send;
const originalStatus = res.status;
res.send = function(data: any) {
if (typeof data === 'string' && (data.includes('<!DOCTYPE') || data.includes('<html>'))) {
logger.info(`[API] 🚨 Converting HTML response to JSON for ${req.method} ${req.path}`);
this.setHeader('Content-Type', 'application/json');
return originalSend.call(this, JSON.stringify({
message: "API endpoint error",
error: "An error occurred while processing your request",
path: req.path,
method: req.method,
timestamp: new Date().toISOString()
}));
}
return originalSend.call(this, data);
};
res.status = function(statusCode: number) {
const result = originalStatus.call(this, statusCode);
const newSend = result.send;
result.send = function(data: any) {
if (typeof data === 'string' && (data.includes('<!DOCTYPE') || data.includes('<html>'))) {
logger.info(`[API] 🚨 Converting status ${statusCode} HTML to JSON for ${req.method} ${req.path}`);
this.setHeader('Content-Type', 'application/json');
return originalSend.call(this, JSON.stringify({
message: "API Error",
error: `HTTP ${statusCode} Error`,
path: req.path,
method: req.method,
timestamp: new Date().toISOString()
}));
}
return newSend.call(this, data);
};
return result;
};
next();
});
/* ────────────────────────────────────────────────────────────────── */
/* 6. Normalize response keys */
/* ────────────────────────────────────────────────────────────────── */
app.use("/api/snippets", (_req, res, next) => {
type Row = Record<string, unknown>;
const rename = { createdat: "createdAt", updatedat: "updatedAt" } as const;
const origJson = res.json.bind(res);
res.json = (body) => {
if (Array.isArray(body)) {
const fixed = body.map((row: Row) => {
const out: Row = {};
for (const [k, v] of Object.entries(row)) {
const key = rename[k as keyof typeof rename] ?? camelCase(k);
out[key] =
(key === "createdAt" || key === "updatedAt") && v != null
? new Date(v as string).toISOString()
: v;
}
return out;
});
return origJson(fixed);
}
return origJson(body as any);
};
next();
});
/* ────────────────────────────────────────────────────────────────── */
/* 7. Performance + payload logging */
/* ────────────────────────────────────────────────────────────────── */
app.use((req, res, next) => {
const t0 = Date.now();
let payload: any;
const orig = res.json.bind(res);
res.json = (body, ...args) => {
payload = body;
return orig(body, ...args);
};
res.on("finish", () => {
if (req.path.startsWith("/api")) {
const ms = Date.now() - t0;
let msg = `${req.method} ${req.path} ${res.statusCode} in ${ms}ms`;
if (payload) msg += ` :: ${JSON.stringify(payload)}`;
if (msg.length > 80) msg = msg.slice(0, 79) + "…";
logger.info(msg);
}
});
next();
});
/* ────────────────────────────────────────────────────────────────── */
/* 8. Route registration + boot */
/* ────────────────────────────────────────────────────────────────── */
(async () => {
logger.info("🔧 Starting route registration...");
const server = await registerRoutes(app);
logger.info("✅ Route registration complete");
app.use('/api/*', (req, res) => {
logger.info(`[404] API route not found: ${req.method} ${req.path}`);
res.status(404).json({
message: "API endpoint not found",
path: req.path,
method: req.method,
timestamp: new Date().toISOString(),
availableEndpoints: [
"GET /api/test",
"GET /api/snippets",
"GET /api/snippets/:id",
"POST /api/snippets",
"PUT /api/snippets/:id",
"DELETE /api/snippets/:id",
"POST /api/snippets/:id/favorite",
"GET /api/languages",
"GET /api/tags",
"POST /api/auth/user",
"GET /api/auth/me"
]
});
});
app.use((err: any, req: Request, res: Response, next: NextFunction) => {
logger.error(`[💥 GLOBAL ERROR] ${req.method} ${req.path}:`, err.stack || err);
if (!res.headersSent) {
if (req.path.startsWith('/api/')) {
res.setHeader('Content-Type', 'application/json');
res.status(err.status || 500).json({
message: err.message || "Internal server error",
path: req.path,
method: req.method,
timestamp: new Date().toISOString(),
error: process.env.NODE_ENV === 'production' ? 'Something went wrong' : err.stack
});
} else {
res.status(err.status || 500).json({ message: err.message || "Error" });
}
}
});
if (app.get("env") === "development") {
await setupVite(app, server);
} else {
serveStatic(app);
}
const port = Number(process.env.PORT) || 3001;
logger.info(`🚀 Express server starting on port ${process.env.PORT || 3001}`);
server.listen({ host: "0.0.0.0", port, reusePort: true }, () => {
logger.info(`🚀 Serving on port ${port}`);
logger.info(`📡 API available at http://localhost:${port}/api/`);
logger.info(`🧪 Test API at http://localhost:${port}/api/test`);
logger.info("---");
logger.info("🔧 API Endpoints registered:");
logger.info(" GET /api/test");
logger.info(" GET /api/snippets");
logger.info(" GET /api/snippets/:id");
logger.info(" POST /api/snippets");
logger.info(" PUT /api/snippets/:id");
logger.info(" DELETE /api/snippets/:id");
logger.info(" POST /api/snippets/:id/favorite");
logger.info(" GET /api/languages");
logger.info(" GET /api/tags");
logger.info(" POST /api/auth/user");
logger.info(" GET /api/auth/me");
logger.info("---");
});
process.on('SIGTERM', () => {
logger.info('🛑 SIGTERM received, shutting down gracefully');
server.close(() => {
logger.info('✅ Server closed');
process.exit(0);
});
});
process.on('SIGINT', () => {
logger.info('🛑 SIGINT received, shutting down gracefully');
server.close(() => {
logger.info('✅ Server closed');
process.exit(0);
});
});
})().catch((error) => {
logger.error("❌ Failed to start server:", error);
process.exit(1);
});