-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.js
76 lines (64 loc) · 2.2 KB
/
app.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
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
//Configurations
let configFilePath =
"./config/" + (process.env.NODE_ENV || "production") + ".env";
var envImportResult = require("dotenv").config({
path: configFilePath
});
if (envImportResult.error) {
throw envImportResult.error;
}
const config = envImportResult.parsed;
console.log("Configuration values, as found in " + configFilePath);
console.log(config);
//Auth related stuff(mostly via SuperTokens)
var cors = require("cors");
const supertokens = require("supertokens-node");
var {
middleware,
errorHandler
} = require("supertokens-node/framework/express");
var AuthService = require("./services/AuthService.js");
AuthService.init();
var app = express();
app.set("views", "./views");
app.set("view engine", "ejs");
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
//AUTH(SuperTokens) related middleware
app.use(
cors({
origin: process.env.SITE_URL,
allowedHeaders: ["content-type", ...supertokens.getAllCORSHeaders()],
credentials: true
})
);
app.use(middleware());
// AUTH(SuperTokens) related middlewares end here
// Logic for serving different routes is handled by categorized controllers
var indexRouter = require("./routes/index");
var userRouter = require("./routes/user");
var authRouter = require("./routes/auth");
app.use("/", indexRouter);
app.use("/auth", authRouter);
app.use("/user", userRouter);
app.use(errorHandler());
var listener = app.listen(8080, async function () {
var EmailService = require("./services/EmailService.js");
await EmailService.init();
EmailService.sendEmail("admin.alert", {
text: process.env.SITE_TITLE + " is up now",
to: process.env.ADMIN_EMAIL,
subject: process.env.SITE_TITLE + " is up now",
addToQueue: true
})
// You can send the same email to admin as above using following fn
EmailService.alertAdmin(process.env.SITE_TITLE + " is up now. Sending this email as you as you're the admin.");
console.log("Listening on port " + listener.address().port);
});