Skip to content

Commit

Permalink
#21 DB service object
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Werfling committed Sep 25, 2023
1 parent d0b28c1 commit 0d7ad0e
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 116 deletions.
10 changes: 2 additions & 8 deletions backend/src/Routes/Main/Domain/Delete.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {DBHelper, DomainRecordDB, DomainServiceDB, NginxStreamServiceDB} from 'flyingfish_core';
import {DBHelper, DomainRecordDB, DomainServiceDB, NginxHttpServiceDB, NginxStreamServiceDB} from 'flyingfish_core';
import {DomainDelete, DomainDeleteResponse, StatusCodes} from 'flyingfish_schemas';
import {NginxHttp as NginxHttpDB} from '../../../inc/Db/MariaDb/Entity/NginxHttp.js';

/**
* Delete
Expand All @@ -13,7 +12,6 @@ export class Delete {
*/
public static async deleteDomain(data: DomainDelete): Promise<DomainDeleteResponse> {
const domainRecordRepository = DBHelper.getRepository(DomainRecordDB);
const httpRepository = DBHelper.getRepository(NginxHttpDB);

const domain = await DomainServiceDB.getInstance().findOne(data.id);

Expand All @@ -27,11 +25,7 @@ export class Delete {

const countStreams = await NginxStreamServiceDB.getInstance().countByDomain(domain.id);

const countHttps = await httpRepository.count({
where: {
domain_id: domain.id
}
});
const countHttps = await NginxHttpServiceDB.getInstance().countByDomain(domain.id);

if ((countStreams > 0) || (countHttps > 0)) {
return {
Expand Down
10 changes: 2 additions & 8 deletions backend/src/Routes/Main/Listen/Delete.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {DBHelper, NginxStreamServiceDB} from 'flyingfish_core';
import {DBHelper, NginxHttpServiceDB, NginxStreamServiceDB} from 'flyingfish_core';
import {DefaultReturn, ListenDelete, StatusCodes} from 'flyingfish_schemas';
import {NginxHttp as NginxHttpDB} from '../../../inc/Db/MariaDb/Entity/NginxHttp.js';
import {NginxListen as NginxListenDB} from '../../../inc/Db/MariaDb/Entity/NginxListen.js';

/**
Expand All @@ -14,7 +13,6 @@ export class Delete {
*/
public static async deleteListen(data: ListenDelete): Promise<DefaultReturn> {
const listenRepository = DBHelper.getRepository(NginxListenDB);
const httpRepository = DBHelper.getRepository(NginxHttpDB);

const tListen = await listenRepository.findOne({
where: {
Expand All @@ -32,11 +30,7 @@ export class Delete {

const countStream = await NginxStreamServiceDB.getInstance().countByListen(tListen.id);

const countHttp = await httpRepository.count({
where: {
listen_id: tListen.id
}
});
const countHttp = await NginxHttpServiceDB.getInstance().countByListen(tListen.id);

if ((countStream > 0) || (countHttp > 0)) {
return {
Expand Down
14 changes: 3 additions & 11 deletions backend/src/Routes/Main/Route/Http/Delete.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {DBHelper} from 'flyingfish_core';
import {DBHelper, NginxHttpServiceDB} from 'flyingfish_core';
import {DefaultReturn, RouteHttpDelete, StatusCodes} from 'flyingfish_schemas';
import {NginxHttp as NginxHttpDB} from '../../../../inc/Db/MariaDb/Entity/NginxHttp.js';
import {NginxLocation as NginxLocationDB} from '../../../../inc/Db/MariaDb/Entity/NginxLocation.js';

/**
Expand All @@ -20,23 +19,16 @@ export class Delete {
};
}

const httpRepository = DBHelper.getRepository(NginxHttpDB);
const locationRepository = DBHelper.getRepository(NginxLocationDB);

const http = await httpRepository.findOne({
where: {
id: data.id
}
});
const http = await NginxHttpServiceDB.getInstance().findOne(data.id);

if (http) {
await locationRepository.delete({
http_id: http.id
});

const result = await httpRepository.delete({
id: http.id
});
const result = await NginxHttpServiceDB.getInstance().remove(http.id);

if (result) {
return {
Expand Down
19 changes: 4 additions & 15 deletions backend/src/Routes/Main/Route/Http/Save.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {DBHelper} from 'flyingfish_core';
import {DBHelper, NginxHttpDB, NginxHttpServiceDB} from 'flyingfish_core';
import {DefaultReturn, RouteHttpSave, StatusCodes} from 'flyingfish_schemas';
import {
NginxHttpVariable as NginxHttpVariableDB,
NginxHttpVariableContextType
} from '../../../../inc/Db/MariaDb/Entity/NginxHttpVariable.js';
import {NginxHTTPVariables} from '../../../../inc/Nginx/NginxVariables.js';
import {NginxHttp as NginxHttpDB} from '../../../../inc/Db/MariaDb/Entity/NginxHttp.js';
import {NginxLocation as NginxLocationDB} from '../../../../inc/Db/MariaDb/Entity/NginxLocation.js';

/**
Expand Down Expand Up @@ -36,30 +35,20 @@ export class Save {

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

const httpRepository = DBHelper.getRepository(NginxHttpDB);
const httpVariableRepository = DBHelper.getRepository(NginxHttpVariableDB);
const locationRepository = DBHelper.getRepository(NginxLocationDB);

let aHttp: NginxHttpDB|null = null;

if (data.http.id > 0) {
const tHttp = await httpRepository.findOne({
where: {
id: data.http.id
}
});
const tHttp = await NginxHttpServiceDB.getInstance().findOne(data.http.id);

if (tHttp) {
aHttp = tHttp;
}
}

const oHttp = await httpRepository.findOne({
where: {
listen_id: data.http.listen_id,
domain_id: data.domainid
}
});
const oHttp = await NginxHttpServiceDB.getInstance().findBy(data.http.listen_id, data.domainid);

if (oHttp) {
if (!aHttp || aHttp.id !== oHttp.id) {
Expand All @@ -84,7 +73,7 @@ export class Save {
aHttp.x_frame_options = data.http.x_frame_options;
aHttp.wellknown_disabled = data.http.wellknown_disabled;

aHttp = await DBHelper.getDataSource().manager.save(aHttp);
aHttp = await NginxHttpServiceDB.getInstance().save(aHttp);

// save variables ----------------------------------------------------------------------------------------------

Expand Down
10 changes: 2 additions & 8 deletions backend/src/Routes/Main/Route/List.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
DBHelper,
DomainServiceDB,
DomainServiceDB, NginxHttpServiceDB,
NginxStreamServiceDB,
NginxUpstreamServiceDB,
SshPortDB,
Expand All @@ -17,7 +17,6 @@ import {
StatusCodes
} from 'flyingfish_schemas';
import {Config} from '../../../inc/Config/Config.js';
import {NginxHttp as NginxHttpDB} from '../../../inc/Db/MariaDb/Entity/NginxHttp.js';
import {
NginxHttpVariable as NginxHttpVariableDB,
NginxHttpVariableContextType
Expand All @@ -36,7 +35,6 @@ export class List {
const list: RouteData[] = [];
const sshportList: RouteSshPort[] = [];

const httpRepository = DBHelper.getRepository(NginxHttpDB);
const httpVariableRepository = DBHelper.getRepository(NginxHttpVariableDB);
const locationRepository = DBHelper.getRepository(NginxLocationDB);
const sshportRepository = DBHelper.getRepository(SshPortDB);
Expand Down Expand Up @@ -115,11 +113,7 @@ export class List {

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

const https = await httpRepository.find({
where: {
domain_id: adomain.id
}
});
const https = await NginxHttpServiceDB.getInstance().findAllByDomain(adomain.id);

if (https) {
for await (const thttp of https) {
Expand Down
11 changes: 2 additions & 9 deletions backend/src/Routes/Main/Ssl/Details.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {DBHelper, DomainServiceDB} from 'flyingfish_core';
import {DomainServiceDB, NginxHttpServiceDB} from 'flyingfish_core';
import {SslDetailInfoData, SslDetailsRequest, SslDetailsResponse, StatusCodes} from 'flyingfish_schemas';
import Path from 'path';
import {Certificate} from '../../../inc/Cert/Certificate.js';
import {NginxHttp as NginxHttpDB} from '../../../inc/Db/MariaDb/Entity/NginxHttp.js';
import {Certbot} from '../../../inc/Provider/Letsencrypt/Certbot.js';

/**
Expand All @@ -15,13 +14,7 @@ export class Details {
* @param data
*/
public static async getCertDetails(data: SslDetailsRequest): Promise<SslDetailsResponse> {
const httpRepository = DBHelper.getRepository(NginxHttpDB);

const http = await httpRepository.findOne({
where: {
id: data.httpid
}
});
const http = await NginxHttpServiceDB.getInstance().findOne(data.httpid);

if (http) {
if (!http.ssl_enable) {
Expand Down
10 changes: 2 additions & 8 deletions backend/src/inc/Service/NginxService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
DBHelper,
DomainServiceDB,
FileHelper,
Logger, NginxStreamDB, NginxStreamServiceDB,
Logger, NginxHttpDB, NginxHttpServiceDB, NginxStreamDB, NginxStreamServiceDB,
NginxUpstreamDB,
NginxUpstreamServiceDB,
SshPortDB
Expand All @@ -14,7 +14,6 @@ import {SchemaErrors} from 'vts';
import {Config} from '../Config/Config.js';
import {NginxHttpAccess as NginxHttpAccessInfluxDB} from '../Db/InfluxDb/Entity/NginxHttpAccess.js';
import {NginxStreamAccess as NginxStreamAccessInfluxDB} from '../Db/InfluxDb/Entity/NginxStreamAccess.js';
import {NginxHttp as NginxHttpDB} from '../Db/MariaDb/Entity/NginxHttp.js';
import {
NginxHttpVariable as NginxHttpVariableDB,
NginxHttpVariableContextType
Expand Down Expand Up @@ -313,7 +312,6 @@ export class NginxService {
// read db -----------------------------------------------------------------------------------------------------

const listenRepository = DBHelper.getRepository(NginxListenDB);
const httpRepository = DBHelper.getRepository(NginxHttpDB);
const httpVariableRepository = DBHelper.getRepository(NginxHttpVariableDB);
const locationRepository = DBHelper.getRepository(NginxLocationDB);
const sshportRepository = DBHelper.getRepository(SshPortDB);
Expand Down Expand Up @@ -385,11 +383,7 @@ export class NginxService {
} else if (alisten.listen_type === ListenTypes.http) {
// read http by db -----------------------------------------------------------------------------

const https = await httpRepository.find({
where: {
listen_id: alisten.id
}
});
const https = await NginxHttpServiceDB.getInstance().findAllByListen(alisten.id);

for await (const http of https) {
const adomain = await DomainServiceDB.getInstance().findOne(http.domain_id);
Expand Down
37 changes: 5 additions & 32 deletions backend/src/inc/Service/SslCertService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Ets} from 'ets';
import {DateHelper, DBHelper, DomainServiceDB, FileHelper, Logger} from 'flyingfish_core';
import {DomainServiceDB, FileHelper, Logger, NginxHttpServiceDB} from 'flyingfish_core';
import {DomainCheckReachability, SchemaDomainCheckReachability} from 'flyingfish_schemas';
import fs from 'fs/promises';
import got from 'got';
Expand All @@ -8,7 +8,6 @@ import Path from 'path';
import {v4 as uuid} from 'uuid';
import {SchemaErrors} from 'vts';
import {Certificate} from '../Cert/Certificate.js';
import {NginxHttp as NginxHttpDB} from '../Db/MariaDb/Entity/NginxHttp.js';
import {NginxServer} from '../Nginx/NginxServer.js';
import {Certbot} from '../Provider/Letsencrypt/Certbot.js';
import {NginxService} from './NginxService.js';
Expand Down Expand Up @@ -119,9 +118,7 @@ export class SslCertService {
public async update(): Promise<void> {
this._inProcess = true;

const httpRepository = DBHelper.getRepository(NginxHttpDB);

const https = await httpRepository.find();
const https = await NginxHttpServiceDB.getInstance().findAll();

const certbot = new Certbot();
let reloadNginx = false;
Expand All @@ -147,15 +144,7 @@ export class SslCertService {
} else if (certbot.isOverLimitAndTime(http.cert_create_attempts, http.cert_last_request)) {
Logger.getLogger().info(`SslCertService::update: time over, rest attempts for cert request for domain: ${domain.domainname}`);

await httpRepository
.createQueryBuilder()
.update()
.set({
cert_create_attempts: 0,
cert_last_request: DateHelper.getCurrentDbTime()
})
.where('id = :id', {id: http.id})
.execute();
await NginxHttpServiceDB.getInstance().updateLastCertReq(http.id, 0);
}

// ---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -228,25 +217,9 @@ export class SslCertService {
// -----------------------------------------------------------------------------------------

if (isCreateFailed) {
await httpRepository
.createQueryBuilder()
.update()
.set({
cert_create_attempts: http.cert_create_attempts + 1,
cert_last_request: DateHelper.getCurrentDbTime()
})
.where('id = :id', {id: http.id})
.execute();
await NginxHttpServiceDB.getInstance().updateLastCertReq(http.id, http.cert_create_attempts + 1);
} else if (isCreate) {
await httpRepository
.createQueryBuilder()
.update()
.set({
cert_create_attempts: 0,
cert_last_request: DateHelper.getCurrentDbTime()
})
.where('id = :id', {id: http.id})
.execute();
await NginxHttpServiceDB.getInstance().updateLastCertReq(http.id, 0);
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {DynDnsServer as DynDnsServerController} from './Routes/Main/DynDnsServer
import {Config} from './inc/Config/Config.js';
import {v4 as uuid} from 'uuid';
import {DBSetup} from './inc/Db/MariaDb/DBSetup.js';
import {NginxHttp as NginxHttpDB} from './inc/Db/MariaDb/Entity/NginxHttp.js';
import {NginxListen as NginxListenDB} from './inc/Db/MariaDb/Entity/NginxListen.js';
import {NginxLocation as NginxLocationDB} from './inc/Db/MariaDb/Entity/NginxLocation.js';
import {NginxServer} from './inc/Nginx/NginxServer.js';
Expand Down Expand Up @@ -109,7 +108,6 @@ import exitHook from 'async-exit-hook';
const entities: MixedList<Function | string | EntitySchema> = await DBEntitiesLoader.loadEntities() as [];

entities.push(NginxListenDB);
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 @@ -15,6 +15,7 @@ import {IpListMaintainer} from './Entity/IpListMaintainer.js';
import {IpLocation} from './Entity/IpLocation.js';
import {IpWhitelist} from './Entity/IpWhitelist.js';
import {NatPort} from './Entity/NatPort.js';
import {NginxHttp} from './Entity/NginxHttp.js';
import {NginxStream} from './Entity/NginxStream.js';
import {NginxUpstream} from './Entity/NginxUpstream.js';
import {Settings} from './Entity/Settings.js';
Expand Down Expand Up @@ -52,6 +53,7 @@ export class DBEntitiesLoader {
IpLocation,
IpWhitelist,
NatPort,
NginxHttp,
NginxStream,
NginxUpstream,
Settings,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import {BaseEntity, Column, Entity, Index, PrimaryGeneratedColumn} from 'typeorm';
import {ServerXFrameOptions} from '../../../Nginx/Config/Server.js';
import {Column, Entity, Index} from 'typeorm';
import {DBBaseEntityId} from '../DBBaseEntityId.js';

/**
* NginxHttp
*/
@Entity({name: 'nginx_http'})
export class NginxHttp extends BaseEntity {

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

/**
* domain id
Expand Down Expand Up @@ -95,7 +86,7 @@ export class NginxHttp extends BaseEntity {
* x-frame-options
*/
@Column({
default: ServerXFrameOptions.deny
default: 'DENY'
})
public x_frame_options!: string;

Expand Down
Loading

0 comments on commit 0d7ad0e

Please sign in to comment.