-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
308 lines (286 loc) · 11.7 KB
/
server.js
File metadata and controls
308 lines (286 loc) · 11.7 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
require("dotenv").config();
const http = require("http");
const fs = require("fs");
const path = require("path");
const url = require("url");
// Import pfp handler
const telegramPfpHandler = require("./api/telegram/pfps/[id]");
// Import handlers
const summariesHandler = require("./api/summaries");
const githubHandler = require("./api/github/[...params]");
const telegramUsersHandler = require("./api/telegram/users");
const discordUserStatsHandler = require("./api/discord/user-stats");
const devrankUserStatsHandler = require("./api/devrank/user-stats");
const xUserStatsHandler = require("./api/x/user-stats");
// Import list handlers
const discordListHandler = require("./api/discord");
const githubListHandler = require("./api/github");
const telegramListHandler = require("./api/telegram");
const xListHandler = require("./api/x");
const xSeedListHandler = require("./api/x_seed");
const communitiesListHandler = require("./api/communities/index");
const PORT = process.env.PORT || 3000;
// Mock Vercel request/response for local development
function createMockVercelContext(req, res, params, queryParams) {
req.query = {
params: params,
...queryParams,
};
}
function serveStaticFile(filePath, res) {
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("404 Not Found");
return;
}
const ext = path.extname(filePath);
const contentTypes = {
".html": "text/html",
".css": "text/css",
".js": "text/javascript",
".json": "application/json",
".png": "image/png",
".jpg": "image/jpeg",
".gif": "image/gif",
".svg": "image/svg+xml",
};
res.writeHead(200, { "Content-Type": contentTypes[ext] || "text/plain" });
res.end(data);
});
}
const server = http.createServer(async (req, res) => {
const parsedUrl = url.parse(req.url, true);
const pathname = parsedUrl.pathname;
const queryParams = parsedUrl.query;
console.log(`${req.method} ${pathname}`);
// Serve static files from public directory
if (pathname === "/" || pathname === "/index.html") {
serveStaticFile(path.join(__dirname, "public", "index.html"), res);
return;
}
// Handle platform routes
const pathParts = pathname
.slice(1)
.split("/")
.filter((part) => part);
if (pathParts.length > 0) {
const platform = pathParts[0];
const params = pathParts.slice(1);
try {
// Mock Vercel request format
createMockVercelContext(req, res, params, queryParams);
// Route to appropriate handler
switch (platform) {
case "summaries":
await summariesHandler(req, res);
return;
case "discord":
if (params.length === 0) {
// List all available datasets
await discordListHandler(req, res);
} else if (params[0] === "user-stats") {
// Handle /discord/user-stats endpoint
await discordUserStatsHandler(req, res);
} else {
// New file-based routing for discord (like telegram/x)
const serverId = params[0];
req.query.serverId = serverId;
if (params.length === 1) {
// /discord/:serverId -> index.js
await require("./api/discord/[serverId]/index")(req, res);
} else if (params.length === 2) {
const endpoint = params[1];
if (endpoint === "seed") {
await require("./api/discord/[serverId]/seed")(req, res);
} else if (endpoint === "scores") {
await require("./api/discord/[serverId]/scores")(req, res);
} else {
// Fallback or 404 for unknown sub-endpoints
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Endpoint not found" }));
}
} else {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Endpoint not found" }));
}
}
return;
case "github":
if (params.length === 0) {
// List all available datasets
await githubListHandler(req, res);
} else {
// Handle specific dataset
await githubHandler(req, res);
}
return;
case "telegram":
if (params.length === 0) {
// List all available datasets
await telegramListHandler(req, res);
} else if (params[0] === "pfps" && params.length >= 2) {
// Handle profile picture requests
req.query.id = params[1];
await telegramPfpHandler(req, res);
} else if (params[0] === "users") {
// Handle /telegram/users endpoint
await telegramUsersHandler(req, res);
} else {
// New file-based routing for telegram
const channelId = params[0];
req.query.channelId = channelId;
if (params.length === 1) {
// /telegram/:channelId -> index.js
await require("./api/telegram/[channelId]/index")(req, res);
} else if (params.length === 2) {
const endpoint = params[1];
if (endpoint === "seed") {
await require("./api/telegram/[channelId]/seed")(req, res);
} else if (endpoint === "scores") {
await require("./api/telegram/[channelId]/scores")(req, res);
} else {
// Fallback or 404 for unknown sub-endpoints
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Endpoint not found" }));
}
} else {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Endpoint not found" }));
}
}
return;
case "x":
if (params.length === 0) {
// List all available datasets
await xListHandler(req, res);
} else if (params[0] === "user-stats") {
// Handle /x/user-stats endpoint
await xUserStatsHandler(req, res);
} else {
// New file-based routing for x
const communityId = params[0];
req.query.communityId = communityId;
if (params.length === 1) {
// /x/:communityId -> index.js
await require("./api/x/[communityId]/index")(req, res);
} else if (params.length === 2) {
const endpoint = params[1];
if (endpoint === "seed") {
await require("./api/x/[communityId]/seed")(req, res);
} else if (endpoint === "scores") {
await require("./api/x/[communityId]/scores")(req, res);
} else {
// Fallback or 404 for unknown sub-endpoints
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Endpoint not found" }));
}
} else {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Endpoint not found" }));
}
}
return;
case "x_seed":
if (params.length === 0) {
// List all available datasets
await xSeedListHandler(req, res);
} else {
// File-based routing for x_seed
const communityId = params[0];
req.query.communityId = communityId;
if (params.length === 1) {
// /x_seed/:communityId -> index.js
await require("./api/x_seed/[communityId]/index")(req, res);
} else if (params.length === 2) {
const endpoint = params[1];
if (endpoint === "seed") {
await require("./api/x_seed/[communityId]/seed")(req, res);
} else if (endpoint === "scores") {
await require("./api/x_seed/[communityId]/scores")(req, res);
} else {
// Fallback or 404 for unknown sub-endpoints
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Endpoint not found" }));
}
} else {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Endpoint not found" }));
}
}
return;
case "devrank":
if (params[0] === "user-stats") {
// Handle /devrank/user-stats endpoint
await devrankUserStatsHandler(req, res);
} else {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Endpoint not found" }));
}
return;
case "communities":
if (params.length === 0) {
// List all communities
await communitiesListHandler(req, res);
} else {
// File-based routing for communities
const communityId = params[0];
req.query.communityId = communityId;
if (params.length === 1) {
// /communities/:communityId -> index.js
await require("./api/communities/[communityId]/index")(req, res);
} else {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Endpoint not found" }));
}
}
return;
}
} catch (error) {
console.error("Error handling request:", error);
res.writeHead(500, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Internal server error" }));
return;
}
}
// 404 for everything else
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("404 Not Found");
});
server.listen(PORT, () => {
console.log("");
console.log("╔════════════════════════════════════════════════════════╗");
console.log("║ ║");
console.log("║ 🚀 OpenRank API Server ║");
console.log("║ ║");
console.log("╚════════════════════════════════════════════════════════╝");
console.log("");
console.log(`📡 Server running at: http://localhost:${PORT}`);
console.log(`📖 Documentation: http://localhost:${PORT}`);
console.log("");
console.log("Available endpoints:");
console.log(` • http://localhost:${PORT}/summaries - Fetch summaries`);
console.log(
` • http://localhost:${PORT}/discord - List all Discord datasets`,
);
console.log(` • http://localhost:${PORT}/github - List all GitHub datasets`);
console.log(
` • http://localhost:${PORT}/telegram - List all Telegram datasets`,
);
console.log(` • http://localhost:${PORT}/x - List all X datasets`);
console.log(` • http://localhost:${PORT}/x_seed - List all X Seed datasets`);
console.log(` • http://localhost:${PORT}/discord/ritual`);
console.log(` • http://localhost:${PORT}/github/bitcoin`);
console.log(` • http://localhost:${PORT}/telegram/decentraliseddotco`);
console.log(` • http://localhost:${PORT}/x/ritual-community`);
console.log("");
console.log("Press Ctrl+C to stop the server");
console.log("");
});
process.on("SIGINT", () => {
console.log("\n\n👋 Shutting down server...\n");
server.close(() => {
console.log("✅ Server closed\n");
process.exit(0);
});
});