-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.js
44 lines (35 loc) · 1.49 KB
/
server.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
// server.js
// where your node app starts
// init project
const express = require('express');
const app = express();
const hbars = require('express-handlebars');
const bot = require("./bot/index");
const db = require('./bot/db.js');
const imageUtils = require('./utils/image');
const probeSize = require('probe-image-size');
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
app.engine('handlebars', hbars({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.enable('trust proxy')
// http://expressjs.com/en/starter/basic-routing.html
app.get('/', function(request, response) {
response.sendFile(__dirname + '/views/index.html');
});
app.get('/recent', async function(req, res) {
if (req.ip != process.env.ADMIN_IP) return res.status(404).send({message: 'error'});
let images = await db.images.find({}).sort({createdTimestamp: -1}).limit(500);
let servers = images.reduce((servers, im) => {
servers[im.guildId] = servers[im.guildId] || {images: [], count: 0, name: (bot.client.guilds.get(im.guildId) || {name: im.guildId}).name};
if(servers[im.guildId].images.length < 10) servers[im.guildId].images.push(im);
servers[im.guildId].count++;
im.urls = imageUtils.getResizedUrls(im, im);
return servers;
}, {});
res.render('recent', {servers: servers});
})
// listen for requests :)
const listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port);
});