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.
137 lines
3.5 KiB
137 lines
3.5 KiB
|
|
const express = require('express');
|
|
const bodyParser = require('body-parser');
|
|
const jwt = require('jsonwebtoken');
|
|
const cors = require('cors');
|
|
|
|
const fs = require('fs');
|
|
const app = express();
|
|
const upload = require('./backend/routes/upload-replay');
|
|
|
|
const AdvancableBuffer = require('./backend/utilities/AdvancableBuffer.js');
|
|
const gbxHeader = require('./backend/trackmania-replays/gbx-header.js');
|
|
|
|
// handling CORS
|
|
app.use(cors());
|
|
app.use(bodyParser.json());
|
|
|
|
//const RSA_PRIVATE_KEY = fs.readFileSync('./private.key');
|
|
const RSA_PRIVATE_KEY = "Secret_KeY";
|
|
const RSA_PUBLIC_KEY = fs.readFileSync('./public.key');
|
|
|
|
const users = [
|
|
{ id: 1, username: 'user1', password: 'password1' },
|
|
{ id: 2, username: 'user2', password: 'password2' }
|
|
];
|
|
|
|
const seasons = [
|
|
{ id: 1, seasonName: "Season 1", seasonTag: "Post Winter Blues", seasonCardImage: "", seasonHeaderImage: "", seasonStartDate: "", seasonEndDate: "", seasonDesc: "",},
|
|
{ id: 2, seasonName: "Season 2", seasonTag: "Post Winter Blues", seasonCardImage: "", seasonHeaderImage: "", seasonStartDate: "", seasonEndDate: "", seasonDesc: "",}
|
|
]
|
|
|
|
const season_details = {
|
|
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",
|
|
}
|
|
}
|
|
]
|
|
}]
|
|
}
|
|
|
|
function verifyToken(req, res, next) {
|
|
const token = req.headers['authorization'];
|
|
|
|
if (!token) {
|
|
return res.status(403).json({ message: 'Token not provided' });
|
|
}
|
|
|
|
jwt.verify(token, RSA_PRIVATE_KEY, (err, decoded) => {
|
|
if (err) {
|
|
return res.status(401).json({ message: 'Failed to authenticate token' });
|
|
}
|
|
|
|
req.userId = decoded.userId;
|
|
next();
|
|
});
|
|
}
|
|
|
|
app.post('/api/login', (req, res) => {
|
|
const { username, password } = req.body;
|
|
|
|
// Check if the user exists and the password is correct
|
|
const user = users.find(u => u.username === username && u.password === password);
|
|
|
|
if (!user) {
|
|
return res.status(401).json({ message: 'Invalid username or password' });
|
|
}
|
|
|
|
// Generate and return a JWT token
|
|
const token = jwt.sign({ userId: user.id, username: user.username }, RSA_PRIVATE_KEY, { expiresIn: '1h' });
|
|
res.json({ token });
|
|
});
|
|
|
|
// Protected route example
|
|
app.get('/api/profile', verifyToken, (req, res) => {
|
|
const user = users.find(u => u.id === req.userId);
|
|
res.json({ username: user.username, userId: user.id });
|
|
});
|
|
|
|
app.get('/api/seasons', (req, res) => {
|
|
res.json({seasons: seasons});
|
|
});
|
|
|
|
app.get('/api/seasons/:id', (req, res) => {
|
|
res.json({data: season_details});
|
|
});
|
|
|
|
// 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');
|
|
});
|