Repository navigation
Expand file tree
/
Copy pathlambda.js
More file actions
145 lines (131 loc) · 4.57 KB
/
Copy pathlambda.js
File metadata and controls
145 lines (131 loc) · 4.57 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
import serverlessExpress from "@codegenie/serverless-express";
import { Environment } from "./dist/shared/helpers/Environment.js";
import { createApp } from "./dist/app.js";
import { handleSocket } from "./dist/lambda/socket-handler.js";
import { handle30MinTimer, handleMidnightTimer, handleScheduledTasks, handleWebhookTimer } from "./dist/lambda/timer-handler.js";
import { handleSesFeedback } from "./dist/lambda/ses-feedback-handler.js";
import { handleMigrate } from "./dist/lambda/migrate-handler.js";
const initializeEnvironment = async () => {
if (!Environment.currentEnvironment) {
const stage = process.env.STAGE || process.env.ENVIRONMENT || "dev";
console.log("Initializing environment with stage:", stage);
console.log("Environment variables:", {
STAGE: process.env.STAGE,
ENVIRONMENT: process.env.ENVIRONMENT,
APP_ENV: process.env.APP_ENV
});
try {
await Environment.init(stage);
} catch (err) {
Environment.currentEnvironment = undefined;
throw err;
}
console.log("Environment initialized, connection strings loaded");
// Pools now auto-initialize on first use
}
};
let cachedHandler;
export const web = async function (event, context) {
try {
await initializeEnvironment();
if (!cachedHandler) {
console.log("Creating Express app with fully initialized environment...");
const app = await createApp();
console.log("Express app created");
cachedHandler = serverlessExpress({
app,
binarySettings: {
contentTypes: ["application/octet-stream", "font/*", "image/*", "application/pdf"]
}
});
console.log("Serverless Express handler created");
}
const result = await cachedHandler(event, context);
return result;
} catch (error) {
console.error("Error in web handler:", error);
console.error("Error stack:", error.stack);
return {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type,Authorization",
"Access-Control-Allow-Methods": "GET,POST,PUT,DELETE,OPTIONS,PATCH"
},
body: JSON.stringify({
error: "Internal server error",
timestamp: new Date().toISOString()
})
};
}
};
export const socket = async function (event, context) {
try {
await initializeEnvironment();
return await handleSocket(event, context);
} catch (error) {
console.error("Error in socket handler:", error);
return {
statusCode: 500,
body: JSON.stringify({ error: "Socket handler error" })
};
}
};
// Timer handlers. Export name "timer15Min" is legacy — kept as-is so it matches
// the serverless.yml function key/handler string (renaming would recreate the
// EventBridge rule); it actually runs every 30 minutes.
export const timer15Min = async function (event, context) {
try {
await initializeEnvironment();
await handle30MinTimer(event, context);
return { statusCode: 200, body: "Timer executed successfully" };
} catch (error) {
console.error("Error in 30-minute timer:", error);
throw error;
}
};
export const timerMidnight = async function (event, context) {
try {
await initializeEnvironment();
await handleMidnightTimer(event, context);
return { statusCode: 200, body: "Timer executed successfully" };
} catch (error) {
console.error("Error in midnight timer:", error);
throw error;
}
};
export const timerScheduledTasks = async function (event, context) {
try {
await initializeEnvironment();
await handleScheduledTasks(event, context);
return { statusCode: 200, body: "Scheduled tasks executed successfully" };
} catch (error) {
console.error("Error in scheduled tasks timer:", error);
throw error;
}
};
export const sesFeedback = async function (event) {
try {
await initializeEnvironment();
await handleSesFeedback(event);
return { statusCode: 200, body: "SES feedback processed" };
} catch (error) {
console.error("Error in SES feedback handler:", error);
throw error;
}
};
export const timerWebhooks = async function (event, context) {
try {
await initializeEnvironment();
await handleWebhookTimer(event, context);
return { statusCode: 200, body: "Webhook deliveries processed successfully" };
} catch (error) {
console.error("Error in webhook timer:", error);
throw error;
}
};
// Invoked by the deploy workflows after `serverless deploy`; see src/lambda/migrate-handler.ts.
export const migrate = async function () {
await initializeEnvironment();
return await handleMigrate();
};