-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
121 lines (104 loc) · 3.64 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
const express = require('express');
const cloudinary = require('cloudinary').v2;
const fileUpload = require('express-fileupload')
const {sequelize} = require('./utils/database');
const authRoutes = require('./routes/authRoutes');
const tweetRoutes = require('./routes/tweetRoutes');
const profileRoutes = require('./routes/profileRoutes');
const replyRoutes = require('./routes/replyRoutes');
const chatRoutes = require('./routes/chatRoutes');
const User = require('./models/userModel');
const Tweet = require('./models/tweetModel');
const nodeCron = require("node-cron");
const nodecron = require('./utils/cleandb');
const {getGoogleAuthURL,googleAuth} = require('./utils/google');
require('dotenv').config();
const app = express();
const cors=require('cors');
const Tag = require('./models/Tag');
const Likes = require('./models/Likes');
const Bookmarks = require('./models/Bookmark');
const Follow = require('./models/Follow');
const Chat = require('./models/Chat');
const Message = require('./models/Message');
const Chatrel = require('./models/Chatrel');
const Notification = require('./models/Notification');
app.use(cors({origin:true}));
app.use(express.json());
app.use(fileUpload({
useTempFiles:true,
limits:{fileSize: 10*1024*1024}
}));
cloudinary.config({
cloud_name: 'dov6iolx4',
api_key: '266736621665225',
api_secret: 'sJ9QzdCBIDP-1OVnoIw-dFCtZew'
});
// Associations
Tweet.belongsTo(User,{constraints:true,onDelete:'CASCADE'});
User.hasMany(Tweet);
Tweet.belongsToMany(User, { through: Likes });
User.belongsToMany(Tweet, { through: Likes });
Tweet.belongsToMany(User, { through: Bookmarks });
User.belongsToMany(Tweet, { through: Bookmarks });
User.belongsToMany(User,{as:'follower',through: Follow});
Tweet.belongsTo(Tweet,{as:'retweet'});
Tweet.belongsToMany(Tag,{through:'tweettag'});
Tag.belongsToMany(Tweet,{through:'tweettag'});
User.belongsToMany(Chat,{through: Chatrel});
Chat.belongsToMany(User,{through: Chatrel});
Message.belongsTo(Chat);
Chat.hasMany(Message);
Message.belongsTo(User);
User.hasMany(Message);
Tweet.hasMany(Tweet,{sourceKey:'_id',foreignkey:'tweetId'});
Message.belongsTo(Tweet);
Notification.belongsTo(User);
Notification.belongsTo(Tweet,{constraints:true,onDelete:'CASCADE'});
const connectdb = async ()=>{
try {
const result = await sequelize.sync();
console.log('DB Connection has been established successfully.');
const server = app.listen(process.env.PORT);
console.log(`Listening on port ${process.env.PORT}`);
const io = require('socket.io')(server,{
cors:{
origin:'*'
}
});
io.on('connection',(socket)=>{
require('./utils/socket').init(socket);
});
const job = nodeCron.schedule("*/30 * * * *", () => {
nodecron.cleanDB();
console.log("called");
});
} catch (err) {
console.error('Unable to connect to the database:', err);
}
}
connectdb();
app.use(express.static(__dirname + '/public'));
app.use('/uploads', express.static('uploads'));
app.get('/auth/google/url',(req,res)=>{
return res.send(getGoogleAuthURL());
});
app.get('/auth/google',async (req,res)=>{
try{
const code = req.query.code;
const result = await googleAuth(code);
if(result && result.success == true)
return res.status(200).json(result);
else
return res.status(500).json({success:false,msg:'Server Error'});
}catch(err){
console.log(err);
const statusCode = 500;
return res.status(statusCode).json({success:false,msg:err});
}
});
app.use('/c',chatRoutes);
app.use('/t',tweetRoutes);
app.use('/p',profileRoutes);
app.use('/r',replyRoutes);
app.use(authRoutes);