import * as config from "./config"; import * as requests from "./requests"; import { QueryResult } from "pg"; import express from "express"; import session from "express-session"; import pgSessionStore from "connect-pg-simple"; import db from "./db"; import errorHandler from "./errors"; const app = express(); app.use(express.static('public')); app.use(express.urlencoded({extended: false})); app.use(session({ secret: config.sessionSecret, saveUninitialized: false, resave: false, store: new (pgSessionStore(session))() })); app.get("/api/getRequests", async (request, response) => { var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 ); requests.getRequests(requestCount).then((val: QueryResult) => response.send(val)) .catch((e: any) => errorHandler(request,response,e)); }); app.get("/api/getAllRequests", async (request, response) => { var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 ); requests.getAllRequests(requestCount).then((val: QueryResult) => response.send(val)) .catch((e: any) => errorHandler(request,response,e)); }); app.post("/api/addRequest", async (request, response) => { response.type('text/plain'); if (!request.body.url) { response.status(400); response.send("Missing url"); return } if (!request.body.requester) { response.status(400); response.send("Missing requester"); return } var url = request.body.url as string; var requester = request.body.requester as string; requests.addRequest(url,requester).then((val: [number,string]) => { response.status(val[0]); response.send(val[1]); }) .catch((e: any) => errorHandler(request,response,e)); }); app.post("/api/updateRequestState", async (request, response) => { response.type('text/plain'); if (!request.body.url) { response.status(400); response.send("Missing url"); return } if (!request.body.state) { response.status(400); response.send("Missing scoreDiff"); return } var url = request.body.url as string; var state = request.body.state as string; requests.updateRequestState(url,state).then((val: [number,string]) => { response.status(val[0]); response.send(val[1]); }) .catch((e: any) => errorHandler(request,response,e)); }); app.post("/api/updateRequestScore", async (request, response) => { response.type('text/plain'); if (!request.body.url) { response.status(400); response.send("Missing url"); return } if (!request.body.scoreDiff) { response.status(400); response.send("Missing scoreDiff"); return } var url = request.body.url as string; var scoreDiff = parseInt(request.body.scoreDiff as string, 10); requests.updateRequestScore(url,scoreDiff).then((val: string) => response.send(val)) .catch((e: any) => errorHandler(request,response,e)); }); app.post("/api/deleteRequest", async (request, response) => { response.type('text/plain'); if (!request.body.url) { response.status(400); response.send("Missing url"); return } var url = request.body.url as string; requests.deleteRequest(url).then((val: string) => response.send(val)) .catch((e: any) => errorHandler(request,response,e)); }); const server = app.listen(config.port, () => { console.log(`Listening on port ${config.port}`); });