Add route to get stuff

This commit is contained in:
Christopher Cookman 2026-01-25 15:00:32 -07:00
parent aec4e84d4a
commit 0a5b3775a7
4 changed files with 24 additions and 2 deletions

View file

@ -37,6 +37,13 @@ app.use((req, res, next) => {
next();
})
global.auth = (req, res, next) => {
if (process.env.API_KEY && req.headers['Authorization'] === process.env.API_KEY) {
return next();
}
return res.status(401).json({ error: 'Unauthorized' });
}
// Load routes from routes/* recursively
const fs = require('fs');
const path = require('path');

15
routes/api/getAll.js Normal file
View file

@ -0,0 +1,15 @@
const express = require('express');
const router = express.Router();
const db = global.db;
router.get("/", global.auth, async (req, res) => {
db.all("SELECT * FROM analytics", [], (err, rows) => {
if (err) {
console.error('Failed to retrieve analytics data', err);
return res.status(500).json({ error: 'Database error' });
}
return res.status(200).json({ data: rows });
});
});
module.exports = router;

View file

@ -2,7 +2,7 @@ const express = require('express');
const router = express.Router();
const db = global.db;
router.post('/', async (req, res) => {
router.post('/', global.auth, async (req, res) => {
const { serverId, totalPlayers, currentPlayers, duration } = req.body;
if (!serverId || totalPlayers === undefined || currentPlayers === undefined || duration === undefined) {
return res.status(400).json({ error: 'Missing required fields' });

View file

@ -2,7 +2,7 @@ const express = require('express');
const router = express.Router();
const db = global.db;
router.post('/', async (req, res) => {
router.post('/', global.auth, async (req, res) => {
const { serverId, serverStartTime, revision, placeId} = req.body;
console.log('Received startup data:', req.body);
if (serverId === undefined || serverStartTime === undefined || revision === undefined || placeId === undefined) {