-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
67 lines (59 loc) · 2.03 KB
/
main.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
const fastify = require("fastify")({ logger: true });
const welcomeData = require("./welcome.js");
const { getRoutes } = require("./route/getRoutes.js");
const { postRoutesNoAuth, postRoutesAuth } = require("./route/postRoutes.js");
const { connectToMongoDB } = require("./config/mongodb.js");
const PORT = process.env.PORT || 3000;
const fs = require('fs');
const path = require('path');
const brevoEmailer = require("./SMTP/brevoEmailer.js");
fastify.register(require("@fastify/cors"), {
origin: "*",
credentials: true,
optionSuccessStatus: 200,
});
fastify.register(async (instance, options) => {
const db = await connectToMongoDB();
// const db = {};
instance.get("/", async (request, reply) => {
try {
const htmlPath = path.join(__dirname, 'index.html');
const htmlContent = fs.readFileSync(htmlPath, 'utf8');
reply.type('text/html').send(htmlContent);
} catch (err) {
console.log(err);
reply.code(500).send({
status: 500,
error: "Internal Error",
message: err,
});
}
});
instance.get("/sendtestemail", async (request, reply) => {
const emailtemplate = path.join(__dirname, './SMTP/template/OTP.html');
const templateContent = fs.readFileSync(emailtemplate, 'utf8');
const data = await brevoEmailer("Forgot Password",templateContent,"[email protected]");
reply.code(200).send(data);
})
getRoutes.forEach((item) => {
instance.get(item.route, async (request, reply) =>
item.fetchFunction(item.fetcher, request, reply)
);
});
postRoutesNoAuth.forEach((item) => {
instance.post(item.route, async (request, reply) =>
item.postFuntion(request, reply, db)
);
});
postRoutesAuth.forEach((item) => {
instance.post(
item.route,
{ preHandler: item.authFunction },
async (request, reply) => item.postFuntion(request, reply, db)
);
});
});
fastify.listen(PORT, "0.0.0.0", (err, address) => {
if (err) throw err;
console.log(`server: ${address} listening on http://localhost:${PORT}`);
});