From fcde6c95484eac44af201eb441ea195520e1096c Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sun, 8 Dec 2019 17:29:26 +0100 Subject: [PATCH] Move db connect initialization --- src/server.js | 37 ++++++++++++++++++++++++++----------- src/utils/dataHolder.js | 5 ----- src/utils/scanner.js | 6 +++--- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/server.js b/src/server.js index 23a931f..6abf151 100644 --- a/src/server.js +++ b/src/server.js @@ -3,6 +3,7 @@ import helmet from 'helmet' import expressStaticGzip from 'express-static-gzip' import path from 'path' import morgan from 'morgan' +import mongoose from 'mongoose' import jsonfile from 'jsonfile' import { ServerRenderer } from './utils/serverRender' import { Scanner } from './utils/scanner' @@ -17,10 +18,6 @@ if (config == null) { throw new Error('Config file not found!') } -const dataHolder = new DataHolder(config) -const scanner = new Scanner(config, dataHolder) -scanner.watch() - app.use(morgan('common')) app.use(helmet.contentSecurityPolicy({ @@ -48,13 +45,31 @@ if (head == null) { } } +const dataHolder = new DataHolder(config) +const scanner = new Scanner(config, dataHolder) + const serverRenderer = new ServerRenderer(head, config, dataHolder) app.get('*', serverRenderer.render.bind(serverRenderer)) -app.listen(port, function (error) { - if (error) { - console.error(error) - } else { - console.info('[Server] Listening on port %s', port) - } -}) +if (config.storage === 'mongo') { + mongoose.connect(config.mongourl, { useNewUrlParser: true }) + const db = mongoose.connection + db.on('error', (error) => console.error(`[Server] Unable to connect to database\n${error}`)) + db.once('open', () => { + console.log('[Server] Connected to database') + startServer() + }) +} else { + startServer() +} + +function startServer () { + scanner.watch() + app.listen(port, function (error) { + if (error) { + console.error(`[Server] Unable to start server\n${error}`) + } else { + console.info(`[Server] Listening on port ${port}`) + } + }) +} diff --git a/src/utils/dataHolder.js b/src/utils/dataHolder.js index 78096fc..81fb447 100644 --- a/src/utils/dataHolder.js +++ b/src/utils/dataHolder.js @@ -13,11 +13,6 @@ export class DataHolder { other: {} } } else if (this.config.storage === 'mongo') { - mongoose.connect(config.mongourl, { useNewUrlParser: true }) - this.db = mongoose.connection - this.db.on('error', (error) => console.error(`[DataHolder] ${error}`)) - this.db.once('open', () => console.log('[DataHolder] Connected to database')) - this.Post = mongoose.model('Post', { filename: String, published: String, diff --git a/src/utils/scanner.js b/src/utils/scanner.js index 6ba7e9c..9509a39 100644 --- a/src/utils/scanner.js +++ b/src/utils/scanner.js @@ -19,15 +19,15 @@ export class Scanner { watcher .on('add', filepath => { - console.log(`[Watcher] File ${filepath} has been added`) + console.log(`[Scanner] File ${filepath} has been added`) this.addFile(filepath) }) .on('change', filepath => { - console.log(`[Watcher] File ${filepath} has been changed`) + console.log(`[Scanner] File ${filepath} has been changed`) this.updateFile(filepath) }) .on('unlink', filepath => { - console.log(`[Watcher] File ${filepath} has been removed`) + console.log(`[Scanner] File ${filepath} has been removed`) this.deleteFile(filepath) }) }