From 2fc4c9c3419a912b9a15945d83ea89e713867199 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 1 Feb 2022 02:51:43 +0100 Subject: [PATCH] cleanup index - check db version externally define required db version in cofig as constant some type updates --- backend/src/config/index.ts | 5 +++++ backend/src/server/createServer.ts | 34 +++++++++-------------------- backend/src/typeorm/DBVersion.ts | 29 ++++++++++++++++++++++++ backend/src/typeorm/getDBVersion.ts | 15 ------------- 4 files changed, 44 insertions(+), 39 deletions(-) create mode 100644 backend/src/typeorm/DBVersion.ts delete mode 100644 backend/src/typeorm/getDBVersion.ts diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 5d5324808..73bb80a4d 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -3,6 +3,10 @@ import dotenv from 'dotenv' dotenv.config() +const constants = { + DB_VERSION: '0012-login_user_backups_unify_wordlist', +} + const server = { PORT: process.env.PORT || 4000, JWT_SECRET: process.env.JWT_SECRET || 'secret123', @@ -67,6 +71,7 @@ const webhook = { process.env.APP_SECRET = server.JWT_SECRET const CONFIG = { + ...constants, ...server, ...database, ...klicktipp, diff --git a/backend/src/server/createServer.ts b/backend/src/server/createServer.ts index 1912d0b9e..1dbcf2f34 100644 --- a/backend/src/server/createServer.ts +++ b/backend/src/server/createServer.ts @@ -1,15 +1,12 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ -/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ - import 'reflect-metadata' import 'module-alias/register' import { ApolloServer } from 'apollo-server-express' -import express from 'express' +import express, { Express } from 'express' // database import connection from '../typeorm/connection' -import getDBVersion from '../typeorm/getDBVersion' +import { checkDBVersion } from '../typeorm/DBVersion' // server import cors from './cors' @@ -24,27 +21,25 @@ import schema from '../graphql/schema' // webhooks import { elopageWebhook } from '../webhook/elopage' +import { Connection } from 'typeorm' // TODO implement // import queryComplexity, { simpleEstimator, fieldConfigEstimator } from "graphql-query-complexity"; -const DB_VERSION = '0012-login_user_backups_unify_wordlist' +type ServerDef = { apollo: ApolloServer; app: Express; con: Connection } -const createServer = async (context: any = serverContext): Promise => { +// eslint-disable-next-line @typescript-eslint/no-explicit-any +const createServer = async (context: any = serverContext): Promise => { // open mysql connection const con = await connection() if (!con || !con.isConnected) { - throw new Error(`Couldn't open connection to database`) + throw new Error(`Fatal: Couldn't open connection to database`) } // check for correct database version - const dbVersion = await getDBVersion() - if (!dbVersion || dbVersion.indexOf(DB_VERSION) === -1) { - throw new Error( - `Wrong database version - the backend requires '${DB_VERSION}' but found '${ - dbVersion || 'None' - }'`, - ) + const dbVersion = await checkDBVersion(CONFIG.DB_VERSION) + if (!dbVersion) { + throw new Error('Fatal: Database Version incorrect') } // Express Server @@ -58,15 +53,6 @@ const createServer = async (context: any = serverContext): Promise => { // bodyparser urlencoded for elopage app.use(express.urlencoded({ extended: true })) - // Log every request - /* - app.use((req, res, next) => { - // eslint-disable-next-line no-console - console.log(req) - next() - }) - */ - // Elopage Webhook app.post('/hook/elopage/' + CONFIG.WEBHOOK_ELOPAGE_SECRET, elopageWebhook) diff --git a/backend/src/typeorm/DBVersion.ts b/backend/src/typeorm/DBVersion.ts new file mode 100644 index 000000000..79216fefb --- /dev/null +++ b/backend/src/typeorm/DBVersion.ts @@ -0,0 +1,29 @@ +import { getRepository } from 'typeorm' +import { Migration } from '@entity/Migration' + +const getDBVersion = async (): Promise => { + try { + const dbVersion = await getRepository(Migration).findOne({ order: { version: 'DESC' } }) + return dbVersion ? dbVersion.fileName : null + } catch (error) { + // eslint-disable-next-line no-console + console.log(error) + return null + } +} + +const checkDBVersion = async (DB_VERSION: string): Promise => { + const dbVersion = await getDBVersion() + if (!dbVersion || dbVersion.indexOf(DB_VERSION) === -1) { + // eslint-disable-next-line no-console + console.log( + `Wrong database version detected - the backend requires '${DB_VERSION}' but found '${ + dbVersion || 'None' + }`, + ) + return false + } + return true +} + +export { checkDBVersion, getDBVersion } diff --git a/backend/src/typeorm/getDBVersion.ts b/backend/src/typeorm/getDBVersion.ts deleted file mode 100644 index a64a6c2b7..000000000 --- a/backend/src/typeorm/getDBVersion.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { getRepository } from 'typeorm' -import { Migration } from '@entity/Migration' - -const getDBVersion = async (): Promise => { - try { - const dbVersion = await getRepository(Migration).findOne({ order: { version: 'DESC' } }) - return dbVersion ? dbVersion.fileName : null - } catch (error) { - // eslint-disable-next-line no-console - console.log(error) - return null - } -} - -export default getDBVersion