-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
161 lines (145 loc) · 4.14 KB
/
server.js
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
import express from "express";
import * as dotenv from "dotenv";
import cors from "cors";
import mongoose from "mongoose";
import morgan from "morgan";
import helmet from "helmet";
import authRouter from "./router/authRouter.js";
import adminRouter from "./router/adminRouter.js";
import configRouter from "./router/configRouter.js";
import scaleRouter from "./router/scaleRouter.js";
import analyticsRouter from "./router/analyticsRouter.js";
import { createClient } from "redis";
import RedisStore from "connect-redis";
import session from "express-session";
import passport from "passport";
import GoogleStrategy from "passport-google-oauth2";
import User from "./models/auth/user.js";
import { startCronJobs } from "./lib/cron.js";
import crypto from "crypto";
// Const declarations
dotenv.config();
const app = express();
const PORT = process.env.PORT;
// Configure Redis Client
export const redisClient = createClient({
socket: {
host: process.env.REDIS_HOST,
port: 6379,
},
});
redisClient.connect().then(console.log("Redis Connected")).catch(console.error);
// Middlewares
const corsOptions = {
origin:
process.env.NODE_ENV === "production"
? [
"http://localhost:3000",
"http://localhost:8000",
"http://localhost:5173",
"https://facottry-website-pearl.vercel.app",
"https://client-sdk.vercel.app",
"https://final-netflix-clone-git-main-vishal-aryas-projects.vercel.app",
"https://hotstart-clone-62.vercel.app/https://hotstart-clone-62.vercel.app",
"https://e-commerce-demo-git-main-vishal-aryas-projects.vercel.app",
]
: [
"http://localhost:3000",
"http://localhost:5173",
"http://localhost:8000",
],
credentials: true,
optionSuccessStatus: 200,
};
app.use(cors(corsOptions));
if (process.env.NODE_ENV === "production") app.set("trust proxy", 1);
app.use(express.json());
app.use(morgan("tiny"));
app.use(helmet());
app.use(
session({
store: new RedisStore({
client: redisClient,
}),
credentials: true,
name: "sid",
resave: false,
saveUninitialized: false,
secret: process.env.SESS_SECRET,
cookie: {
secure: process.env.NODE_ENV === "production" ? true : false,
sameSite: process.env.NODE_ENV === "production" ? "none" : "lax",
httpOnly: true,
expires: null,
},
})
);
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.GOOGLE_CALLBACK_URL,
passReqToCallback: true,
},
async function (request, accessToken, refreshToken, profile, done) {
try {
const user = await User.findOne({ email: profile.email });
if (user) {
return done(null, user);
}
const newUser = new User({
googleId: profile.id,
email: profile.email,
name: profile.displayName,
profilePic: profile.picture,
});
await newUser.save();
return done(null, newUser);
} catch (err) {
return done(err, null);
}
}
)
);
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(async function (id, done) {
try {
const user = await User.findById(id);
done(null, user);
} catch (err) {
done(err, null);
}
});
// MongoDB Connection
mongoose
.connect(process.env.MONG_URI)
.then(
app.listen(PORT, () => {
console.log("Connected to MongoDB");
startCronJobs();
if (process.env.NODE_ENV === "production") {
console.log("Production Ready");
} else {
console.log(`Server: http://localhost:${PORT}`);
}
})
)
.catch((err) => {
console.log(err);
});
// Routes
app.get("/", (req, res) => {
// const secret = crypto.randomBytes(32).toString('hex');
// console.log(secret);
return res.send("FacOTTry Backend");
});
app.use("/auth", authRouter);
app.use("/admin", adminRouter);
app.use("/config", configRouter);
app.use("/scale", scaleRouter);
app.use("/analytics", analyticsRouter);