-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
147 lines (123 loc) · 6.09 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
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
/* Core Dependencies */
const express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const logger = require('./core/logger');
/* Other 3rd Party Dependencies */
const secret = require('./secret/secret.json');
const jwt = require('jsonwebtoken');
const crypto = require('crypto');
const nodemailer = require('nodemailer');
const stripe = require("stripe")(secret.stripe_secret_key);
/* Import Core Modules */
const permissions = require('./core/permissions');
const errorResponseService = require('./core/error-response');
const Tokenizer = require('./core/tokenizer.service');
const Mailer = require('./core/mail.service');
/* Import Middlewares */
const AuthMiddleware = require('./core/auth');
const EmployeesMiddleware = require('./middlewares/employees.middleware');
const UsersMiddleware = require('./middlewares/users.middleware');
const BlacklistMiddleware = require('./middlewares/blacklist.middleware');
const PropertiesMiddleware = require('./middlewares/properties.middleware');
const PricingsMiddleware = require('./middlewares/pricings.middleware');
const AccessMiddleware = require('./middlewares/access.middleware');
const ResetPasswordMiddleware = require('./middlewares/reset-password.middleware');
const WishlistMiddleware = require('./middlewares/wishlist.middleware');
/* Import Controllers */
const EmployeesController = require('./controller/employees.controller');
const EmployeeAuthController = require('./controller/employee-auth.controller');
const BlacklistController = require('./controller/blacklist.controller');
const UserController = require('./controller/user.controller');
const PropertiesController = require('./controller/property.controller');
const PricingsController = require('./controller/pricings.controller');
const AccessController = require('./controller/access.controller');
const ResetPasswordController = require('./controller/reset-password.controller');
const WishlistController = require('./controller/wishlist.controller');
/* Import Config Constants */
const CONFIG = {
WEB_URL: 'https://ms3-web.firebaseapp.com',
TOKEN_CONFIG: require('./configs/token.config'),
ROLES: require('./configs/roles.constants'),
ENTITY_KEYS: require('./configs/entity-keys.constants'),
MAILER: {
FROM: 'ms3.cs506@gmail.com'
}
};
// TODO: CONFIG & ENV should be combined
/* Import Env */
const ENV = require('./environments/environment')();
/* Set-up Scripts */
require('./core/super-admin-creator')();
/* Initialize Application */
const app = express();
const router = express.Router();
/* Global Route Settings */
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, token");
next();
});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
/* Connect DB */
const datastore = ENV.connectToDatastore();
/* Initialize Core Services */
const tokenizer = new Tokenizer(jwt, secret, CONFIG);
const mailer = Mailer(nodemailer, tokenizer, secret, CONFIG);
/* Initialize Middleware */
const auth = AuthMiddleware(datastore, errorResponseService, secret, jwt, CONFIG);
const employees = EmployeesMiddleware(datastore, errorResponseService, secret, crypto, CONFIG);
const users = UsersMiddleware(datastore, errorResponseService, secret, crypto, CONFIG);
const blacklist = BlacklistMiddleware(datastore, errorResponseService, CONFIG);
const pricings = PricingsMiddleware(datastore, errorResponseService, CONFIG);
const access = AccessMiddleware(datastore, errorResponseService, stripe, CONFIG);
const properties = PropertiesMiddleware(datastore, errorResponseService, auth, CONFIG);
const resetPassword = ResetPasswordMiddleware(datastore, errorResponseService, secret, crypto, jwt, CONFIG);
const wishlist = WishlistMiddleware(datastore, errorResponseService, CONFIG);
/* Initialize Controllers */
const employeesController = EmployeesController(express, bodyParser, permissions, auth, employees, CONFIG);
const employeeAuthController = EmployeeAuthController(express, bodyParser, auth, employees);
const blacklistController = BlacklistController(express, bodyParser, permissions, auth, blacklist, CONFIG);
const userController = UserController(express, bodyParser, permissions, mailer, auth, users);
const pricingsController = PricingsController(express, pricings);
const propertiesController = PropertiesController(express, bodyParser, permissions, auth, properties);
const accessController = AccessController(express, bodyParser, auth, access, pricings, mailer);
const resetPasswordController = ResetPasswordController(express, bodyParser, permissions, mailer, auth, users, resetPassword);
const wishlistController = WishlistController(express, bodyParser, auth, wishlist);
/* Add Routes */
router.use('/employees', employeesController);
router.use('/employee-auth', employeeAuthController);
router.use('/blacklist', blacklistController);
router.use('/users', userController);
router.use('/pricings', pricingsController);
router.use('/properties', propertiesController);
router.use('/reset-password', resetPasswordController);
router.use('/wishlist', wishlistController);
// TODO: REFACTOR CONTROLLERS
const authController = require('./controller/auth_controller');
const reactivateController = require('./controller/reactivate_controller');
const activateController = require('./controller/activate_controller');
router.use('/auth', authController);
router.use('/reactivate', reactivateController);
router.use('/activate', activateController);
// TODO /api/access
router.use('/access', accessController);
// TODO /api/
app.get('/', function(req, res){
res.send('Hello');
});
app.use(morgan('dev', {
skip: function (req, res) {
return res.statusCode < 400
}, stream: logger.errorStream
}));
app.use(morgan('dev', {
skip: function (req, res) {
return res.statusCode >= 400
}, stream: logger.stream
}));
/* Run */
app.use('/api', router);
app.listen(ENV.PORT, () => logger.info(`Application Server listening on port ${ENV.PORT}`));