Skip to content

Commit

Permalink
Merge pull request #7486 from ever-co/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
evereq authored Feb 1, 2024
2 parents 7ca4dc0 + 003037a commit 08df6bc
Show file tree
Hide file tree
Showing 34 changed files with 997 additions and 469 deletions.
230 changes: 84 additions & 146 deletions apps/server/src/index.ts

Large diffs are not rendered by default.

162 changes: 88 additions & 74 deletions apps/server/src/preload/desktop-server-api.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,99 @@
import { fork } from 'child_process';
import { fork, ChildProcessWithoutNullStreams } from 'child_process';
import { LocalStore } from '@gauzy/desktop-libs';

export interface IEnvApi {
IS_ELECTRON: string,
DB_PATH: string,
DB_TYPE: string,
DB_HOST: string,
DB_PORT: string,
DB_NAME: string,
DB_USER: string,
DB_PASS: string,
API_PORT: string,
API_HOST: string,
API_BASE_URL: string
interface IEnvApi {
IS_ELECTRON: string;
DB_PATH: string;
DB_TYPE: string;
DB_HOST?: string;
DB_PORT?: string;
DB_NAME?: string;
DB_USER?: string;
DB_PASS?: string;
API_PORT: string;
API_HOST: string;
API_BASE_URL: string;
}

class ApiServerProcessFactory {
public static createApiServerProcess(): ChildProcessWithoutNullStreams {
try {
const { apiPath, db, dbName, dbPassword, dbUsername, dbPort, port, dbPath, dbHost, apiBaseUrl, apiHost } =
this.prepareServerApi();

const runServerApi = () => {
const { apiPath, db, dbName, dbPassword, dbUsername, dbPort, port, dbPath, dbHost, apiBaseUrl, apiHost } = prepareServerApi();
const apiEnv: IEnvApi = {
IS_ELECTRON: 'true',
DB_PATH: dbPath,
DB_TYPE: db,
DB_HOST: dbHost,
DB_PORT: String(dbPort),
DB_NAME: dbName,
DB_USER: dbUsername,
DB_PASS: dbPassword,
API_PORT: String(port),
API_HOST: apiHost,
API_BASE_URL: apiBaseUrl
}
const uiService = fork(apiPath, {
silent: true, detached: true, env: {
...process.env,
IS_ELECTRON: apiEnv.IS_ELECTRON
}});
uiService.stdout.on('data', (data) => {
console.log('SERVER API STATE LOGS -> ', data.toString());
uiService.unref();
process.exit(0);
});
const apiEnv: IEnvApi = {
IS_ELECTRON: 'true',
DB_PATH: dbPath,
DB_TYPE: db,
DB_HOST: dbHost || '',
DB_PORT: dbPort ? String(dbPort) : '',
DB_NAME: dbName || '',
DB_USER: dbUsername || '',
DB_PASS: dbPassword || '',
API_PORT: String(port),
API_HOST: apiHost,
API_BASE_URL: apiBaseUrl
};

const uiService = fork(apiPath, {
silent: true,
detached: true,
env: {
...process.env,
...apiEnv
}
});

uiService.stdout.on('data', (data) => {
console.log('SERVER API STATE LOGS -> ', data.toString());
uiService.unref();
process.exit(0);
});

return uiService;
} catch (error) {
console.error('[CRITICAL::ERROR]: Running API server failed:', error);
}
}

private static prepareServerApi(): {
apiPath: string;
db: string;
dbPort: number | null;
dbName: string | null;
dbUsername: string | null;
dbPassword: string | null;
port: number;
dbPath: string;
dbHost: string | null;
apiBaseUrl: string;
apiHost: string;
} {
const { port, db, apiPath, dbPath, postgres, apiBaseUrl } = LocalStore.getStore('configs');

return {
apiPath,
db,
dbPort: postgres?.dbPort || null,
dbName: postgres?.dbName || null,
dbPassword: postgres?.dbPassword || null,
dbUsername: postgres?.dbUsername || null,
dbHost: postgres?.dbHost || null,
port,
dbPath,
apiBaseUrl,
apiHost: '0.0.0.0'
};
}
}

const prepareServerApi = (): {
apiPath: string,
db: string,
dbPort: number | null,
dbName: string | null,
dbUsername: string | null,
dbPassword: string | null,
port: number,
dbPath: string,
dbHost: string | null,
apiBaseUrl: string,
apiHost: string
} => {
const {
port,
db,
apiPath,
dbPath,
postgres,
apiBaseUrl
} = LocalStore.getStore('configs');
return {
apiPath,
db,
dbPort: postgres?.dbPort || null,
dbName: postgres?.dbName || null,
dbPassword: postgres?.dbPassword || null,
dbUsername: postgres?.dbUsername || null,
dbHost: postgres?.dbHost || null,
port,
dbPath,
apiBaseUrl,
apiHost: '0.0.0.0'
};
class App {
public static main(): void {
ApiServerProcessFactory.createApiServerProcess();
}
}

export default function () {
console.log('Before Run Api Server');
runServerApi();
console.log('Before running API Server');
App.main();
}
81 changes: 51 additions & 30 deletions apps/server/src/preload/desktop-server-ui.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,59 @@
import { spawn } from 'child_process';
import * as path from 'path';
import { spawn, ChildProcessWithoutNullStreams } from 'child_process';
import path from 'path';
import { app } from 'electron';
import os from 'os';
const appPath = app.getPath('userData');

const runServerUI = () => {
try {
const { uiPath } = prepareServerUi();
console.log('ui path', uiPath);
const uiService = spawn(uiPath, { detached: true });
class ServerProcessFactory {
public static createUiServerProcess(): ChildProcessWithoutNullStreams {
const appPath = app.getPath('userData');
const uiPath = this.prepareServerUi(appPath);
console.log('UI Path:', uiPath);

const uiService = spawn(uiPath, { detached: true, stdio: 'pipe' });

uiService.stdout.on('data', (data) => {
console.log('SERVER UI STATE LOGS -> ', data.toString());
});
} catch (error) {
console.log('Error in runServerUI', error);

uiService.stderr.on('data', (data) => {
console.error('SERVER UI ERROR LOGS -> ', data.toString());
});

uiService.on('error', (error) => {
console.error('Failed to start UI server:', error);
});

uiService.on('exit', (code, signal) => {
console.log(`UI server exited with code ${code} and signal ${signal}`);
});

return uiService;
}

private static prepareServerUi(appPath: string): string {
let appName = '';
switch (os.platform()) {
case 'win32':
appName = `${process.env.NAME}.exe`;
break;
case 'darwin':
appName = process.env.NAME || '';
break;
default:
break;
}
return path.join(appPath, appName);
}
};

const prepareServerUi = (): {
uiPath: string;
} => {
let appName: string = '';
switch (os.platform()) {
case 'win32':
appName = `${process.env.NAME}.exe`;
break;
case 'darwin':
appName = process.env.NAME;
break;
default:
break;
}

class App {
public static main(): void {
try {
ServerProcessFactory.createUiServerProcess();
} catch (error) {
console.error('[CRITICAL::ERROR]: Starting server:', error);
}
}
return {
uiPath: path.join(appPath, appName)
};
};
runServerUI();
}

App.main();
12 changes: 0 additions & 12 deletions apps/server/src/preload/loginPage.ts

This file was deleted.

17 changes: 17 additions & 0 deletions apps/server/src/preload/server-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { StaticFileServer } from './static-file-server';

export class ServerManager {
private staticFileServer: StaticFileServer;

public constructor() {
this.staticFileServer = StaticFileServer.getInstance();
}

public runServer(port: number) {
try {
this.staticFileServer.startServer(port);
} catch (error) {
console.error('[CRITICAL::ERROR]: Starting server:', error);
}
}
}
54 changes: 54 additions & 0 deletions apps/server/src/preload/static-file-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import http from 'http';
import path from 'path';
import { parse } from 'url';
import { Server as NodeStaticServer } from 'node-static';
import { IncomingMessage, ServerResponse } from 'http';

export class StaticFileServer {
private static instance: StaticFileServer;
private fileServer: NodeStaticServer;
private isPackaged: boolean;
private dirUi: string;

private constructor() {
this.isPackaged = process.env.isPackaged === 'true';
this.dirUi = this.isPackaged
? path.join(__dirname, '..', '..', 'data', 'ui')
: path.join(__dirname, '..', 'data', 'ui');
this.fileServer = new NodeStaticServer(this.dirUi);
}

public serveFile(req: IncomingMessage, res: ServerResponse): void {
this.fileServer.serve(req, res, (err: any | null, result: any) => {
if (err) {
console.error(`Error serving ${req.url} - ${err.message}`);
res.writeHead(err.status || 500, err.headers || {});
res.end();
} else {
console.log('UI server started');
console.log(JSON.stringify(parse(req.url || '', true).query));
}
});
}

public startServer(port: number): void {
const server = http.createServer((req, res) => this.serveFile(req, res));

server.on('listening', () => {
console.log(`Listening on port ${port}`);
});

server.on('error', (error: Error) => {
console.error(`[CRITICAL::ERROR]: Server error: ${error}`);
});

server.listen(port, '0.0.0.0');
}

public static getInstance(): StaticFileServer {
if (!StaticFileServer.instance) {
StaticFileServer.instance = new StaticFileServer();
}
return StaticFileServer.instance;
}
}
35 changes: 14 additions & 21 deletions apps/server/src/preload/ui-server.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
import http from 'http';
import * as path from 'path';
const serverStatic = require('node-static');
const dirUi =
process.env.isPackaged === 'true'
? path.join(__dirname, '..', '..', 'data', 'ui')
: path.join(__dirname, '..', 'data', 'ui');
const file = new serverStatic.Server(dirUi);
const url = require('url');
console.log('server started');
import { ServerManager } from './server-manager';

async function runServer() {
const portUi = process.env.uiPort ? Number(process.env.uiPort) : 4200;
http.createServer(function (req, res) {
file.serve(req, res);
console.log('server ui started');
console.log(url.parse(req.url, true).query);
}).listen(portUi, '0.0.0.0', () => {
console.log(`listen ui on port ${portUi}`);
});
return true;
class App {
public static main(): void {
try {
const portUi = process.env.UI_PORT ? Number(process.env.UI_PORT) : 4200;
const serverManager = new ServerManager();

serverManager.runServer(portUi);
} catch (error) {
console.error('[CRITICAL::ERROR]: Starting server:', error);
}
}
}

runServer();
// Call the function to start the server when this module is executed
App.main();
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const LOCAL_SERVER_UPDATE_CONFIG = {
FOLDER_PATH: '/',
PORT: 11999
PORT: 11999,
TEST_SSL_PORT: 11998
};
Loading

0 comments on commit 08df6bc

Please sign in to comment.