mirror of
https://github.com/Tzahi12345/YoutubeDL-Material.git
synced 2026-03-07 20:10:03 +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';
|
||||
|
||||
// logging setup
|
||||
|
||||
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();
|
||||
// required initialization
|
||||
|
||||
// config values
|
||||
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'};
|
||||
}
|
||||
|
||||
// don't overwrite config if it already happened.. NOT
|
||||
// let alreadyWritten = db.get('configWriteFlag').value();
|
||||
exports.initialize = () => {
|
||||
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
|
||||
config_api.configExistsCheck();
|
||||
// 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();
|
||||
|
||||
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 }));
|
||||
app.use(bodyParser.json());
|
||||
// checks if config exists, if not, a config is auto generated
|
||||
config_api.configExistsCheck();
|
||||
|
||||
// use passport
|
||||
app.use(auth_api.passport.initialize());
|
||||
setAndLoadConfig();
|
||||
|
||||
app.use(bodyParser.urlencoded({ extended: false }));
|
||||
app.use(bodyParser.json());
|
||||
|
||||
// use passport
|
||||
app.use(auth_api.passport.initialize());
|
||||
}
|
||||
|
||||
exports.initialize();
|
||||
|
||||
// actual functions
|
||||
|
||||
@@ -255,7 +256,7 @@ async function simplifyDBFileStructure() {
|
||||
return true;
|
||||
}
|
||||
|
||||
async function startServer() {
|
||||
exports.startServer = async () => {
|
||||
if (process.env.USING_HEROKU && process.env.PORT) {
|
||||
// default to heroku port if using heroku
|
||||
backendPort = process.env.PORT || backendPort;
|
||||
@@ -548,7 +549,9 @@ async function loadConfig() {
|
||||
}
|
||||
|
||||
// start the server here
|
||||
startServer();
|
||||
if (typeof require !== 'undefined' && require.main === module) {
|
||||
exports.startServer();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1643,6 +1646,8 @@ app.get('/api/stream', optionalJwt, async (req, res) => {
|
||||
}
|
||||
if (!fs.existsSync(file_path)) {
|
||||
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 fileSize = stat.size;
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
const { app, BrowserWindow } = require('electron');
|
||||
const path = require('path');
|
||||
const elogger = require('electron-log');
|
||||
const { spawn } = require('child_process');
|
||||
const server = require('./app');
|
||||
|
||||
let win;
|
||||
let splashWindow;
|
||||
let serverProcess;
|
||||
|
||||
function createSplashWindow() {
|
||||
async function createSplashWindow() {
|
||||
splashWindow = new BrowserWindow({
|
||||
width: 800,
|
||||
height: 600,
|
||||
@@ -17,7 +16,7 @@ function createSplashWindow() {
|
||||
nodeIntegration: true
|
||||
}
|
||||
})
|
||||
splashWindow.loadFile('public/assets/splash.html')
|
||||
await splashWindow.loadFile('public/assets/splash.html')
|
||||
splashWindow.on('closed', () => {
|
||||
splashWindow = null
|
||||
})
|
||||
@@ -52,37 +51,14 @@ function loadPage() {
|
||||
win.show()
|
||||
}
|
||||
|
||||
function createWindow() {
|
||||
async function createWindow() {
|
||||
await createSplashWindow();
|
||||
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!')
|
||||
createMainWindow();
|
||||
createSplashWindow();
|
||||
|
||||
// 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);
|
||||
});
|
||||
loadPage();
|
||||
}
|
||||
|
||||
app.on('ready', createWindow);
|
||||
@@ -92,10 +68,6 @@ app.on('window-all-closed', () => {
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit();
|
||||
}
|
||||
if (serverProcess) {
|
||||
serverProcess.stdin.pause();
|
||||
serverProcess.kill();
|
||||
}
|
||||
});
|
||||
|
||||
// initialize the app's main window
|
||||
|
||||
Reference in New Issue
Block a user