-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
169 lines (152 loc) · 4.66 KB
/
Copy pathindex.ts
File metadata and controls
169 lines (152 loc) · 4.66 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
import express from "express";
import cors from "cors";
import dotenv from "dotenv";
import session from "express-session";
import passport from "passport";
import { Strategy as GoogleStrategy } from "passport-google-oauth20";
import authRoutes from "./routes/auth";
import accountRoutes from "./routes/account";
import leaderboardRoutes from "./routes/leaderboard";
import profileRoutes from "./routes/profile";
import contestRoutes from "./routes/contest";
import potdRoutes from "./routes/potd";
import wrappedRoutes from "./routes/wrapped";
import eventRoutes from "./routes/event";
import { RedisStore } from "connect-redis";
import { createClient } from "redis";
import { client, db } from "./drizzle/db";
import { users } from "./drizzle/schema";
import { eq } from "drizzle-orm";
import { startCronJobs } from "./cron";
import {cruxMembers} from "./middlewares/auth"
import {
fetchContests,
fetchProblems,
fetchSubmissions,
fetchRatingChanges,
} from "./controllers/codeforces";
import { rateLimit } from "express-rate-limit";
import "./workers/codeforcesWorker";
dotenv.config();
const app = express();
app.set("trust proxy", 1);
app.use(
cors({
origin: process.env.VITE_CLIENT_URL || "http://localhost:4173",
credentials: true,
})
);
app.use(express.json());
const redisClient = createClient({
url: process.env.REDIS_URL || "redis://localhost:6379",
});
redisClient.connect().catch(console.error);
(async () => {
await client
.connect()
.then(() => console.log("Connected to DB successfully"))
.catch((error) => {
console.error("Error connecting to db: ", error);
});
})();
app.use(
session({
store: new RedisStore({ client: redisClient }),
secret: process.env.SESSION_SECRET || "default_secret",
resave: false,
saveUninitialized: false,
cookie: {
sameSite: "lax",
secure: process.env.NODE_ENV === "production",
maxAge: 7 * 24 * 60 * 60 * 1000, // 7 days
},
})
);
app.use(passport.initialize());
app.use(passport.session());
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
callbackURL: process.env.CALLBACK_URL!,
},
async (accessToken, refreshToken, profile, done) => {
try {
const email = profile.emails![0].value;
const name = profile.displayName;
const pfpUrl = profile.photos![0].value;
const user = await db
.insert(users)
.values({ email, name, pfpUrl })
.onConflictDoUpdate({
target: users.email,
set: { name },
})
.returning()
.then((rows) => rows[0]);
return done(null, user);
} catch (error) {
console.error("Error during Google authentication:", error);
return done(new Error("Authentication failed"));
}
}
)
);
passport.serializeUser((user, done) => {
const drizzleUser = user as typeof users.$inferSelect;
done(null, drizzleUser.id);
});
passport.deserializeUser(async (userId: string, done) => {
// console.log(userId);
try {
const user = await db
.select()
.from(users)
.where(eq(users.id, userId))
.limit(1)
.then((rows) => rows[0]);
if (user) {
const userwithisCruxMemberProperty= {
...user,
isCruxMember: cruxMembers.has(user.cfHandle ?? ''),
};
return done(null, userwithisCruxMemberProperty);
}
done(null, null);
} catch (error) {
console.error("Error deserializing user:", error);
done(new Error("Could not deserialize user"), null);
}
});
const limiter = rateLimit({
windowMs: 1000,
limit: 25,
standardHeaders: "draft-8", // draft-6: `RateLimit-*` headers; draft-7 & draft-8: combined `RateLimit` header
legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
ipv6Subnet: 56, // Set to 60 or 64 to be less aggressive, or 52 or 48 to be more aggressive
// store: ... , // Redis, Memcached, etc. See below.
});
app.use(limiter);
app.get("/", (_req, res) => {
res.send("Backend API is working!");
});
app.use("/auth", authRoutes);
app.use("/account", accountRoutes);
app.use("/leaderboard", leaderboardRoutes);
app.use("/profile", profileRoutes);
app.use("/contest", contestRoutes);
app.use("/potd", potdRoutes);
app.use("/wrapped", wrappedRoutes);
app.use("/event", eventRoutes);
const PORT = parseInt(process.env.BACKEND_PORT || "5000", 10);
app.listen(PORT, "0.0.0.0", async () => {
console.log(`Server running on port ${PORT}`);
await fetchContests(1);
await fetchProblems(1);
if (process.env.VITE_ENV === "production") {
await fetchSubmissions();
await fetchRatingChanges();
startCronJobs();
}
});