mirror of
https://github.com/Tzahi12345/YoutubeDL-Material.git
synced 2026-04-11 09:01:28 +03:00
Frontend initialization refactor to ensure app start in electron
This commit is contained in:
111
backend/app.js
111
backend/app.js
@@ -60,48 +60,7 @@ let debugMode = process.env.YTDL_MODE === 'debug';
|
|||||||
|
|
||||||
const admin_token = '4241b401-7236-493e-92b5-b72696b9d853';
|
const admin_token = '4241b401-7236-493e-92b5-b72696b9d853';
|
||||||
|
|
||||||
// logging setup
|
// required initialization
|
||||||
|
|
||||||
config_api.initialize();
|
|
||||||
db_api.initialize(db, users_db);
|
|
||||||
auth_api.initialize(db_api);
|
|
||||||
|
|
||||||
// Set some defaults
|
|
||||||
db.defaults(
|
|
||||||
{
|
|
||||||
playlists: [],
|
|
||||||
files: [],
|
|
||||||
configWriteFlag: false,
|
|
||||||
downloads: {},
|
|
||||||
subscriptions: [],
|
|
||||||
files_to_db_migration_complete: false,
|
|
||||||
tasks_manager_role_migration_complete: false,
|
|
||||||
archives_migration_complete: false
|
|
||||||
}).write();
|
|
||||||
|
|
||||||
users_db.defaults(
|
|
||||||
{
|
|
||||||
users: [],
|
|
||||||
roles: {
|
|
||||||
"admin": {
|
|
||||||
"permissions": [
|
|
||||||
'filemanager',
|
|
||||||
'settings',
|
|
||||||
'subscriptions',
|
|
||||||
'sharing',
|
|
||||||
'advanced_download',
|
|
||||||
'downloads_manager'
|
|
||||||
]
|
|
||||||
}, "user": {
|
|
||||||
"permissions": [
|
|
||||||
'filemanager',
|
|
||||||
'subscriptions',
|
|
||||||
'sharing'
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
).write();
|
|
||||||
|
|
||||||
// config values
|
// config values
|
||||||
let url = null;
|
let url = null;
|
||||||
@@ -153,19 +112,61 @@ if (fs.existsSync('version.json')) {
|
|||||||
version_info = {'type': 'N/A', 'tag': 'N/A', 'commit': 'N/A', 'date': 'N/A'};
|
version_info = {'type': 'N/A', 'tag': 'N/A', 'commit': 'N/A', 'date': 'N/A'};
|
||||||
}
|
}
|
||||||
|
|
||||||
// don't overwrite config if it already happened.. NOT
|
exports.initialize = () => {
|
||||||
// let alreadyWritten = db.get('configWriteFlag').value();
|
config_api.initialize();
|
||||||
|
db_api.initialize(db, users_db);
|
||||||
|
auth_api.initialize(db_api);
|
||||||
|
|
||||||
// checks if config exists, if not, a config is auto generated
|
// Set some defaults
|
||||||
config_api.configExistsCheck();
|
db.defaults(
|
||||||
|
{
|
||||||
|
playlists: [],
|
||||||
|
files: [],
|
||||||
|
configWriteFlag: false,
|
||||||
|
downloads: {},
|
||||||
|
subscriptions: [],
|
||||||
|
files_to_db_migration_complete: false,
|
||||||
|
tasks_manager_role_migration_complete: false,
|
||||||
|
archives_migration_complete: false
|
||||||
|
}).write();
|
||||||
|
|
||||||
setAndLoadConfig();
|
users_db.defaults(
|
||||||
|
{
|
||||||
|
users: [],
|
||||||
|
roles: {
|
||||||
|
"admin": {
|
||||||
|
"permissions": [
|
||||||
|
'filemanager',
|
||||||
|
'settings',
|
||||||
|
'subscriptions',
|
||||||
|
'sharing',
|
||||||
|
'advanced_download',
|
||||||
|
'downloads_manager'
|
||||||
|
]
|
||||||
|
}, "user": {
|
||||||
|
"permissions": [
|
||||||
|
'filemanager',
|
||||||
|
'subscriptions',
|
||||||
|
'sharing'
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
).write();
|
||||||
|
|
||||||
app.use(bodyParser.urlencoded({ extended: false }));
|
// checks if config exists, if not, a config is auto generated
|
||||||
app.use(bodyParser.json());
|
config_api.configExistsCheck();
|
||||||
|
|
||||||
// use passport
|
setAndLoadConfig();
|
||||||
app.use(auth_api.passport.initialize());
|
|
||||||
|
app.use(bodyParser.urlencoded({ extended: false }));
|
||||||
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
|
// use passport
|
||||||
|
app.use(auth_api.passport.initialize());
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.initialize();
|
||||||
|
|
||||||
// actual functions
|
// actual functions
|
||||||
|
|
||||||
@@ -255,7 +256,7 @@ async function simplifyDBFileStructure() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function startServer() {
|
exports.startServer = async () => {
|
||||||
if (process.env.USING_HEROKU && process.env.PORT) {
|
if (process.env.USING_HEROKU && process.env.PORT) {
|
||||||
// default to heroku port if using heroku
|
// default to heroku port if using heroku
|
||||||
backendPort = process.env.PORT || backendPort;
|
backendPort = process.env.PORT || backendPort;
|
||||||
@@ -548,7 +549,9 @@ async function loadConfig() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// start the server here
|
// start the server here
|
||||||
startServer();
|
if (typeof require !== 'undefined' && require.main === module) {
|
||||||
|
exports.startServer();
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -1643,6 +1646,8 @@ app.get('/api/stream', optionalJwt, async (req, res) => {
|
|||||||
}
|
}
|
||||||
if (!fs.existsSync(file_path)) {
|
if (!fs.existsSync(file_path)) {
|
||||||
logger.error(`File ${file_path} could not be found! UID: ${uid}, ID: ${file_obj.id}`);
|
logger.error(`File ${file_path} could not be found! UID: ${uid}, ID: ${file_obj.id}`);
|
||||||
|
res.sendStatus(404);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
const stat = fs.statSync(file_path);
|
const stat = fs.statSync(file_path);
|
||||||
const fileSize = stat.size;
|
const fileSize = stat.size;
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
const { app, BrowserWindow } = require('electron');
|
const { app, BrowserWindow } = require('electron');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const elogger = require('electron-log');
|
const elogger = require('electron-log');
|
||||||
const { spawn } = require('child_process');
|
const server = require('./app');
|
||||||
|
|
||||||
let win;
|
let win;
|
||||||
let splashWindow;
|
let splashWindow;
|
||||||
let serverProcess;
|
|
||||||
|
|
||||||
function createSplashWindow() {
|
async function createSplashWindow() {
|
||||||
splashWindow = new BrowserWindow({
|
splashWindow = new BrowserWindow({
|
||||||
width: 800,
|
width: 800,
|
||||||
height: 600,
|
height: 600,
|
||||||
@@ -17,7 +16,7 @@ function createSplashWindow() {
|
|||||||
nodeIntegration: true
|
nodeIntegration: true
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
splashWindow.loadFile('public/assets/splash.html')
|
await splashWindow.loadFile('public/assets/splash.html')
|
||||||
splashWindow.on('closed', () => {
|
splashWindow.on('closed', () => {
|
||||||
splashWindow = null
|
splashWindow = null
|
||||||
})
|
})
|
||||||
@@ -52,37 +51,14 @@ function loadPage() {
|
|||||||
win.show()
|
win.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
function createWindow() {
|
async function createWindow() {
|
||||||
|
await createSplashWindow();
|
||||||
elogger.info('Spawning server.')
|
elogger.info('Spawning server.')
|
||||||
serverProcess = spawn('node', [path.join(__dirname, 'app.js')]);
|
// serverProcess = spawn('node', [path.join(__dirname, 'app.js')]);
|
||||||
|
await server.startServer();
|
||||||
elogger.info('Done spawning!')
|
elogger.info('Done spawning!')
|
||||||
createMainWindow();
|
createMainWindow();
|
||||||
createSplashWindow();
|
loadPage();
|
||||||
|
|
||||||
// Log the server output to the console
|
|
||||||
serverProcess.stdout.on('data', (data) => {
|
|
||||||
const data_str = data.toString();
|
|
||||||
if (data_str.includes('started on PORT')) {
|
|
||||||
loadPage();
|
|
||||||
}
|
|
||||||
console.log(`Server output: ${data}`);
|
|
||||||
elogger.info(data_str);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Log any errors to the console
|
|
||||||
serverProcess.stderr.on('data', (data) => {
|
|
||||||
console.error(`Server error: ${data}`);
|
|
||||||
const error = data.toString();
|
|
||||||
if (error.includes('EADDRINUSE')) {
|
|
||||||
loadPage();
|
|
||||||
}
|
|
||||||
elogger.error(error);
|
|
||||||
});
|
|
||||||
|
|
||||||
process.on('uncaughtException', (error) => {
|
|
||||||
console.error('Uncaught exception:', error);
|
|
||||||
elogger.error(error);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
app.on('ready', createWindow);
|
app.on('ready', createWindow);
|
||||||
@@ -92,10 +68,6 @@ app.on('window-all-closed', () => {
|
|||||||
if (process.platform !== 'darwin') {
|
if (process.platform !== 'darwin') {
|
||||||
app.quit();
|
app.quit();
|
||||||
}
|
}
|
||||||
if (serverProcess) {
|
|
||||||
serverProcess.stdin.pause();
|
|
||||||
serverProcess.kill();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// initialize the app's main window
|
// initialize the app's main window
|
||||||
|
|||||||
Reference in New Issue
Block a user