2015-07-20 09:41:58 +00:00
|
|
|
"use strict";
|
|
|
|
|
2015-12-28 13:21:35 +00:00
|
|
|
const path = require("path");
|
|
|
|
const winston = require("winston");
|
|
|
|
const config = require("./config.js");
|
2016-04-01 13:48:12 +00:00
|
|
|
const GatherPool = require("../lib/gather/gather_pool");
|
2015-12-28 13:21:35 +00:00
|
|
|
const mongoose = require("mongoose");
|
|
|
|
const Message = mongoose.model("Message");
|
|
|
|
const cors = require("cors");
|
2015-07-20 09:41:58 +00:00
|
|
|
|
2015-08-10 23:56:53 +00:00
|
|
|
module.exports = app => {
|
2015-09-17 12:48:10 +00:00
|
|
|
app.use(cors());
|
|
|
|
|
2015-08-18 09:56:35 +00:00
|
|
|
app.get("/", (request, response, next) => {
|
2015-12-29 02:20:27 +00:00
|
|
|
response.render("gather.hbs", {
|
2015-09-14 22:33:49 +00:00
|
|
|
redirect: config.ensl_url,
|
2015-09-20 00:43:09 +00:00
|
|
|
bot_url: config.steam_bot_link,
|
|
|
|
rules_url: config.ensl_rules_url
|
2015-08-18 10:53:28 +00:00
|
|
|
});
|
2015-07-20 09:41:58 +00:00
|
|
|
});
|
|
|
|
|
2015-08-18 09:56:35 +00:00
|
|
|
app.get("/redirect", (request, response, next) => {
|
|
|
|
response.render("redirect.hbs", {
|
|
|
|
redirect: config.ensl_url
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-09-17 12:48:10 +00:00
|
|
|
app.get("/gathers/current", (request, response) => {
|
2016-04-01 13:48:12 +00:00
|
|
|
response.status(200).json(GatherPool.get("classic").current.toJson());
|
2015-09-17 12:48:10 +00:00
|
|
|
});
|
|
|
|
|
2015-12-29 02:20:27 +00:00
|
|
|
app.get("/api/messages", (request, response) => {
|
|
|
|
const page = parseInt(request.query.page, 10) || 0;
|
2015-12-29 02:23:56 +00:00
|
|
|
|
|
|
|
let limit = parseInt(request.query.limit, 10) || 250;
|
|
|
|
if (limit > 250 || limit < 1) limit = 250;
|
|
|
|
|
2015-12-29 02:20:27 +00:00
|
|
|
let query = {};
|
|
|
|
let searchTerm = request.query.query;
|
|
|
|
if (searchTerm) {
|
|
|
|
query = {
|
|
|
|
$text: {
|
|
|
|
$search: searchTerm
|
2015-12-28 13:21:35 +00:00
|
|
|
}
|
2015-12-29 02:20:27 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
Message
|
|
|
|
.find(query)
|
|
|
|
.limit(limit)
|
|
|
|
.skip(page * limit)
|
|
|
|
.sort({createdAt: -1})
|
|
|
|
.exec((error, messages) => {
|
|
|
|
if (error) {
|
|
|
|
winston.error(error);
|
|
|
|
return response.status(500).json({
|
|
|
|
message: "An error occurred",
|
|
|
|
error: JSON.stringify(error)
|
2015-12-28 13:21:35 +00:00
|
|
|
});
|
2015-12-29 02:20:27 +00:00
|
|
|
}
|
|
|
|
response.status(200).json({
|
|
|
|
messages: messages,
|
|
|
|
page: page,
|
|
|
|
limit: limit
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get("/messages", (request, response) => {
|
|
|
|
response.render("messages.hbs");
|
2015-12-27 19:42:32 +00:00
|
|
|
});
|
|
|
|
|
2015-08-10 23:56:53 +00:00
|
|
|
app.get("*", (request, response) => {
|
2015-07-20 09:41:58 +00:00
|
|
|
response.status(404).render("404.hbs");
|
|
|
|
});
|
2015-08-18 09:56:35 +00:00
|
|
|
|
|
|
|
app.use(function (error, request, response, next) {
|
|
|
|
winston.error(error);
|
|
|
|
return response.status(500).render("500.hbs");
|
|
|
|
});
|
2015-07-20 09:41:58 +00:00
|
|
|
};
|