-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
260 lines (221 loc) · 7.6 KB
/
index.ts
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
import {
createProject,
getProjectsByUser,
} from "./controllers/ProjectRecord.ts";
import {
deployProject,
getAccessToken,
getUserData,
} from "./controllers/DeployProject.ts";
import {
createBoilerplate,
getAllBoilerplates,
getBoilerplateById,
updateBoilerplate,
deleteBoilerplate,
} from "./controllers/Boilerplate.ts";
import { addCorsHeaders } from "./helpers/CorsHeader.ts";
import * as mongoose from "mongoose";
import {
checkGithubSession,
createUser,
getFirebaseUserFromEmail,
getFrameworkInfo,
getGithubAccessToken,
getGithubUserDetails,
getRepoContents,
getRepositories,
getUserById,
handleGithubCallback,
handleGithubRevoke,
loginWithGithub,
refreshAccessToken,
updateUserById,
} from "./controllers";
import { initializeApp } from "firebase-admin/app";
import admin from "firebase-admin";
import serviceAccountKey from "./serviceAccountKey.json";
import {
createSupport,
getAllSupportTickets,
getSupportTicketById,
} from "./controllers/Support.controller.ts";
import Stripe from "stripe";
import {
getPaymentById,
handleStripePayment,
handleWebHook,
} from "./controllers/Payment.controller.ts";
import { stripeKey } from "./constants/variable.ts";
import { handleDownloadInvoice } from "./controllers/invoice.controller.ts";
export const stripe = new Stripe(stripeKey!);
initializeApp({
credential: admin.credential.cert(serviceAccountKey as admin.ServiceAccount),
});
type Method = "GET" | "PUT" | "POST" | "DELETE" | "PATCH" | "OPTIONS";
mongoose
.connect(
// "mongodb://localhost:27017/ming"
`mongodb+srv://Cluster53271:${process.env.MONGODB_PASSWORD}@cluster53271.l3uzg.mongodb.net/ming?retryWrites=true&w=majority&appName=Cluster53271`
)
.then(() => {
console.log("Connected to MongoDB!");
console.log("\nHTTP Logs");
})
.catch((err) => {
return console.log(err);
});
const server = Bun.serve({
port: 3000,
development: true,
async fetch(req: Request) {
try {
const url = new URL(req.url);
const method = req.method as Method;
if (method === "OPTIONS") {
return addCorsHeaders(new Response("Preflight request successful"));
}
const apiEndpoint = `${method} ${url.pathname}`;
console.log(Date.now(), apiEndpoint); // works as morgan for Bun
// if (method === "POST" && apiEndpoint === "POST /api/v1/user") {
// // Run validation middleware before createUser
// const validationResponse = await validateUser(req);
// console.log("validte response :", validationResponse);
// if (validationResponse) return validationResponse;
// // Call createUser if validation passed
// return createUser(req);
// }
switch (apiEndpoint) {
case "POST /api/v1/deploy-project":
return deployProject(req);
case "POST /api/v1/create-project":
return createProject(req);
case "GET /api/v1/get-projects":
return getProjectsByUser(req);
// Boilerplate API endpoints
case "POST /api/v1/boilerplate":
return createBoilerplate(req); // Create a new boilerplate
case "GET /api/v1/boilerplates":
return getAllBoilerplates(req); // Get all boilerplates
case `GET /api/v1/boilerplate/${url.pathname.split("/")[4]}`:
return getBoilerplateById(req); // Get a boilerplate by ID
case `PUT /api/v1/boilerplate/${url.pathname.split("/")[4]}`:
return updateBoilerplate(req); // Update a boilerplate by ID
case `DELETE /api/v1/boilerplate/${url.pathname.split("/")[4]}`:
return deleteBoilerplate(req); // Delete a boilerplate by ID
case "GET /api/v1/getAccessToken":
return getAccessToken(req);
case "GET /api/v1/getUserData":
return getUserData(req);
// Github Api Endpoints
case "GET /github/login":
return loginWithGithub(req);
case "POST /github/callback":
return handleGithubCallback(req);
case "GET /github/revoke":
return handleGithubRevoke(req);
case "GET /check-github-session":
return checkGithubSession(req);
// User API endpoints
case "POST /api/v1/user":
return createUser(req);
case `GET /api/v1/user`:
return getUserById(req);
case "POST /api/v1/user/update":
return updateUserById(req);
case "GET /api/v1/user/accessToken":
return getGithubAccessToken(req);
case "GET /api/v1/user/refreshToken":
return refreshAccessToken(req);
case "GET /api/v1/user/repos":
return getRepositories(req);
case "POST /api/v1/user/getRepoContents":
return getRepoContents(req);
case "POST /api/v1/repo/getFrameworkInfo":
return getFrameworkInfo(req);
case "POST /api/v1/getUserData":
return getGithubUserDetails(req);
case "POST /api/v1/user/getFirebaseUserByEmail":
return getFirebaseUserFromEmail(req);
// Support API endpoints
case "POST /api/v1/user/support":
return createSupport(req);
case "GET /api/v1/user/support":
return getSupportTicketById(req);
case "POST /api/v1/user/create-checkout-session":
return handleStripePayment(req);
case "GET /api/v1/user/payments":
return getPaymentById(req);
case "POST /download-invoice":
return handleDownloadInvoice(req);
// case "POST /github/webhook":
// return async function () {
// const payload = await req.json();
// const githubId = payload.pusher.id || payload.sender?.id;
// if (!githubId) {
// return addCorsHeaders(
// new Response(
// JSON.stringify("GitHub user ID not found in payload"),
// {
// status: 400,
// }
// )
// );
// }
// try {
// } catch (error) {
// return addCorsHeaders(
// new Response("Internal Server Error", { status: 500 })
// );
// }
// return addCorsHeaders(
// new Response(
// JSON.stringify({
// message: "Response from github webhook",
// })
// )
// );
// };
case "POST /stripe/webhook":
return handleWebHook(req);
case "GET /api/v1/status":
return addCorsHeaders(
new Response(
JSON.stringify({ message: `I am alive! Thanks for asking. 🥲` }),
{
headers: { "Content-Type": "application/json" },
status: 200,
}
)
);
default:
return addCorsHeaders(
new Response(
JSON.stringify({
message: `You called ${apiEndpoint}, which I don't know how to handle!`,
}),
{ headers: { "Content-Type": "application/json" }, status: 404 }
)
);
}
} catch (err) {
console.error(err);
return addCorsHeaders(
new Response(JSON.stringify({ message: "Internal Server Error" }), {
headers: { "Content-Type": "application/json" },
status: 500,
})
);
}
},
error(error: Error) {
return addCorsHeaders(
new Response(`<pre>${error}\n${error.stack}</pre>`, {
headers: {
"Content-Type": "text/html",
},
})
);
},
});
console.log(`Server running at http://localhost:3000/`);