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.
 
 
 
 
 

199 lines
4.7 KiB

const express = require('express');
const bodyParser = require('body-parser');
const jwt = require('jsonwebtoken');
const cors = require('cors');
const { Document, Schema, connect } = require('camo');
// Define the Racers schema
class Racer extends Document {
constructor() {
super();
this.name = String;
this.gameHandle = String;
}
}
// Define the Seasons schema
class Season extends Document {
constructor() {
super();
this.name = String;
this.subtitle = String;
this.seasonStartDate = Date;
}
}
// Define the Races schema
class Race extends Document {
constructor() {
super();
this.weekNumber = Number;
this.raceStartDate = Date;
this.raceEndDate = Date;
this.mapName = String;
this.mapLink = String;
this.season = Season;
}
}
// Define the RaceResults schema
class RaceResult extends Document {
constructor() {
super();
this.race = Race;
this.racer = Racer;
this.timeInSeconds = Number;
this.replayPath = String;
}
}
connect('nedb://./nedb-database.db');
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',
error: err});
}
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', async (req, res) => {
const seasons = await Season.find();
res.json({seasons: seasons});
});
app.get('/api/seasons/:id', (req, res) => {
res.json({data: season_details});
});
app.post('/api/seasons/add', verifyToken, async (req, res) => {
console.log("New Seaosn Time");
const { name, subtitle, seasonStartDate } = req.body;
const season = await Season.create({ name, subtitle, seasonStartDate });
console.log(season);
res.json(season);
});
// 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');
});