Files
Jellystat/backend/server.js
Thegan Govender 4391c2ede8 Fix for Proxy with SSL support
Added SSL proxying on the server side so that HTTP content can be served with HTTPS content

Changed css colors from hardcoded values to use variables

Changes to websockets to be able to work with SSL/Proxies

WIP support of 12/24Hr time format in activities tables  (Not working yet)
WIP added version checker to be able to check for available updates from GitHub (Not working yet)
2023-05-09 09:53:39 +02:00

82 lines
2.3 KiB
JavaScript

const express = require('express');
const cors = require('cors');
const jwt = require('jsonwebtoken');
const knex = require('knex');
const createdb = require('./create_database');
const knexConfig = require('./migrations');
const authRouter= require('./auth');
const apiRouter = require('./api');
const proxyRouter = require('./proxy');
const syncRouter = require('./sync');
const statsRouter = require('./stats');
const backupRouter = require('./backup');
const ActivityMonitor = require('./watchdog/ActivityMonitor');
const { checkForUpdates } = require('./version-control');
const app = express();
const db = knex(knexConfig.development);
const PORT = process.env.PORT || 3003;
const LISTEN_IP = '127.0.0.1';
const JWT_SECRET = process.env.JWT_SECRET ||'my-secret-jwt-key';
if (JWT_SECRET === undefined) {
console.log('JWT Secret cannot be undefined');
process.exit(1); // end the program with error status code
}
app.use(express.json()); // middleware to parse JSON request bodies
app.use(cors());
// JWT middleware
function verifyToken(req, res, next) {
const authHeader = req.headers.authorization;
if (authHeader) {
const token = authHeader.split(' ')[1];
jwt.verify(token, JWT_SECRET, (err, user) => {
if (err) {
return res.sendStatus(403);
}
req.user = user;
next();
});
} else {
res.sendStatus(401);
}
}
app.use('/auth', authRouter); // mount the API router at /api, with JWT middleware
app.use('/api', verifyToken, apiRouter); // mount the API router at /api, with JWT middleware
app.use('/proxy', proxyRouter); // mount the API router at /api, with JWT middleware
app.use('/sync', verifyToken, syncRouter); // mount the API router at /sync, with JWT middleware
app.use('/stats', verifyToken, statsRouter); // mount the API router at /stats, with JWT middleware
app.use('/data', verifyToken, backupRouter); // mount the API router at /stats, with JWT middleware
try{
createdb.createDatabase().then((result) => {
if (result) {
console.log('Database created');
}
db.migrate.latest().then(() => {
app.listen(PORT, async () => {
console.log(`Server listening on http://${LISTEN_IP}:${PORT}`);
checkForUpdates();
ActivityMonitor.ActivityMonitor(1000);
});
});
});
}catch(error)
{
console.log('An error has occured on startup: '+error);
}