aboutsummaryrefslogtreecommitdiff
path: root/src/server.js
blob: b92d7d41d238e49dad7b0b18aac904a3957ae6d7 (plain)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const request = require('request');
const crypto = require('crypto');
const uuidv4 = require('uuid/v4');
const path = require('path');

const Op = require('sequelize').Op;

const multer = require('multer');
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, __dirname+'/uploads/')
    },
    filename: function (req, file, cb) {
        var ext = "";
        if (file.originalname.includes(".")) {
            ext = "." + file.originalname.split(".")[1];
        }
        return cb(null, 'img-' + Date.now() + ext)
    }
})
var upload = multer({ storage: storage })

const server = express();
server.use(cookieParser())
server.use(bodyParser.urlencoded({ extended: true }));

async function addImagesAndTagsToPosts(models, posts) {
    for (const post of posts) {
        const images = await models.pictures.findAll({ attributes: ["source"], where: { postId: post.id } }).map(x => x.source);
        post.images = images;
        const tags = await models.tags.findAll({ attributes: ["text"], where: { postId: post.id } }).map(x => x.text);
        post.tags = tags;
    }
}

function listen(port) {
    server.listen(port, () => console.info(`Listening on port ${port}!`));
}

function hashWithSalt(password, salt){
    var hash = crypto.createHmac('sha512', salt);
    hash.update(password);
    return hash.digest("base64");
};

