-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
99 lines (80 loc) · 3.15 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
require('dotenv').config();
const express = require('express');
const sequelize = require('./util/db');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
const morgan = require('morgan');
const http = require('http');
const sentryConfig = require('./configuration/sentry');
const socketServerConfig = require('./services/socket').socketServerConfig;
const cronService = require('./services/archive-cron');
cronService.start();
const userRoutes = require('./routes/user');
const groupRoutes = require('./routes/group');
const passwordRoutes = require('./routes/password');
const User = require('./models/user');
const Chat = require('./models/chat');
const Group = require('./models/group');
const UserGroup = require('./models/user-group');
const ForgetPassword = require('./models/forget-password');
//-----------------------------------------------------------------------------------------
const accessLogStream = fs.createWriteStream(
path.join(__dirname, 'access.log'),
{flags: 'a'}
);
const app = express();
const server = http.createServer(app);
socketServerConfig(server); //SOCKET SERVER CONFIG
const Sentry = sentryConfig(server); //SENTRY CONFIG
app.use(Sentry.Handlers.requestHandler());
// app.use(helmet());
// app.use(compression());
app.use(morgan('combined', {stream: accessLogStream}));
app.use(cors({
origin: JSON.parse(`${process.env.ACCEPTED_ORIGINS}`)
}));
app.use(express.json({extended: false}));
app.use('/public', express.static(path.join(__dirname, 'public')));
//------------------------------------ROUTES----------------------------------------------
app.use('/users', userRoutes);
app.use('/groups', groupRoutes);
app.use('/password', passwordRoutes);
app.use((req,res) => {
// console.log(__dirname, req.url);
const fileExists = fs.existsSync(path.join(__dirname, `/views/${req.url}`));
if(req.url === '/'){
req.url = 'home.html';
return res.sendFile(path.join(__dirname, `/views/${req.url}`));
}
else if(fileExists)
return res.sendFile(path.join(__dirname, `/views/${req.url}`));
else
return res.sendFile(path.join(__dirname, `/views/error404.html`));
});
// -----------------------------------SENTRY----------------------------------------------------
app.use(Sentry.Handlers.errorHandler()); //sentry error handler
// -----------------------------------MODEL ASSOSCIATIONS-----------------------------------------------------
User.hasMany(Chat);
Chat.belongsTo(User);
Group.hasMany(Chat);
Chat.belongsTo(Group);
User.belongsToMany(Group, {through: UserGroup});
Group.belongsToMany(User, {through: UserGroup});
UserGroup.belongsTo(User);
UserGroup.belongsTo(Group);
User.hasMany(ForgetPassword);
ForgetPassword.belongsTo(User);
//-----------------------------------------------------------------------------------------
const serverSync = async()=>{
try{
// await sequelize.sync({force: true});
await sequelize.sync()
server.listen(process.env.APP_PORT || 4000);
console.log(`server running on PORT: ${process.env.APP_PORT}`);
}
catch(err){
console.error(err);
}
}
serverSync();