-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
192 lines (160 loc) · 5.38 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
import express from "express";
import arcjet, {
createRemoteClient,
defaultBaseUrl,
} from "@arcjet/node";
import { createConnectTransport } from "@connectrpc/connect-web";
import http from "http";
import { getPostSummaries, getPostDetail } from "./utils/vrite.ts";
import { getMessage, getPortfolio } from "./utils/files.ts";
import { streamData } from "./utils/streaming.ts";
import { getGist } from "./utils/apis.ts";
import { get10DaysLeaderboard, get10daysDetailForUser } from "./utils/slack.ts";
const app = express();
const port = 3000;
const transport = createConnectTransport({
baseUrl: defaultBaseUrl(),
fetch,
});
const client = createRemoteClient({
transport,
baseUrl: defaultBaseUrl(),
timeout: 500,
});
const aj = arcjet({
key: process.env.ARCJET_KEY!,
rules: [],
client,
});
// Middleware to set Content-Type and enable streaming
app.use((req, res, next) => {
const userAgent = req.headers["user-agent"] as string;
if (userAgent.includes("Firefox") || userAgent.includes("MSIE")) {
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.setHeader("Content-Disposition", "inline"); // This will display content in browser
} else {
res.setHeader("Content-Type", "text/event-stream; charset=utf-8");
}
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
next();
});
// return the favicon
app.get("/favicon.ico", async (req, res, next) => {
const decision = await aj.protect(req);
if (decision.isDenied()) {
res.writeHead(429, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Too Many Requests" }));
return;
}
res.sendFile("content/favicon.ico", { root: "." });
});
// get the home page message and stream it
app.get("/", async (req, res) => {
const decision = await aj.protect(req);
if (decision.isDenied()) {
res.writeHead(429, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Too Many Requests" }));
return;
}
const message = await getMessage();
streamData(req, res, message, res.get("Content-Type"));
});
// get the blog posts's summaries and descriptions and stream them
app.get("/blog", async (req, res) => {
const decision = await aj.protect(req);
if (decision.isDenied()) {
res.writeHead(429, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Too Many Requests" }));
return;
}
const posts = await getPostSummaries();
streamData(req, res, posts);
});
// stream the blog post's content by slug
app.get("/blog/:slug", async (req, res) => {
const decision = await aj.protect(req);
if (decision.isDenied()) {
res.writeHead(429, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Too Many Requests" }));
return;
}
const post = await getPostDetail(req.params.slug);
streamData(req, res, post);
});
// get a gist by id and stream it
app.get("/g/:id", async (req, res) => {
const decision = await aj.protect(req);
if (decision.isDenied()) {
res.writeHead(429, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Too Many Requests" }));
return;
}
const gist = await getGist(req.params.id);
streamData(req, res, gist);
});
// #10daysinpublic leaderboard
app.get("/s/10daysinpublic", async (req, res) => {
const decision = await aj.protect(req);
if (decision.isDenied()) {
res.writeHead(429, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Too Many Requests" }));
return;
}
const leaderboard = await get10DaysLeaderboard(
new Date("2024-02-15"),
new Date("2024-02-30"),
);
streamData(req, res, leaderboard);
});
app.get("/s/10daysinpublic/:user", async (req, res) => {
const decision = await aj.protect(req);
if (decision.isDenied()) {
res.writeHead(429, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Too Many Requests" }));
return;
}
const userDetail = await get10daysDetailForUser(
decodeURIComponent(req.params.user),
);
streamData(req, res, userDetail);
});
app.get("/portfolio", async (req, res) => {
const decision = await aj.protect(req);
if (decision.isDenied()) {
res.writeHead(429, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Too Many Requests" }));
return;
}
const portfolio = await getPortfolio("");
streamData(req, res, portfolio);
});
app.get("/portfolio/:companyID", async (req, res) => {
const decision = await aj.protect(req);
if (decision.isDenied()) {
res.writeHead(429, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Too Many Requests" }));
return;
}
const portfolio = await getPortfolio(req.params.companyID);
streamData(req, res, portfolio);
});
let logger = (req: any, res: any, next: any) => {
let current_datetime = new Date();
let formatted_date = current_datetime.toISOString();
let method = req.method;
let url = req.url;
let status = res.statusCode;
let user_agent = req.headers["user-agent"];
let log = `\x1b[36m[${formatted_date}]\x1b[0m ${method}:${url} ${status} ${user_agent}`;
console.log(log); // Highlight log in cyan color
};
// Create server
const server = http.createServer(app);
// add logging middleware
server.on("request", logger);
// Start server
server.listen(port, () => {
console.log(`Server is listening on port ${port}`);
console.log(`Visit http://localhost:${port}`);
});