function setUpRoutes(models, jwtFunctions, database) {
    // Authentication routine
    server.use(function (req, res, next) {
        if (req.path.toLowerCase().startsWith("/admin")) {
            let cookie = req.cookies.authorization
            if (!cookie) {
                console.debug("Redirecting to login - no cookie")
                res.redirect('/login');
                return;
            }
            try {
                const decryptedUserId = jwtFunctions.verify(cookie);
                models.users.findOne({ where: { username: decryptedUserId } }).then((user, error) => {
                    if (user) {
                        res.locals.user = user.get({ plain: true });
                    } else {
                        console.debug("Redirecting to login - invalid cookie")
                        res.redirect('/login');
                        return;
                    }
                });
            } catch (e) {
                res.status(400).send(e.message);
            }
        }
        next();
    })

    // Route logging
    server.use(function (req, res, next) {
        let cookie = req.cookies.session;
        if (!cookie) {
            cookie = uuidv4();
            res.cookie('session', cookie, { expires: new Date(Date.now() + (1000 * 60 * 60)) });
        }
        models.requests.create({
            createdAt: new Date(), session: cookie, method: req.method, url: req.originalUrl
        });
        next()
    })

    server.get('/', (req, res) => res.sendFile(__dirname + "/html/index.html"))
    server.get('/index', (req, res) => res.sendFile(__dirname + "/html/index.html"))
    server.get('/admin', (req, res) => res.sendFile(__dirname + "/html/admin.html"));
    server.get('/login', (req, res) => res.sendFile(__dirname + "/html/login.html"))
    server.get('/email', (req, res) => res.sendFile(__dirname + "/html/email.html"))
    server.get('/bread', (req, res) => res.sendFile(__dirname + "/html/bread.html"));
    server.get('/blog', (req, res) => res.sendFile(__dirname + "/html/blog.html"));
    server.get('/tags', (req, res) => res.sendFile(__dirname + "/html/tags.html"));
    server.get('/feed', (req, res) => res.sendFile(__dirname + "/html/feed.html"));
    server.get('/essay', (req, res) => res.sendFile(__dirname + "/html/essay.html"));
    server.get('/misc', (req, res) => res.sendFile(__dirname + "/html/misc.html"));
    server.get('/word-square', (req, res) => res.sendFile(__dirname + "/html/word-square.html"));
    server.get('/chess', (req, res) => res.sendFile(__dirname + "/html/chess.html"));
    server.get('/admin/chess', async (req, res, next) => res.sendFile(__dirname + "/html/chess.html"));
    server.get('/projects', (req, res) => res.sendFile(__dirname + "/html/projects.html"));
    server.get('/wordsquares/best', async (req, res, next) => {
        var best = await database.query("select words, name from wordsquares where best = 1", { type: database.QueryTypes.SELECT })
        res.status(200).send({ best: best });
    })
    server.get('/admin/chess/games', async (req, res, next) => {
        const { name } = req.params;
        var game = await models.chessgames.findOne({where: {
            turn: {
                [Op.ne]: {$col: 'userside'}
            }
        
            // database.where(
            //     database.col('userside'),
            //     database.col('turn')
            //   )
        }})
        // var game = await database.query("select * from chessgames where userside != turn", { type: database.QueryTypes.SELECT })
        res.status(200).send({game:game});
    })
    server.get('/chess/:name', async (req, res, next) => {
        const { name } = req.params;
        var game = await models.chessgames.findOne({where: {name: name}})
        //var game = await database.query("select * from chessgames where name = '"+name+"'", { type: database.QueryTypes.SELECT })
        res.status(200).send({game:game});
    })
    server.get('/admin/emails', async (req, res, next) => {
        var emails = await models.emails.findAll();
        res.status(200).send(emails);
    })
    server.get('/admin/stats', async (req, res, next) => {
        try {
            var sessionResult = await database.query("SELECT session, count(id) as c FROM requests GROUP BY session HAVING c > 1", { type: database.QueryTypes.SELECT })
            var total = await database.query("select count(distinct session) as t FROM requests", { type: database.QueryTypes.SELECT })
            var urlResult = await database.query("SELECT method, url, count(id) as c FROM requests GROUP BY method, url", { type: database.QueryTypes.SELECT })
            var logResult = await database.query("SELECT createdAt, session, method, url FROM requests order by createdAt desc limit 15", { type: database.QueryTypes.SELECT })
            res.status(200).send({ total: total[0].t, session: sessionResult, url: urlResult, log: logResult });
            next();
        } catch (e) {
            res.status(400).send(e.message);
        }
    })
    server.get('/post/:type/:id', async (req, res, next) => {
        res.sendFile(__dirname + "/html/post-single.html");
    })
    server.get('/tags/:name', async (req, res, next) => {
        console.log("TAGS/NAME");
        try {
            const { name } = req.params;
            const postsWithTag = await models.tags.findAll({ attributes: ["postId"], where: { text: name } })
                .map(function (x) {
                    return { id: x.postId }
                });
            var posts = await models.posts.findAll({
                where: { [Op.or]: postsWithTag }, order: [['createdAt', 'DESC']]
            });
            posts = posts.map(x => x.get({ plain: true }));
            await addImagesAndTagsToPosts(models, posts)
            console.log(posts);
            res.status(200).send(posts);
            next();
        } catch (e) {
            console.error(e);
            res.status(400).send(e.message);
        }
    })
    server.get('/posts/:type', async (req, res, next) => {
        try {
            const { type } = req.params;
            var posts = await models.posts.findAll({
                where: { type: type }, order: [['createdAt', 'DESC']]
            });
            posts = posts.map(x => x.get({ plain: true }));
            await addImagesAndTagsToPosts(models, posts)
            res.status(200).send(posts);
            next();
        } catch (e) {
            res.status(400).send(e.message);
        }
    })
    server.get('/posts/:type/:id', async (req, res, next) => {
        try {
            const { type, id } = req.params;
            var posts = await models.posts.findAll({
                where: { 
                    type: type,
                    id: id
                }, order: [['createdAt', 'DESC']]
            });
            posts = posts.map(x => x.get({ plain: true }));
            await addImagesAndTagsToPosts(models, posts)
            res.status(200).send(posts);
            next();
        } catch (e) {
            res.status(400).send(e.message);
        }
    })
    server.post('/posts', upload.array('images'), async (req, res, next) => {
        try {
            const type = req.body.type
            const newPost = await models.posts.create(req.body);
            req.files.forEach(async (file) => {	
                await models.pictures.create({ "source": "uploads/" + file.filename, "postId": newPost.id });
                console.log("uploaded ", file.path);
            })
            req.body.tags.split(" ").forEach(async (tag) => {
                await models.tags.create({ "text": tag, "postId": newPost.id });
            })
            console.log(newPost);
            res.redirect(`/${type}`);
            next();
        } catch (e) {
            res.status(400).send(e.message);
        }
    })
    server.post('/login', async (req, res, next) => {
        const user = await models.users.findOne({ where: { username: req.body.username} })
        const hash = hashWithSalt(req.body.password, user.salt)
        if (user.password == hash) {
            const token = jwtFunctions.sign(user.username);
            res.cookie('authorization', token, { expires: new Date(Date.now() + (1000 * 60 * 60)) });
            console.debug("Redirecting to admin - logged in")
            res.redirect('/admin');
        } else {
            console.debug("Redirecting to login - invalid login")
            res.redirect('/login');
        }
    })
    server.post('/email', async (req, res, next) => {
        const name = req.body.name;
        const email = req.body.email;
        if (name && email) {
            models.emails.create({"name": name, "address": email})
            res.redirect('/email#success');
        } else {
            console.debug("Error with email submission")
        }
    })
    server.post('/wordsquares', async (req, res, next) => {
        const words = req.body.words;
        const name = req.body.name;
        if (name && words) {
            models.wordsquares.create({"name": name, "words": words, "best": false})
            res.redirect('/wordsquare#success');
        } else {
            console.debug("Error with wordsquare submission")
        }
    })
    server.post('/chess', async (req, res, next) => {
        const game = req.body;
        if (game) {
            models.chessgames.findOne({where: {name: game.name}})
            .then(obj => {
                if(obj){
                    obj.update(game)
                } else {
                    models.chessgames.create(game)
                }
            })
        } else {
            console.debug("Error with chess submission")
        }
        res.status(200).send(game);
    })


    server.get('/favicon.ico', (req, res) => res.sendFile(__dirname + "/icon/favicon.ico"))
    server.get('/css/:id', (req, res) => {
        res.sendFile(__dirname + "/css/" + req.params.id);
    });
    server.get('/uploads/:id', (req, res) => {
        res.sendFile(__dirname + "/uploads/" + req.params.id);
    });
    server.get('/essay/:id', (req, res) => {
        res.sendFile(__dirname + "/html/essay/" + req.params.id);
    });
    server.get('/js/:id', (req, res) => {
        res.sendFile(__dirname + "/js/" + req.params.id);
    });

}

module.exports = {
    listen,
    setUpRoutes
};