-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.js
259 lines (220 loc) · 13.4 KB
/
backend.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//Import libs
const http = require('http');
const express = require('express');
const cookie_parser = require('cookie-parser');
const dotenv = require('dotenv').config();
const fileUpload = require('express-fileupload');
const MyMLH = require('./services/auth/mymlh.js');
const Mailer = require('./services/email/email.js');
const Database = require('./services/database/sqlite3.js'); //swap provider when needed
const InternalAuth = require('./services/auth/internal.js');
const SponsorsAuth = require('./services/auth/sponsors.js');
//Implement caching
const MyMLHUserCache = require('./services/caching/mlh-usercache.js');
//Implement API endpoints
const FormApiEndpoints = require('./services/apis/form.js');
const TeamsApiEndpoints = require('./services/apis/teams.js');
const JudgeApiEndpoints = require('./services/apis/judge.js');
const SponsorsApiEndpoints = require('./services/apis/sponsors.js');
const CheckinApiEndpoints = require('./services/apis/checkin.js');
const queryInfo = require('./services/dev/queryInfo');
(async function main() {
const app = express();
const router = express.Router();
app.use("/", require("body-parser").json());
app.use(cookie_parser());
app.use(fileUpload({
createParentPath: true,
limits: {
fileSize: 10000000 //10mb
},
abortOnLimit: true
}));
//Setup APIs
// Email setup
const key = require('./credentials.json');
const mailer = new Mailer(key);
// Database Connection
let db_connection = null;
if (Database.provider !== "sqlite")
db_connection = new Database(process.env.SQL_UNAME, process.env.SQL_PASS);
else
db_connection = new Database(); //sqlite connects to a file, no username and password needed
//Start MLH api
let mlh_auth = new MyMLH(process.env.MLH_APP_ID, process.env.MLH_APP_SECRET, process.env.JWT_SECRET);
let admin_auth = new InternalAuth(db_connection, process.env.JWT_SECRET);
let sponsors_auth = new SponsorsAuth(db_connection, process.env.JWT_SECRET);
//Set up cache
let mlh_cache = new MyMLHUserCache(mlh_auth);
await mlh_cache.build();
//Start APIs
let form_api = new FormApiEndpoints(db_connection, process.env.JWT_SECRET, mailer, mlh_auth);
let team_api = new TeamsApiEndpoints(db_connection, process.env.JWT_SECRET, mlh_cache);
let judge_api = new JudgeApiEndpoints(db_connection, process.env.JWT_SECRET, mlh_cache);
let sponsors_api = new SponsorsApiEndpoints(db_connection, process.env.JWT_SECRET, mlh_cache);
let checkin_api = new CheckinApiEndpoints(db_connection, process.env.JWT_SECRET, mlh_cache);
//Bind api calls
router.get("/oauth", async (req, res) => { mlh_auth.auth_callback(req, res) }); //Node has a hella weird callback system
router.get("/my-mlh-login", async (req, res) => { res.redirect("https://my.mlh.io/oauth/authorize?client_id=" + process.env.MLH_APP_ID +
"&redirect_uri=" + encodeURIComponent(process.env.MLH_REDIRECT_URI) +
"&response_type=code&scope=email+education+birthday+phone_number+demographics") }); //Auto-redirrect the user to mymlh
//Admin login API
router.get("/admin", async (req, res) => { admin_auth.auth_callback(req, res) });
router.get("/sponsors-admin", async (req, res) => { sponsors_auth.auth_callback(req, res) });
//Application CSV info
router.get("/judge/applications.csv", async (req, res, next) => { judge_api.judge_auth_middleware(req, res, next) },
async (req, res) => { judge_api.get_applications_csv(req, res) });
router.get("/judge/applications_invited.csv", async (req, res, next) => { judge_api.judge_auth_middleware(req, res, next) },
async (req, res) => { judge_api.get_invited_applications_csv(req, res) });
//TODO: probably add auth
router.post("/api/user-info", async (req, res) => { mlh_auth.get_user_data_endpoint(req, res) });
router.post("/api/form-data",
async (req, res, next) => { form_api.form_auth_middleware(req, res, next) },
async (req, res) => { form_api.form_data_endpoint(req, res) });
router.post("/api/form-update",
async (req, res, next) => { form_api.form_auth_middleware(req, res, next) },
async (req, res) => { form_api.form_delta_endpoint(req, res) });
router.post("/api/form-close",
async (req, res, next) => { form_api.form_auth_middleware(req, res, next) },
async (req, res) => { form_api.form_close_endpoint(req, res) });
router.post("/api/form-file-upload",
async (req, res, next) => { form_api.form_auth_middleware(req, res, next) },
async (req, res) => { form_api.form_upload_file(req, res) });
router.post("/api/ticket-file-upload",
async (req, res, next) => { form_api.form_auth_middleware(req, res, next) },
async (req, res) => { form_api.form_upload_file_ticket(req, res) });
router.post("/api/accept-invite",
async (req, res, next) => { form_api.form_auth_middleware(req, res, next) },
async (req, res) => { form_api.accept_invitation(req, res) });
router.post("/api/decline-invite",
async (req, res, next) => { form_api.form_auth_middleware(req, res, next) },
async (req, res) => { form_api.decline_invitation(req, res) });
router.post("/api/update-cv-file-id",
async (req, res, next) => { form_api.form_auth_middleware(req, res, next) },
async (req, res) => { form_api.update_cv_file_id(req, res) });
router.post("/api/update-ticket-file-id",
async (req, res, next) => { form_api.form_auth_middleware(req, res, next) },
async (req, res) => { form_api.update_ticket_file_id(req, res) });
router.post("/api/ticket-data",
async (req, res, next) => { form_api.form_auth_middleware(req, res, next) },
async (req, res) => { form_api.get_ticket_data(req, res) });
router.post("/api/set-iban",
async (req, res, next) => { form_api.form_auth_middleware(req, res, next) },
async (req, res) => { form_api.set_iban(req, res) });
router.post("/api/get-iban",
async (req, res, next) => { form_api.form_auth_middleware(req, res, next) },
async (req, res) => { form_api.get_iban(req, res) });
router.post("/api/team-create",
async (req, res, next) => { team_api.team_auth_middleware(req, res, next) },
async (req, res) => { team_api.team_create_endpoint(req, res) });
router.post("/api/team-join",
async (req, res, next) => { team_api.team_auth_middleware(req, res, next) },
async (req, res) => { team_api.team_join_endpoint(req, res) });
router.post("/api/team-leave",
async (req, res, next) => { team_api.team_auth_middleware(req, res, next) },
async (req, res) => { team_api.team_leave_endpoint(req, res) });
router.post("/api/team-kick",
async (req, res, next) => { team_api.team_auth_middleware(req, res, next) },
async (req, res) => { team_api.team_kick_endpoint(req, res) });
router.post("/api/team-info",
async (req, res, next) => { team_api.team_auth_middleware(req, res, next) },
async (req, res) => { team_api.team_info_endpoint(req, res) });
//It's login and registration so there is no need for auth middleware
router.post("/api/auth-login",
async (req, res) => { admin_auth.login_endpoint(req, res) });
router.post("/api/auth-register",
async (req, res) => { admin_auth.register_endpoint(req, res) });
router.post("/api/sponsors-auth-login",
async (req, res) => { sponsors_auth.login_endpoint(req, res) });
router.post("/api/sponsors-auth-register",
async (req, res) => { sponsors_auth.register_endpoint(req, res) });
router.post("/api/judge-vote",
async (req, res, next) => { judge_api.judge_auth_middleware(req, res, next) },
async (req, res) => { judge_api.cast_vote_endpoint(req, res) });
router.post("/api/judge-application",
async (req, res, next) => { judge_api.judge_auth_middleware(req, res, next) },
async (req, res) => { judge_api.get_application_endpoint(req, res) });
router.post("/api/judge-application-detail",
async (req, res, next) => { judge_api.judge_auth_middleware(req, res, next) },
async (req, res) => { judge_api.get_application_detail_endpoint(req, res) });
router.post("/api/judge-scoreboard",
async (req, res, next) => { judge_api.judge_auth_middleware(req, res, next) },
async (req, res) => { judge_api.get_judge_scoreboard_endpoint(req, res) });
router.post("/api/judge-application-scoreboard",
async (req, res, next) => { judge_api.judge_auth_middleware(req, res, next) },
async (req, res) => { judge_api.get_applications_scoreboard_endpoint(req, res) });
router.post("/api/judge-invite",
async (req, res, next) => { judge_api.judge_auth_middleware(req, res, next) },
async (req, res) => { judge_api.invite_application_endpoint(req, res) });
router.post("/api/judge-reject",
async (req, res, next) => { judge_api.judge_auth_middleware(req, res, next) },
async (req, res) => { judge_api.reject_application_endpoint(req, res) });
router.post("/api/judge-get-iban",
async (req, res, next) => { judge_api.judge_auth_middleware(req, res, next) },
async (req, res) => { judge_api.get_iban_judge(req, res) });
router.post("/api/checkin-info",
async (req, res, next) => { checkin_api.user_auth_middleware(req, res, next) },
async (req, res) => { checkin_api.create_checkin_info_endpoint(req, res) });
router.post("/api/checkin-user",
async (req, res, next) => { checkin_api.judge_auth_middleware(req, res, next) },
async (req, res) => { checkin_api.get_checkin_user(req, res) });
router.post("/api/checkin-data",
async (req, res, next) => { checkin_api.judge_auth_middleware(req, res, next) },
async (req, res) => { checkin_api.get_checkin_data(req, res) });
router.post("/api/checkin-complete",
async (req, res, next) => { checkin_api.judge_auth_middleware(req, res, next) },
async (req, res) => { checkin_api.check_in_user(req, res) });
router.post("/api/checkin-table-uid",
async (req, res, next) => { checkin_api.judge_auth_middleware(req, res, next) },
async (req, res) => { checkin_api.get_table_code_from_uid(req, res) });
router.post("/api/checkin-table-uid-user",
async (req, res, next) => { checkin_api.user_auth_middleware(req, res, next) },
async (req, res) => { checkin_api.get_table_code_from_uid(req, res) });
router.post("/api/checkin-table-team",
async (req, res, next) => { checkin_api.judge_auth_middleware(req, res, next) },
async (req, res) => { checkin_api.get_table_code_from_team(req, res) });
router.post("/api/checkin-table-team-user",
async (req, res, next) => { checkin_api.user_auth_middleware(req, res, next) },
async (req, res) => { checkin_api.get_table_code_from_team(req, res) });
router.post("/api/checkin-table",
async (req, res, next) => { checkin_api.judge_auth_middleware(req, res, next) },
async (req, res) => { checkin_api.get_manual_checkin_data(req, res) });
router.post("/api/checkin-checked-in",
async (req, res, next) => { checkin_api.judge_auth_middleware(req, res, next) },
async (req, res) => { checkin_api.get_checked_data(req, res) });
router.post("/api/sponsors-applications",
async (req, res, next) => { sponsors_api.sponsors_auth_middleware(req, res, next) },
async (req, res) => { sponsors_api.get_applications_endpoint(req, res) });
router.post("/api/sponsors-application-detail",
async (req, res, next) => { sponsors_api.sponsors_auth_middleware(req, res, next) },
async (req, res) => { sponsors_api.get_application_detail_endpoint(req, res) });
router.post("/api/checkin-info",
async (req, res, next) => { checkin_api.user_auth_middleware(req, res, next) },
async (req, res) => { checkin_api.create_checkin_info_endpoint(req, res) });
router.post("/api/sponsors-applications",
async (req, res, next) => { sponsors_api.sponsors_auth_middleware(req, res, next) },
async (req, res) => { sponsors_api.get_applications_endpoint(req, res) });
router.post("/api/sponsors-application-detail",
async (req, res, next) => { sponsors_api.sponsors_auth_middleware(req, res, next) },
async (req, res) => { sponsors_api.get_application_detail_endpoint(req, res) });
//Bind static content
app.use("/", express.static("./static"));
app.use("/", router);
//CVs
app.use('/judge/cvs/', async (req, res, next) => { judge_api.judge_auth_middleware(req, res, next) });
app.use('/judge/cvs/', express.static("./uploads/cvs/"));
//CVs
app.use('/sponsors/cvs/', async (req, res, next) => { sponsors_api.sponsors_auth_middleware(req, res, next) });
app.use('/sponsors/cvs/', express.static("./uploads/cvs/"));
//404 handler
app.use(function(req, res) {
res.status(404);
res.redirect("/404.html");
});
//Start the HTTP server
const server = http.createServer(app);
const port = process.env.PORT;
server.listen(port, "127.0.0.1");
console.log("Server started in mode: " + process.env.NODE_ENV);
// queryInfo(db_connection)
})();