forked from ggwadera/c0d3-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
156 lines (126 loc) · 3.81 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
const express = require('express');
const session = require('express-session');
const multer = require('multer');
const utils = require('./utils');
const geolocation = require('./js5/1/geolocation');
const commands = require('./js5/2/commands');
const memegen = require('./js5/3/memegen');
const assets = require('./js5/4/assets');
const chatroom = require('./js5/5/chatroom');
const auth = require('./js5/6/jwt_auth');
const ocr = require('./js5/7/ocr');
const webcam = require('./js5/8/webcam');
const memechat = require('./js5/9/memechat');
const app = express();
const upload = multer({ dest: 'public/files/' });
const port = process.env.PORT || 8123;
app.use(session({
secret: 'keyboard cat', resave: false, saveUninitialized: false, cookie: { maxAge: 1000 * 60 * 60, sameSite: true },
})); // cookie
app.use(express.static('public')); // generate static link to files in /public
app.use(express.json({ limit: '10mb' })); // parse json body in the requests
app.use(express.urlencoded({ extended: true }));
utils.createFolder();
utils.cleanFolder();
// 1. IP GEOLOCATION
app.get('/visitors', (req, res) => {
geolocation.visitorsRoute(req, res);
});
app.get('/api/visitors', (req, res) => {
geolocation.findAll(req, res);
});
// 2. COMMANDS
app.post('/commands', (req, res) => {
commands.runCommand(req, res);
});
// 3. Meme Gen
app.get('/memegen/api/:text', (req, res) => {
memegen.genMeme(req, res);
});
// 4. Asset Creation
app.get('/api/files', (req, res) => {
assets.findAll(req, res);
});
app.get('/api/files/:filename', (req, res) => {
assets.findFile(req, res);
});
app.post('/api/files', (req, res) => {
assets.createFile(req, res);
});
assets.initialize();
// 5. Chatroom using JWT Auth
app.get('/chatroom/:room?', (req, res) => {
res.sendFile(`${__dirname}/public/js5/5/chatroom.html`);
});
app.use('/chatroom/api/*', (req, res, next) => {
chatroom.getUserMiddleware(req, res, next);
});
app.get('/chatroom/api/session', (req, res) => {
chatroom.getSession(req, res);
});
app.get('/chatroom/api/:room/messages', (req, res) => {
chatroom.getMessages(req, res);
});
app.post('/chatroom/api/:room/messages', (req, res) => {
chatroom.postMessage(req, res);
});
// 6. Authentication
app.get('/auth', (req, res) => {
res.sendFile(`${__dirname}/public/js5/6/auth.html`);
});
app.post('/auth/api/users', (req, res) => {
auth.postUser(req, res);
});
app.post('/auth/api/session', (req, res) => {
auth.postSession(req, res);
});
app.get('/auth/api/session', (req, res) => {
auth.getSession(req, res);
});
// 7. Image Text Extraction
app.post('/ocr/files', upload.single('userFile'), (req, res) => {
ocr.postJob(req, res);
});
app.get('/ocr/job/:jobid', (req, res) => {
res.sendFile(`${__dirname}/public/js5/7/ocr_job.html`);
});
app.get('/ocr/api/job/:jobid', (req, res) => {
ocr.getJob(req, res);
});
ocr.deleteOldFiles();
// 8. Selfie Queen
app.post('/webcam/files', (req, res) => {
webcam.postImage(req, res);
});
webcam.deleteOldFiles();
// 9. MemeChat
app.get('/memechat/', (req, res) => {
res.sendFile(`${__dirname}/public/js5/9/login.html`);
});
app.get('/memechat/room', (req, res) => {
res.sendFile(`${__dirname}/public/js5/9/room.html`);
});
app.get('/memechat/api/session', (req, res) => {
if (!req.session.username) {
return res.status(403).json({ error: 'Invalid session' });
}
return res.sendStatus(200);
});
app.post('/memechat/api/session', (req, res) => {
if (!req.session.username) {
req.session.username = req.body.username;
}
res.status(201).redirect('/memechat/room');
});
app.get('/memechat/api/room/', (req, res) => {
memechat.getUsers(req, res);
});
app.post('/memechat/api/files', (req, res) => {
memechat.postImage(req, res);
});
// Start server
app.listen(port, () => {
if (!process.env.PORT) {
console.log(`Server running at http://localhost:${port}`);
}
});