-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
228 lines (183 loc) · 6.05 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
require('dotenv').config();
const useAPM = process.env.USE_APM || false;
const apm = require('elastic-apm-node').start({
serviceName: process.env.APP_NAME,
serverUrl : process.env.SERVER_URL || 'http://localhost:8200',
environment: 'development',
active: useAPM,
});
const express = require('express');
const app = express();
const socketIO = require('socket.io');
const http = require('http');
const cors = require('cors');
const swaggerUi = require('swagger-ui-express'); // import swagger
let logger = require('morgan');
const fs = require('fs');
const moment = require('moment-timezone');
const bcrypt = require('bcrypt');
const _ = require('lodash');
const cloudinary = require('./libs/handle_upload');
const generateToken = require('./helper/jwt');
const googleOauth = require('./libs/google-auth');
const func = require('./libs/function');
const defaultImage = require('./internal/constant/defaultImage');
const email_message = require('./internal/constant/email_message');
const serverError = require('./middleware/serverError');
const orderConstant = require('./internal/constant/order');
const CategoryRepository = require('./repository/category');
const CategoryUseCase = require('./usecase/category');
const AddressRepository = require('./repository/address');
const AddressUseCase = require('./usecase/address');
const ProductUseCase = require('./usecase/product');
const ProductRepository = require('./repository/product');
const OrderUseCase = require('./usecase/order');
const OrderRepository = require('./repository/order');
const OrderDetailRepository = require('./repository/orderDetail');
const UserRepository = require('./repository/user');
const UserUseCase = require('./usecase/user');
const AuthRepository = require('./repository/auth');
const AuthUseCase = require('./usecase/auth');
const ProductImageRepository = require('./repository/product_image');
const ProductImageUseCase = require('./usecase/product_image');
const EmailRepository = require('./repository/email');
const OtpRepository = require('./repository/otp');
const OtpUseCase = require('./usecase/otp');
const ChatRepository = require('./repository/chat');
const ChatUseCase = require('./usecase/chat');
const customerRouter = require('./routes/customer');
const publicRouter = require('./routes/public');
const authRouter = require('./routes/auth');
const adminRouter = require('./routes/admin');
const otpRouter = require('./routes/otp');
const addressUC = new AddressUseCase(
new AddressRepository(),
new UserRepository(),
);
const categoryUC = new CategoryUseCase(new CategoryRepository(), new ProductRepository());
const productUC = new ProductUseCase(
new ProductRepository(),
new CategoryRepository(),
new ProductImageRepository(),
defaultImage,
_,
);
const userUC = new UserUseCase(
new UserRepository(),
new OtpRepository(),
bcrypt,
cloudinary,
);
const chatUC = new ChatUseCase(new ChatRepository(), new UserRepository(), _);
const authUC = new AuthUseCase(
new AuthRepository(),
new UserRepository(),
new OtpRepository(),
bcrypt,
cloudinary,
generateToken,
_,
googleOauth,
func,
defaultImage,
);
const productImageUC = new ProductImageUseCase(
new ProductImageRepository(),
new ProductRepository(),
cloudinary,
_,
defaultImage,
);
const orderUC = new OrderUseCase(
new OrderRepository(),
new OrderDetailRepository(),
new ProductRepository(),
new CategoryRepository(),
new EmailRepository(),
new UserRepository(),
_,
new AddressRepository(),
orderConstant,
);
const otpUC = new OtpUseCase(
new OtpRepository(),
new EmailRepository(),
email_message,
);
const ACCESS_LOG = process.env.ACCESS_LOG || './logs/access.log';
const ERROR_LOG = process.env.ERROR_LOG || './logs/errors.log';
app.set('view engine', 'ejs');
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use('/public', express.static('public'));
// Logger
logger.token('date', (req, res, tz) => moment().tz(tz).format());
logger.format('custom_format', ':remote-addr - :remote-user [:date[Asia/Jakarta]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"');
app.use(logger('custom_format', {
stream: fs.createWriteStream(ACCESS_LOG, { flags: 'a' }),
}));
app.use(logger('custom_format', {
skip(req, res) { return res.statusCode < 400; },
stream: fs.createWriteStream(ERROR_LOG, { flags: 'a' }),
}));
app.use((req, res, next) => {
req.categoryUC = categoryUC;
req.productUC = productUC;
req.userUC = userUC;
req.addressUC = addressUC;
req.productImageUC = productImageUC;
req.orderUC = orderUC;
req.authUC = authUC;
req.otpUC = otpUC;
req.chatUC = chatUC;
next();
});
app.get('/', (req, res) => {
// #swagger.ignore = true
res.send('Hello Platinum Maju Jaya');
});
app.use('/', authRouter);
app.use('/', adminRouter);
app.use('/', customerRouter);
app.use('/', publicRouter);
app.use('/', otpRouter);
// handle server error
app.use(serverError);
const swaggerDocument = require('./docs/docs_custom.json');
const adminSwaggerDocument = require('./docs/admin_docs_custom.json');
app.use(
'/docs/admin',
swaggerUi.serveFiles(adminSwaggerDocument),
swaggerUi.setup(adminSwaggerDocument),
);
app.use(
'/docs',
swaggerUi.serveFiles(swaggerDocument),
swaggerUi.setup(swaggerDocument),
);
const httpServer = http.createServer(app);
const authorizeWebSocket = require('./middleware/socket_io');
const io = socketIO(httpServer);
io.use(authorizeWebSocket);
io.on('connection', (socket) => {
let userId = socket.handshake.auth.user.id;
let room = `room_${userId}`;
socket.join(room);
socket.on('sendMessage', async (messageData) => {
let recipient = messageData.recipient_id;
messageData.sender_id = userId;
let result = await chatUC.addChat(messageData);
if (result !== null) {
socket.emit('onNewMessage', messageData);
socket.to(`room_${recipient}`).emit('onNewMessage', {
...result,
is_sender: false,
});
}
});
socket.on('disconnected', () => {
console.log('user is disconnected');
});
});
module.exports = httpServer;