-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (145 loc) · 5.8 KB
/
index.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
162
163
164
165
166
"use strict";
require("dotenv").config();
const express = require("express");
const handlebars = require("express-handlebars");
const subdomain = require("express-subdomain");
const models = require("./models");
const session = require("express-session");
const redisStore = require("connect-redis").default;
const { createClient } = require("redis");
const passport = require("./controllers/passport");
const connectFlash = require("connect-flash");
const { createPagination } = require("express-handlebars-paginate");
const { initializeApp } = require("firebase/app");
const logger = require('morgan');
const braintree = require('braintree');
// Configure redis connection
const redisClient = createClient({ url: process.env.REDIS_URL });
redisClient.connect().then(() => {
// Paypal Braintree Gateway configs
const braintreeGateway = new braintree.BraintreeGateway({
environment: braintree.Environment.Sandbox,
merchantId: process.env.BRAINTREE_MERCHANT_ID,
publicKey: process.env.BRAINTREE_PUBLIC_KEY,
privateKey: process.env.BRAINTREE_PRIVATE_KEY
});
// Firebase configuration
const firebaseConfig = {
apiKey: process.env.FIREBASE_API_KEY,
authDomain: process.env.FIREBASE_AUTH_DOMAIN,
projectId: process.env.FIREBASE_PROJECT_ID,
storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.FIREBASE_APP_ID
};
const firebaseApp = initializeApp(firebaseConfig);
module.exports = { braintreeGateway, firebaseApp };
// Initialize ExpressJS application
const app = express();
app.use(logger('dev'));
// Set static folder
app.use(express.static(__dirname + "/public"));
// Config handlebars
app.engine(
"hbs",
handlebars.engine({
layoutsDir: __dirname + "/views/layouts",
partialsDir: __dirname + "/views/partials",
extname: "hbs",
defaultLayout: "layout",
runtimeOptions: {
allowProtoPropertiesByDefault: true,
},
helpers: {
createPagination,
equal: function (a, b) {
return a === b;
}
},
})
);
app.set("view engine", "hbs");
// To be able to get data from POST request body
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// Setup session
app.use(
session({
secret: process.env.SESSION_SECRET,
store: new redisStore({ client: redisClient }),
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 20 * 60 * 1000,
httpOnly: true,
},
})
);
// To create database tables
// app.use("/create-database-tables", (req, res) => {
// const models = require("./models");
// models.sequelize.sync().then(() => {
// res.send("models created successfully");
// });
// });
// Save subdomains to session so that passport.deserializeUser knows
// what subdomain being used
app.use((request, response, next) => {
request.session.subdomains = request.subdomains;
next();
});
// Setup Passport
app.use(passport.initialize());
app.use(passport.session());
// Setup Connect Flash
app.use(connectFlash());
// Subdomains routes
app.use("/auth", subdomain("writer", require("./routers/writer/authRoutes")));
app.use("/edit", subdomain("writer", require('./routers/writer/editRoutes')));
app.use("/", subdomain("writer", require("./routers/writer/writerRoutes")));
app.use("/auth", subdomain("editor", require("./routers/editor/authRoutes")));
app.use("/edit", subdomain("editor", require("./routers/editor/editRoutes")));
app.use(subdomain("editor", require("./routers/editor/editorRoutes")));
app.use("/auth", subdomain("admin", require("./routers/admin/authRouter")));
app.use(subdomain("admin", require("./routers/admin/adminRouter")));
// User's Middleware
app.use(require('./controllers/user/accountController').middleware);
app.use(async (request, response, next) => {
// Query categories for header menu
const categories = await models.Category.findAll({
attributes: ["id", "name"],
include: [
{
model: models.SubCategory,
attributes: ["id", "name"],
},
],
});
response.locals.headerCategories = categories;
next();
});
app.use("/", require("./routers/user/indexRouter"));
app.use("/news", require("./routers/user/newsRouter"));
app.use("/auth", require("./routers/user/authRouter"));
app.use("/account", require("./routers/user/accountRouter"));
// Handle request from client to generate braintree client token
app.get('/braintree_client_token', (req, res) => {
const customerId = req.user ? req.user.braintreeCustomerId : null;
braintreeGateway.clientToken.generate({ customerId: customerId }, (error, response) => {
res.json({ clientToken: response.clientToken });
})
});
app.use((req, res, next) => {
res.locals.pageTitle = 'Không tìm thấy trang!';
res.status(404).render("error", { message: "404 - File not Found!" });
});
app.use((error, req, res, next) => {
console.error(error);
res.locals.pageTitle = 'Lỗi Server';
res.status(500).render("error", { message: "500 - Internal Server Error!" });
});
app.set("port", process.env.PORT || 5000);
app.listen(app.get("port"), () => {
console.log(`Server is running on port ${app.get("port")}`);
});
}).catch(error => console.log(error));