-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
187 lines (170 loc) · 5.06 KB
/
index.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
require("dotenv").config();
const express = require("express");
const app = express();
const cors = require("cors");
const bodyParser = require("body-parser");
const session = require("express-session");
const mongoose = require("mongoose");
const MongoStore = require("connect-mongo")(session);
const authMiddleware = require("./middleware/middleware_auth");
const authRoutes = require("./routes/routes_auth");
const leagueRoutes = require("./routes/routes_leagues");
const matchRoutes = require("./routes/routes_matches");
const playerRoutes = require("./routes/routes_players");
const scheduleRoutes = require("./routes/routes_schedules");
const tagRoutes = require("./routes/routes_tags");
const teamRoutes = require("./routes/routes_teams");
const userRoutes = require("./routes/routes_users");
const MAX_AGE = 1000 * 60 * 60;
const IN_PROD = process.env.NODE_ENV === "production";
const config = {
origin: "http://localhost:3000",
credentials: true
};
app.use(cors(config));
app.use(
session({
cookie: {
maxAge: MAX_AGE,
sameSite: true,
secure: IN_PROD
},
name: process.env.SESS_NAME,
resave: false,
saveUninitialized: false,
secret: process.env.SESSION_SECRET,
store: new MongoStore({ mongooseConnection: mongoose.connection })
})
);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", (req, res) => res.send("Let's Play Kickball"));
const apiVersion = "/api/ver0001";
app.get(`${apiVersion}/leagues`, leagueRoutes);
app.post(`${apiVersion}/leagues`, authMiddleware.loginRequired, leagueRoutes);
app.get(`${apiVersion}/leagues/:leagueId`, leagueRoutes);
app.patch(
`${apiVersion}/leagues/:leagueId`,
authMiddleware.loginRequired,
leagueRoutes
);
app.put(
`${apiVersion}/leagues/:leagueId`,
authMiddleware.loginRequired,
leagueRoutes
);
app.delete(
`${apiVersion}/leagues/:leagueId`,
authMiddleware.loginRequired,
leagueRoutes
);
app.use(`${apiVersion}/leagues`, leagueRoutes);
app.get(`${apiVersion}/matches`, matchRoutes);
app.post(`${apiVersion}/matches`, authMiddleware.loginRequired, matchRoutes);
app.get(`${apiVersion}/matches/:matchId`, matchRoutes);
app.patch(
`${apiVersion}/matches/:matchId`,
authMiddleware.loginRequired,
matchRoutes
);
app.put(
`${apiVersion}/matches/:matchId`,
authMiddleware.loginRequired,
matchRoutes
);
app.delete(`${apiVersion}/matches/:matchId`, matchRoutes);
app.use(`${apiVersion}/matches`, matchRoutes);
app.get(`${apiVersion}/players`, playerRoutes);
app.post(`${apiVersion}/players`, authMiddleware.loginRequired, playerRoutes);
app.get(`${apiVersion}/players/:playerId`, playerRoutes);
app.patch(
`${apiVersion}/players/:playerId`,
authMiddleware.loginRequired,
playerRoutes
);
app.put(
`${apiVersion}/players/:playerId`,
authMiddleware.loginRequired,
playerRoutes
);
app.delete(
`${apiVersion}/players/:playerId`,
authMiddleware.loginRequired,
playerRoutes
);
app.use(`${apiVersion}/players`, playerRoutes);
app.get(`${apiVersion}/schedules`, scheduleRoutes);
app.post(
`${apiVersion}/schedules`,
authMiddleware.loginRequired,
scheduleRoutes
);
app.get(`${apiVersion}/schedules/:scheduleId`, scheduleRoutes);
app.patch(
`${apiVersion}/schedules/:scheduleId`,
authMiddleware.loginRequired,
scheduleRoutes
);
app.put(
`${apiVersion}/schedules/:scheduleId`,
authMiddleware.loginRequired,
scheduleRoutes
);
app.delete(
`${apiVersion}/schedules/:scheduleId`,
authMiddleware.loginRequired,
scheduleRoutes
);
app.use(`${apiVersion}/schedules`, scheduleRoutes);
app.get(`${apiVersion}/tags`, tagRoutes);
app.post(`${apiVersion}/tags`, authMiddleware.loginRequired, tagRoutes);
app.get(`${apiVersion}/tags/:tagId`, tagRoutes);
app.patch(`${apiVersion}/tags/:tagId`, authMiddleware.loginRequired, tagRoutes);
app.put(`${apiVersion}/tags/:tagId`, authMiddleware.loginRequired, tagRoutes);
app.delete(
`${apiVersion}/tags/:tagId`,
authMiddleware.loginRequired,
tagRoutes
);
app.use(`${apiVersion}/tags`, tagRoutes);
app.get(`${apiVersion}/teams`, teamRoutes);
app.post(`${apiVersion}/teams`, authMiddleware.loginRequired, teamRoutes);
app.get(`${apiVersion}/teams/:teamId`, teamRoutes);
app.patch(
`${apiVersion}/teams/:teamId`,
authMiddleware.loginRequired,
teamRoutes
);
app.put(
`${apiVersion}/teams/:teamId`,
authMiddleware.loginRequired,
teamRoutes
);
app.delete(
`${apiVersion}/teams/:teamId`,
authMiddleware.loginRequired,
teamRoutes
);
app.use(`${apiVersion}/teams`, teamRoutes);
app.get(`${apiVersion}/users`, userRoutes);
app.post(`${apiVersion}/users`, authMiddleware.loginRequired, userRoutes);
app.get(`${apiVersion}/users/:userId`, userRoutes);
app.patch(
`${apiVersion}/users/:userId`,
authMiddleware.loginRequired,
userRoutes
);
app.put(
`${apiVersion}/users/:userId`,
authMiddleware.loginRequired,
userRoutes
);
app.delete(
`${apiVersion}/users/:userId`,
authMiddleware.loginRequired,
userRoutes
);
app.use(`${apiVersion}/users`, userRoutes);
app.use(`${apiVersion}/auth`, authRoutes);
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => console.log(`app is listening on PORT ${PORT}`));