forked from FSD-Project-24/Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
172 lines (158 loc) · 5.47 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
const express = require("express");
const path = require("path");
const app = express();
const fs = require("fs");
const ejs = require("ejs");
const cors = require('cors');
const cookieParser = require('cookie-parser');
const dotenv = require('dotenv');
var globalVariables = require('./public/js/global_variables');
app.use(cors());
app.use(cookieParser());
// .evn congif
dotenv.config();
// importing modals
const Post = require('./models/Post');
const Comment = require('./models/Comment');
const naiveUser = require("./models/naiveUser");
const expertUser = require("./models/expertUser");
const supremeUser = require("./models/supremeUser");
// importing Routes
const mongoose = require("mongoose");
const assetRoutes = require("./routes/assetRoutes");
const authRoutes = require("./routes/authRoutes");
const postRoutes = require("./routes/postRoutes");
const commentRoutes = require('./routes/commentRoutes');
const communityRoutes = require('./routes/communityRoutes');
const userRoutes = require("./routes/userRoutes");
const stablishConnection = require("./db/connection");
// importing Utility Classes
const Validation = require('./utils/Validation');
const { PEP,RBACMiddleware,ABACMiddleware,ChineseWallPolicy,PDP } = require("./utils/PolicyEnforcementPoint");
// importing Middlewares
const isNaive = require('./middlewares/isNaive');
const Community = require("./models/Community");
// Express Configuration
const port = process.env.PORT || 5000;
const viewspath = path.join(__dirname, "views");
app.use(express.static(__dirname + "/public"));
app.set("views", __dirname + "/views");
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Establishing the mongoose connection
stablishConnection();
// Routes
app.use("/api/assets", assetRoutes);
app.use("/api/auth", authRoutes);
app.use("/api/posts",postRoutes);
app.use("/api/comments",commentRoutes);
app.use("/api/communities",communityRoutes);
app.use("/api/users",userRoutes);
// Creating Instances
const rbacMiddleware = new RBACMiddleware();
const validation = new Validation();
// Pages Rendering
// main
app.get("/", async (req, res) => {
if(!req.cookies.jwt){
return res.render('index.ejs',{jwt: null,user: null});
}
const user = await validation.getUser(req.cookies.jwt);
res.render('index.ejs',{user,jwt: req.cookies.jwt});
});
app.get("/profile",async (req,res) =>{
if(!req.cookies.jwt){
return res.render('profile.ejs',{jwt: req.cookies.jwt,user: null});
}
const user = await validation.getUser(req.cookies.jwt);
res.render('profile.ejs',{user,jwt: req.cookies.jwt});
})
app.get('/portfolio',async (req,res)=>{
if(!req.cookies.jwt){
return res.render('portfolio.ejs',{jwt: req.cookies.jwt,user: null});
}
const user = await validation.getUser(req.cookies.jwt);
res.render('portfolio.ejs',{user,jwt: req.cookies.jwt});
})
app.get('/screener',async (req,res)=>{
if(!req.cookies.jwt){
return res.render('screener.ejs',{jwt: req.cookies.jwt,user: null});
}
const user = await validation.getUser(req.cookies.jwt);
res.render('screener.ejs',{user,jwt: req.cookies.jwt});
})
app.get('/gold',async (req,res)=>{
if(!req.cookies.jwt){
return res.render('gold.ejs',{jwt: req.cookies.jwt,user: null});
}
const user = await validation.getUser(req.cookies.jwt);
res.render('gold.ejs',{user,jwt: req.cookies.jwt});
})
app.get('/login',(req,res)=>{
res.render('login.ejs');
})
app.get('/register',(req,res)=>{
res.render('register.ejs');
})
// admin panel
app.get('/admin',async (req,res) => {
if(!req.cookies.jwt){
return res.render('error404.ejs');
}else{
const role = validation.getRole();
if(role != 'supreme'){
res.render('error404.ejs');
}else{
res.render('admin.ejs',{});
}
}
})
// discuss
app.get('/discuss/home',async (req,res) => {
req.role = req.cookies.jwt ? await validation.getRole(req.cookies.jwt) : 'anonymous';
await Post.aggregate().sample(4)
.then((data) => res.render('discuss/index.ejs',{jwt:req.cookies.jwt,data:data,role: req.role}))
.catch((err) => console.error(err))
});
app.get('/discuss/communities/create',(req,res)=>{
res.render('discuss/newCommunity.ejs');
})
// This route should be kept at the very bottom to prevent /anything // res.render("discuss/viewpost.ejs",{post,data})
app.get('/discuss/posts/:id',async (req,res)=>{
const user = await validation.getUser(req.cookies.jwt);
await Post.findById(req.params.id)
.populate("user")
.populate({
path: 'comments',
// Get users of the comments
populate: { path: 'user' }
})
.then(post => res.render("discuss/viewPost.ejs",{post,user}))
.catch(err => res.render('error404.ejs'));
});
// Communities with id
app.get('/discuss/communities/:id', async (req, res) => {
try {
const community = await Community.findById(req.params.id)
.populate('owner')
.populate('members')
.populate('posts');
if (!community) {
return res.render('error404.ejs');
}
const user = await validation.getUser(req.cookies.jwt);
res.render("discuss/viewCommunity.ejs", { community, user });
} catch (err) {
console.error(err);
return res.render("error404.ejs");
}
});
// Rendering 404 on unregistered routes
app.all('*',(req,res)=>{
res.render('error404.ejs');
})
// listing app on defined PORT
app.listen(port, () => {
console.log(`Server is up and running on port ${port}`);
});