Refactor: split main.js into app/ modules

This commit is contained in:
MasterAcnolo
2026-04-11 16:14:21 +02:00
parent 0c7103c5c9
commit 96401a6e1f
9 changed files with 284 additions and 287 deletions

View File

@@ -1,47 +0,0 @@
const config = require('../config');
const RPC = require("discord-rpc");
const { logger } = require("./logger");
const clientId = `${config.DiscordRPCID}`;
const rpc = new RPC.Client({ transport: "ipc" });
let intervalId;
function startRPC() {
rpc.on("ready", () => {
const presence = {
largeImageKey: "icon",
smallImageKey: "acnolo_pfp",
smallImageText: "By MasterAcnolo",
startTimestamp: new Date(),
details: `Open Source Download Tools - ${config.version}`,
state: "masteracnolo.github.io/FreedomLoader",
};
rpc.clearActivity()
rpc.setActivity(presence);
});
rpc.login({ clientId }).catch(err => {
logger.error("Unable to connect to the RPC:", err);
});
}
async function stopRPC(){
try {
if (intervalId) clearInterval(intervalId);
if (rpc && rpc.transport) {
await rpc.clearActivity()
await rpc.destroy();
} else{
logger.error("Not Able to close RPC")
}
} catch (err) {
logger.error("Error while closing the RPC:", err);
}
}
module.exports = { startRPC, stopRPC};

View File

@@ -2,30 +2,19 @@ const express = require("express");
const path = require("path");
const { logger, logSessionEnd } = require("./logger");
const config = require("../config");
const { execFile } = require("child_process");
const { userYtDlp } = require("./helpers/path.helpers");
const { rateLimite } = require("./helpers/rateLimit.helpers")
const { rateLimite } = require("./helpers/rateLimit.helpers");
const app = express();
app.use(express.json());
// Update YT-DLP on Startup
execFile(userYtDlp, ["-U"], (err, stdout, stderr) => {
if (err) logger.warn("yt-dlp update error:", err);
else logger.info(`Update yt-dlp : ${stdout}`);
});
// Middlewares
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "../public")));
// Routes
app.use("/download", require("./routes/download.route"));
app.use("/info", require("./routes/info.route"));
// When we get API Root, it return the front pages.
app.get("/", rateLimite ,(req, res) => {
app.use("/info", require("./routes/info.route"));
app.get("/", rateLimite, (req, res) => {
res.sendFile(path.join(__dirname, "../public/index.html"));
});
@@ -37,19 +26,19 @@ async function startServer() {
});
server.on("error", (err) => {
logger.error("Express Server Error :", err);
logger.error("Express server error:", err);
reject(err);
});
const gracefulExit = () => {
logSessionEnd();
server.close(() => {
logger.info("Express Server close cleanly.");
logger.info("Express server closed cleanly.");
process.exit();
});
};
process.on("SIGINT", gracefulExit);
process.on("SIGINT", gracefulExit);
process.on("SIGTERM", gracefulExit);
});
}

View File

@@ -1,47 +0,0 @@
const { autoUpdater } = require("electron-updater");
const { app, Notification, shell} = require("electron");
const { logger } = require("./logger");
function AutoUpdater() {
autoUpdater.on("update-available", (info) => {
logger.info(`New Version Available : ${info.version}`);
new Notification({
title: "Freedom Loader",
body: `New Version Available : ${info.version}. Application will restart`
}).show();
});
autoUpdater.on("update-downloaded", (info) => {
logger.info(`Update Downloaded : ${info.version}`);
new Notification({
title: "Freedom Loader",
body: `Update ${info.version} downloaded.`
}).on("click", () =>
shell.openExternal("https://github.com/MasterAcnolo/Freedom-Loader/releases/latest")
).show();
setTimeout(() => {
autoUpdater.quitAndInstall();
}, 5000);
});
autoUpdater.on("error", (err) => {
logger.error("Auto Update Error :", err.message);
new Notification({
title: "Freedom Loader - Update Error",
body: err.message
}).show();
});
app.whenReady().then(async () => {
try {
await autoUpdater.checkForUpdates();
logger.info("Update check completed");
} catch (err) {
logger.error("Error during update check :", err.message);
}
});
}
module.exports = { AutoUpdater };