Skip to content

Commit

Permalink
update db service object
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Werfling committed Sep 15, 2023
1 parent ece4b08 commit 47b4ca7
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 72 deletions.
23 changes: 9 additions & 14 deletions backend/src/Routes/Main/Route/List.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DBHelper, DomainServiceDB, SshPortDB, SshUserDB} from 'flyingfish_core';
import {DBHelper, DomainServiceDB, NginxUpstreamServiceDB, SshPortDB, SshUserDB} from 'flyingfish_core';
import {
Location,
RouteData,
Expand All @@ -16,7 +16,6 @@ import {
NginxHttpVariableContextType
} from '../../../inc/Db/MariaDb/Entity/NginxHttpVariable.js';
import {NginxLocation as NginxLocationDB} from '../../../inc/Db/MariaDb/Entity/NginxLocation.js';
import {NginxUpstream as NginxUpstreamDB} from '../../../inc/Db/MariaDb/Entity/NginxUpstream.js';
import {NginxStream as NginxStreamDB} from '../../../inc/Db/MariaDb/Entity/NginxStream.js';

/**
Expand All @@ -32,7 +31,6 @@ export class List {
const sshportList: RouteSshPort[] = [];

const streamRepository = DBHelper.getRepository(NginxStreamDB);
const upstreamRepository = DBHelper.getRepository(NginxUpstreamDB);
const httpRepository = DBHelper.getRepository(NginxHttpDB);
const httpVariableRepository = DBHelper.getRepository(NginxHttpVariableDB);
const locationRepository = DBHelper.getRepository(NginxLocationDB);
Expand Down Expand Up @@ -69,11 +67,7 @@ export class List {
upstreams: []
};

const upstreams = await upstreamRepository.find({
where: {
stream_id: tstream.id
}
});
const upstreams = await NginxUpstreamServiceDB.getInstance().findAllStreams(tstream.id);

for (const aupstream of upstreams) {
streamEntry.upstreams.push({
Expand Down Expand Up @@ -118,7 +112,7 @@ export class List {
}
}

// http --------------------------------------------------------------------------------------------
// http ------------------------------------------------------------------------------------------------

const https = await httpRepository.find({
where: {
Expand Down Expand Up @@ -208,10 +202,11 @@ export class List {
});

if (sshport) {
location.ssh = {};
location.ssh.id = sshport.id;
location.ssh.port_out = sshport.port;
location.ssh.schema = alocation.sshport_schema;
location.ssh = {
id: sshport.id,
port_out: sshport.port,
schema: alocation.sshport_schema
};
}
}

Expand Down Expand Up @@ -239,7 +234,7 @@ export class List {
}
}

// load defaults -------------------------------------------------------------------------------------------
// load defaults -----------------------------------------------------------------------------------------------

const sshports = await sshportRepository.find();

Expand Down
8 changes: 2 additions & 6 deletions backend/src/Routes/Main/Route/Stream/Delete.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {DBHelper} from 'flyingfish_core';
import {DBHelper, NginxUpstreamServiceDB} from 'flyingfish_core';
import {DefaultReturn, RouteStreamDelete, StatusCodes} from 'flyingfish_schemas';
import {NginxStream as NginxStreamDB} from '../../../../inc/Db/MariaDb/Entity/NginxStream.js';
import {NginxUpstream as NginxUpstreamDB} from '../../../../inc/Db/MariaDb/Entity/NginxUpstream.js';
import {Save} from './Save.js';

/**
Expand All @@ -22,7 +21,6 @@ export class Delete {
}

const streamRepository = DBHelper.getRepository(NginxStreamDB);
const upstreamRepository = DBHelper.getRepository(NginxUpstreamDB);

const stream = await streamRepository.findOne({
where: {
Expand All @@ -49,9 +47,7 @@ export class Delete {

// delete upstreams ------------------------------------------------------------------------------------

await upstreamRepository.delete({
stream_id: stream.id
});
await NginxUpstreamServiceDB.getInstance().removeAllStreams(stream.id);

// delete stream ---------------------------------------------------------------------------------------

Expand Down
27 changes: 6 additions & 21 deletions backend/src/Routes/Main/Route/Stream/Save.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as bcrypt from 'bcrypt';
import {DBHelper, SshPortDB, SshUserDB} from 'flyingfish_core';
import {DBHelper, NginxUpstreamDB, NginxUpstreamServiceDB, SshPortDB, SshUserDB} from 'flyingfish_core';
import {DefaultReturn, RouteStreamSave, RouteStreamSSH, StatusCodes} from 'flyingfish_schemas';
import {Not} from 'typeorm';
import {NginxLocation as NginxLocationDB} from '../../../../inc/Db/MariaDb/Entity/NginxLocation.js';
Expand All @@ -8,7 +8,6 @@ import {
NginxStreamDestinationType,
NginxStreamSshR
} from '../../../../inc/Db/MariaDb/Entity/NginxStream.js';
import {NginxUpstream as NginxUpstreamDB} from '../../../../inc/Db/MariaDb/Entity/NginxUpstream.js';

/**
* SaveStream
Expand Down Expand Up @@ -240,8 +239,6 @@ export class Save {

// ---------------------------------------------------------------------------------------------------------

const upstreamRepository = DBHelper.getRepository(NginxUpstreamDB);

let aStream: NginxStreamDB|null = null;

if (data.stream.id !== 0) {
Expand Down Expand Up @@ -357,25 +354,17 @@ export class Save {

if (aStream.destination_listen_id > 0) {
// clear old upstreams
await upstreamRepository.delete({
stream_id: aStream.id
});
await NginxUpstreamServiceDB.getInstance().removeAllStreams(aStream.id);
} else if (data.stream.upstreams.length > 0) {
// remove delete upstreams -----------------------------------------------------------------------------
const tupstreams = await upstreamRepository.find({
where: {
stream_id: aStream.id
}
});
const tupstreams = await NginxUpstreamServiceDB.getInstance().findAllStreams(aStream.id);

if (tupstreams) {
const checkUpstreamExistence = (upstreamId: number): boolean => data.stream.upstreams.some(({id}) => id === upstreamId);

for await (const oldUpstream of tupstreams) {
if (!checkUpstreamExistence(oldUpstream.id)) {
await upstreamRepository.delete({
id: oldUpstream.id
});
await NginxUpstreamServiceDB.getInstance().remove(oldUpstream.id);
}
}
}
Expand All @@ -387,11 +376,7 @@ export class Save {
let aNewUpstream: NginxUpstreamDB|null = null;

if (aUpstream.id > 0) {
const tUpstream = await upstreamRepository.findOne({
where: {
id: aUpstream.id
}
});
const tUpstream = await NginxUpstreamServiceDB.getInstance().findOne(aUpstream.id);

if (tUpstream) {
aNewUpstream = tUpstream;
Expand All @@ -408,7 +393,7 @@ export class Save {
aNewUpstream.index = index;
aNewUpstream.proxy_protocol_out = aUpstream.proxy_protocol_out;

await DBHelper.getDataSource().manager.save(aNewUpstream);
await NginxUpstreamServiceDB.getInstance().save(aNewUpstream);

index++;
}
Expand Down
5 changes: 2 additions & 3 deletions backend/src/inc/Db/MariaDb/DBSetup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as bcrypt from 'bcrypt';
import {DBHelper, DomainDB, DomainServiceDB, Logger, UserDB, UserServiceDB} from 'flyingfish_core';
import {DBHelper, DomainDB, DomainServiceDB, Logger, NginxUpstreamDB, UserDB, UserServiceDB, NginxUpstreamServiceDB} from 'flyingfish_core';
import {ListenCategory, ListenProtocol, ListenTypes, NginxListen as NginxListenDB} from './Entity/NginxListen.js';
import {NginxStream as NginxStreamDB, NginxStreamDestinationType} from './Entity/NginxStream.js';
import {NginxUpstream as NginxUpstreamDB} from './Entity/NginxUpstream.js';

/**
* DBSetup
Expand Down Expand Up @@ -137,7 +136,7 @@ export class DBSetup {
sTo5333Up.destination_address = '127.0.0.1';
sTo5333Up.destination_port = 5333;

await DBHelper.getDataSource().manager.save(sTo5333Up);
await NginxUpstreamServiceDB.getInstance().save(sTo5333Up);

const sTo10443 = new NginxStreamDB();
sTo10443.domain_id = defaultDomain.id;
Expand Down
18 changes: 10 additions & 8 deletions backend/src/inc/Service/NginxService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import {DBHelper, DomainServiceDB, FileHelper, Logger, SshPortDB} from 'flyingfish_core';
import {
DBHelper,
DomainServiceDB,
FileHelper,
Logger,
NginxUpstreamDB,
NginxUpstreamServiceDB,
SshPortDB
} from 'flyingfish_core';
import fs from 'fs/promises';
import path from 'path';
import {SchemaErrors} from 'vts';
Expand All @@ -22,7 +30,6 @@ import {
NginxStreamDestinationType,
NginxStreamSshR
} from '../Db/MariaDb/Entity/NginxStream.js';
import {NginxUpstream as NginxUpstreamDB} from '../Db/MariaDb/Entity/NginxUpstream.js';
import {Context} from '../Nginx/Config/Context.js';
import {If} from '../Nginx/Config/If.js';
import {Listen, ListenProtocol} from '../Nginx/Config/Listen.js';
Expand Down Expand Up @@ -311,7 +318,6 @@ export class NginxService {

const listenRepository = DBHelper.getRepository(NginxListenDB);
const streamRepository = DBHelper.getRepository(NginxStreamDB);
const upstreamRepository = DBHelper.getRepository(NginxUpstreamDB);
const httpRepository = DBHelper.getRepository(NginxHttpDB);
const httpVariableRepository = DBHelper.getRepository(NginxHttpVariableDB);
const locationRepository = DBHelper.getRepository(NginxLocationDB);
Expand Down Expand Up @@ -350,11 +356,7 @@ export class NginxService {
upstreams: []
};

const upstreams = await upstreamRepository.find({
where: {
stream_id: astream.id
}
});
const upstreams = await NginxUpstreamServiceDB.getInstance().findAllStreams(astream.id);

if (upstreams) {
streamCollection.upstreams = upstreams;
Expand Down
2 changes: 0 additions & 2 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {IpListMaintainer as IpListMaintainerDB} from './inc/Db/MariaDb/Entity/Ip
import {IpLocation as IpLocationDB} from './inc/Db/MariaDb/Entity/IpLocation.js';
import {IpWhitelist as IpWhitelistDB} from './inc/Db/MariaDb/Entity/IpWhitelist.js';
import {NginxHttpVariable as NginxHttpVariableDB} from './inc/Db/MariaDb/Entity/NginxHttpVariable.js';
import {NginxUpstream as NginxUpstreamDB} from './inc/Db/MariaDb/Entity/NginxUpstream.js';
import {Dns2Server} from './inc/Dns/Dns2Server.js';
import {SchemaFlyingFishArgs} from './inc/Env/Args.js';
import {BlacklistService} from './inc/Service/BlacklistService.js';
Expand Down Expand Up @@ -117,7 +116,6 @@ import exitHook from 'async-exit-hook';

entities.push(NginxListenDB);
entities.push(NginxStreamDB);
entities.push(NginxUpstreamDB);
entities.push(NginxHttpDB);
entities.push(NginxHttpVariableDB);
entities.push(NginxLocationDB);
Expand Down
2 changes: 2 additions & 0 deletions core/src/inc/Db/MariaDb/DBEntitiesLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {DynDnsServerUser} from './Entity/DynDnsServerUser.js';
import {GatewayIdentifier} from './Entity/GatewayIdentifier.js';
import {IpBlacklist} from './Entity/IpBlacklist.js';
import {NatPort} from './Entity/NatPort.js';
import {NginxUpstream} from './Entity/NginxUpstream.js';
import {Settings} from './Entity/Settings.js';
import {SshPort} from './Entity/SshPort.js';
import {SshUser} from './Entity/SshUser.js';
Expand Down Expand Up @@ -40,6 +41,7 @@ export class DBEntitiesLoader {
GatewayIdentifier,
IpBlacklist,
NatPort,
NginxUpstream,
Settings,
SshPort,
SshUser,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import {BaseEntity, Column, Entity, Index, PrimaryGeneratedColumn} from 'typeorm';
import {DBBaseEntityId} from '../DBBaseEntityId.js';
import {Column, Entity, Index} from 'typeorm';

/**
* Nginx Stream Entity
*/
@Entity({name: 'nginx_upstream'})
export class NginxUpstream extends BaseEntity {

/**
* id
*/
@PrimaryGeneratedColumn()
public id!: number;
export class NginxUpstream extends DBBaseEntityId {

/**
* stream id
Expand Down
50 changes: 50 additions & 0 deletions core/src/inc/Db/MariaDb/NginxUpstreamService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {DBService} from './DBService.js';
import {NginxUpstream} from './Entity/NginxUpstream.js';
import {DeleteResult} from 'typeorm';

/**
* Nginx upstream service object.
*/
export class NginxUpstreamService extends DBService<NginxUpstream> {

/**
* register name
*/
public static REGISTER_NAME = 'nginx_upstream';

/**
* Return an intance from nginx upstream service.
* @returns {NginxUpstreamService}
*/
public static getInstance(): NginxUpstreamService {
return DBService.getSingleInstance(
NginxUpstreamService,
NginxUpstream,
NginxUpstreamService.REGISTER_NAME
);
}

/**
* Find all Upstreams by stream id.
* @param {number} streamId - ID of a stream.
* @returns {NginxUpstream[]}
*/
public async findAllStreams(streamId: number): Promise<NginxUpstream[]> {
return this._repository.find({
where: {
stream_id: streamId
}
});
}

/**
* Remove all Upstreams stream id.
* @param {number} streamId - ID of a stream.
*/
public async removeAllStreams(streamId: number): Promise<DeleteResult> {
return this._repository.delete({
stream_id: streamId
});
}

}
13 changes: 6 additions & 7 deletions core/src/inc/PluginSystem/PluginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class PluginManager {
protected _appPath: string;

/**
* service name (name of the system in which the plugin works)
* @protected
* Service name from service instance (name of the system in which the plugin works).
* @member {string}
*/
protected _serviceName: string;

Expand All @@ -46,7 +46,7 @@ export class PluginManager {

/**
* events
* @
* @member {IPluginEvent[]}
*/
protected _events: IPluginEvent[] = [];

Expand All @@ -63,9 +63,8 @@ export class PluginManager {
}

/**
* constructor
* @param {string} serviceName - Service name, name who start the plugin manager.
* @param {string} appPath - Path to modules directory.
* @param {string} serviceName - Service name, name who starts the plugin manager.
* @param {string} appPath - Path-to-modules directory.
*/
public constructor(serviceName: string, appPath?: string) {
if (appPath) {
Expand Down Expand Up @@ -216,7 +215,7 @@ export class PluginManager {

/**
* getAllEvents
* @param aInterface
* @param {extends IPluginEvent} aInterface
*/
// eslint-disable-next-line @typescript-eslint/ban-types
public getAllEvents<T extends IPluginEvent>(aClass: Function): T[] {
Expand Down
2 changes: 2 additions & 0 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {DynDnsServerUser as DynDnsServerUserDB} from './inc/Db/MariaDb/Entity/Dy
export {GatewayIdentifier as GatewayIdentifierDB} from './inc/Db/MariaDb/Entity/GatewayIdentifier.js';
export {IpBlacklist as IpBlacklistDB} from './inc/Db/MariaDb/Entity/IpBlacklist.js';
export {NatPort as NatPortDB} from './inc/Db/MariaDb/Entity/NatPort.js';
export {NginxUpstream as NginxUpstreamDB} from './inc/Db/MariaDb/Entity/NginxUpstream.js';
export {Settings as SettingsDB} from './inc/Db/MariaDb/Entity/Settings.js';
export {SshUser as SshUserDB} from './inc/Db/MariaDb/Entity/SshUser.js';
export {SshPort as SshPortDB} from './inc/Db/MariaDb/Entity/SshPort.js';
Expand All @@ -42,6 +43,7 @@ export {DynDnsServerDomainService as DynDnsServerDomainServiceDB} from './inc/Db
export {GatewayIdentifierService as GatewayIdentifierServiceDB} from './inc/Db/MariaDb/GatewayIdentifierService.js';
export {IpBlacklistService as IpBlacklistServiceDB} from './inc/Db/MariaDb/IpBlacklistService.js';
export {NatPortService as NatPortServiceDB} from './inc/Db/MariaDb/NatPortService.js';
export {NginxUpstreamService as NginxUpstreamServiceDB} from './inc/Db/MariaDb/NginxUpstreamService.js';
export {SettingService as SettingServiceDB} from './inc/Db/MariaDb/SettingService.js';
export {UserService as UserServiceDB} from './inc/Db/MariaDb/UserService.js';

Expand Down

0 comments on commit 47b4ca7

Please sign in to comment.