fixed electron build + implemented dynamic settings + custom user data folder

This commit is contained in:
X4Lo committed 2024-06-05 00:14:48 +01:00
1 parent 8958ead210
commit b872cd0733
14 files changed
+286 -81

No files matched your search

+44 -23
View File
@@ -1,22 +1,30 @@
// server.js
const express = require('express');
const bodyParser = require('body-parser');
const routes = require('./routes/routes.js');
const sequelize = require('./db/database.js');
const path = require('path');
const fs = require('fs');
const app = express();
const PORT = process.env.PORT || 4000;
const relations = require('./db/relations.js');
const sequelizeHooks = require('./db/sequelizeHooks.js');
const { initializeGenres, initializeData } = require('./db/dataInitializer.js');
const { initializeData } = require('./db/dataInitializer.js');
const killerRouter = require('./routes/killer.routes.js');
const errorHandler = require('./middleware/errorHandler');
const { getSettings } = require('./services/settingsService.js');
const { logToFile } = require('./services/logService.js');
// Middleware
// ? variables
let settings = getSettings()
const PORT = settings["backPort"] || 4000;
const parrentProcess = JSON.parse(process.env.PARENT_PROCESS || '{}');
// ? Middleware
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
// CORS Middleware to allow requests from any origin
// * CORS Middleware to allow requests from any origin
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header(
@@ -30,52 +38,65 @@ app.use((req, res, next) => {
next();
});
// ? Routes
// * media access route
app.use('/api/v1/media/:category/:type', (req, res, next) => {
// ? Routes : media access routes
app.use('/api/v1/media/:category/:type/:file', (req, res, next) => {
const category = req.params.category;
const type = req.params.type;
const file = req.params.file;
if (!['entries', 'collections', 'franchises'].includes(category)) {
return res.status(404).send('Not Found');
}
if (!['backdrops', 'posters', 'others'].includes(type)) {
if (!['backdrops', 'posters', 'logos', 'others'].includes(type)) {
return res.status(404).send('Not Found');
}
const options = {
fallthrough: false, // ! Important: don't call next middleware if file isn't found
};
const settings = getSettings();
const dataPath = settings.userDataPath;
express.static(path.join(__dirname, '..', `uploads/${category}/${type}`), options)(req, res, next);
const mediaPath = path.join(dataPath, 'images', category, type);
// const mediaPath = path.join(__dirname, '..', `data/images/${category}/${type}`);
const filePath = path.join(mediaPath, file);
logToFile(filePath)
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
return res.status(404).send('Not Found');
}
const options = {
fallthrough: false, // ! Important: don't call next middleware if file isn't found
};
// express.static(filePath, options)(req, res, next);
// Serve the file using fs.createReadStream
const stream = fs.createReadStream(filePath);
stream.pipe(res);
});
});
// * other api routes
// ? other api routes
app.use('/api/v1', routes);
// ! Error handling middleware
const errorHandler = require('./middleware/errorHandler');
// ? Error handling middleware
app.use(errorHandler);
// ! KILLER ROUTE: used to crash the backend
// ? KILLER ROUTE: used to crash the backend
app.use('/killer', killerRouter);
// Start the server
// * Start the server
const doUpdate = false;
const doForce = false;
console.log(`Syncing database schema...`);
sequelize
// .sync({force: true}) // will drop all tables and recreate them.
.sync({ alter: doUpdate }) // will attempt to automatically alter tables to fit the model definitions.
// .sync()
.then((result) => {
console.log(`Database schema synced...`);
console.log(`Database schema synced.`);
initializeData();
app.listen(PORT, () => {
//const Entry = require("./models/Entry.js")
console.log(`Backend server running on http://localhost:${PORT}`);
console.log(`Backend server running on http://localhost:${PORT}/`);
});
})
.catch(err => {