-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathlambda.js
More file actions
178 lines (162 loc) · 5.44 KB
/
Copy pathlambda.js
File metadata and controls
178 lines (162 loc) · 5.44 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
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";
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
});
await Environment.init(stage);
console.log("Environment initialized, connection strings loaded");
// Pools now auto-initialize on first use
}
};
let cachedHandler;
export const web = async function (event, context) {
try {
console.log("Web handler invoked");
console.log("Event httpMethod:", event.httpMethod);
console.log("Event path:", event.path);
if (event.path === "/test") {
return {
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify({
message: "Lambda is working",
path: event.path,
method: event.httpMethod,
stage: process.env.STAGE,
time: new Date().toISOString()
})
};
}
if (event.path === "/test-post" && event.httpMethod === "POST") {
return {
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify({
message: "POST request received",
body: event.body,
headers: event.headers,
method: event.httpMethod,
time: new Date().toISOString()
})
};
}
if (event.path === "/api/test") {
return {
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify({
message: "API routing working",
path: event.path,
method: event.httpMethod,
modules: ["membership", "attendance", "content", "giving", "messaging", "doing"],
time: new Date().toISOString()
})
};
}
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",
message: error.message,
stack: process.env.STAGE === "demo" ? error.stack : undefined,
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 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;
}
};