You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
3.3 KiB
140 lines
3.3 KiB
|
|
//import {Request, Response} from "express";
|
|
const express = require('express');
|
|
const fs = require('fs');
|
|
const app = express();
|
|
const bodyParser = require('body-parser');
|
|
const cookieParser = require('cookie-parser');
|
|
const jwt = require('jsonwebtoken');
|
|
const expressJwt = require('express-jwt');
|
|
|
|
const AdvancableBuffer = require('./backend/utilities/AdvancableBuffer.js');
|
|
const gbxHeader = require('./backend/trackmania-replays/gbx-header.js');
|
|
|
|
// handling CORS
|
|
app.use((req, res, next) => {
|
|
res.header("Access-Control-Allow-Origin",
|
|
"http://localhost:4200");
|
|
res.header("Access-Control-Allow-Headers",
|
|
"Origin, X-Requested-With, Content-Type, Accept");
|
|
next();
|
|
});
|
|
|
|
app.use(bodyParser.json());
|
|
app.route('/api/login').post(loginRoute);
|
|
|
|
const RSA_PRIVATE_KEY = fs.readFileSync('./private.key');
|
|
const RSA_PUBLIC_KEY = fs.readFileSync('./public.key');
|
|
|
|
const checkIfAuthenticated = expressJwt({
|
|
secret: RSA_PUBLIC_KEY
|
|
});
|
|
|
|
/*export */function loginRoute(req, res) {
|
|
|
|
const email = req.body.email
|
|
const password = req.body.password;
|
|
|
|
if (validateEmailAndPassword(email, password))
|
|
{
|
|
const userId = findUserIdForEmail(email);
|
|
|
|
const jwtBearerToken = jwt.sign({}, RSA_PRIVATE_KEY, {
|
|
algorithm: 'RS256',
|
|
expiresIn: 120,
|
|
subject: userId
|
|
});
|
|
|
|
res.status(200).json({
|
|
idToken: jwtBearerToken,
|
|
expiresIn: 120
|
|
});
|
|
}
|
|
else {
|
|
// send status 401 Unauthorized
|
|
res.sendStatus(401);
|
|
}
|
|
}
|
|
|
|
//app.route('/api/session/create').get(checkIfAuthenticated, )
|
|
|
|
const upload = require('./backend/routes/upload-replay');
|
|
|
|
app.get('/api/seasons', (req, res) => {
|
|
seasons = [{
|
|
id: 1,
|
|
seasonName: "Season 1",
|
|
seasonTag: "Post Winter Blues",
|
|
seasonCardImage: "",
|
|
seasonHeaderImage: "",
|
|
seasonStartDate: "",
|
|
seasonSendDate: "",
|
|
seasonId: "",
|
|
seasonDesc: "",
|
|
}]
|
|
res.json({seasons: seasons});
|
|
});
|
|
|
|
app.get('/api/seasons/:id', (req, res) => {
|
|
data = {
|
|
details: {
|
|
id: 1,
|
|
seasonName: "Season 1",
|
|
seasonTag: "Post Winter Blues",
|
|
seasonCardImage: "",
|
|
seasonHeaderImage: "",
|
|
seasonStartDate: "",
|
|
seasonSendDate: "",
|
|
seasonId: "",
|
|
seasonDesc: "",
|
|
},
|
|
standings: [{
|
|
position: 1,
|
|
points: 4,
|
|
user: {
|
|
realName: "Dan H",
|
|
gamerHandle: "Quildra",
|
|
}
|
|
},
|
|
{
|
|
position: 2,
|
|
points: 2,
|
|
user: {
|
|
realName: "Dan Mc",
|
|
gamerHandle: "Mini-Quildra",
|
|
}
|
|
},
|
|
],
|
|
weeks:[{
|
|
id: "1",
|
|
map: "bob",
|
|
mapImg: "bob.jpg",
|
|
entries: [
|
|
{
|
|
position: 1,
|
|
runTime: 4.0,
|
|
user: {
|
|
realName: "Dan H",
|
|
gamerHandle: "Quildra",
|
|
}
|
|
}
|
|
]
|
|
}]
|
|
}
|
|
res.json({data: data});
|
|
});
|
|
|
|
// route for handling requests from the Angular client
|
|
app.post('/api/upload-replay', upload.single('file'), (req, res) => {
|
|
let file = req.file;
|
|
console.log("File uploaded: ", req.file);
|
|
fs.readFile(file.path, function(err, buffer)
|
|
{
|
|
buff = new AdvancableBuffer(buffer);
|
|
header = new gbxHeader().parse(buffer);
|
|
})
|
|
});
|
|
|
|
app.listen(3000, () => {
|
|
console.log('Server listening on port 3000');
|
|
});
|