Move db connect initialization

This commit is contained in:
LordMathis 2019-12-08 17:29:26 +01:00
parent c124cc842d
commit fcde6c9548
No known key found for this signature in database
GPG Key ID: 575849FD91CE470C
3 changed files with 29 additions and 19 deletions

View File

@ -3,6 +3,7 @@ import helmet from 'helmet'
import expressStaticGzip from 'express-static-gzip' import expressStaticGzip from 'express-static-gzip'
import path from 'path' import path from 'path'
import morgan from 'morgan' import morgan from 'morgan'
import mongoose from 'mongoose'
import jsonfile from 'jsonfile' import jsonfile from 'jsonfile'
import { ServerRenderer } from './utils/serverRender' import { ServerRenderer } from './utils/serverRender'
import { Scanner } from './utils/scanner' import { Scanner } from './utils/scanner'
@ -17,10 +18,6 @@ if (config == null) {
throw new Error('Config file not found!') 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(morgan('common'))
app.use(helmet.contentSecurityPolicy({ 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) const serverRenderer = new ServerRenderer(head, config, dataHolder)
app.get('*', serverRenderer.render.bind(serverRenderer)) app.get('*', serverRenderer.render.bind(serverRenderer))
app.listen(port, function (error) { if (config.storage === 'mongo') {
if (error) { mongoose.connect(config.mongourl, { useNewUrlParser: true })
console.error(error) const db = mongoose.connection
} else { db.on('error', (error) => console.error(`[Server] Unable to connect to database\n${error}`))
console.info('[Server] Listening on port %s', port) 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}`)
}
})
}

View File

@ -13,11 +13,6 @@ export class DataHolder {
other: {} other: {}
} }
} else if (this.config.storage === 'mongo') { } 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', { this.Post = mongoose.model('Post', {
filename: String, filename: String,
published: String, published: String,

View File

@ -19,15 +19,15 @@ export class Scanner {
watcher watcher
.on('add', filepath => { .on('add', filepath => {
console.log(`[Watcher] File ${filepath} has been added`) console.log(`[Scanner] File ${filepath} has been added`)
this.addFile(filepath) this.addFile(filepath)
}) })
.on('change', filepath => { .on('change', filepath => {
console.log(`[Watcher] File ${filepath} has been changed`) console.log(`[Scanner] File ${filepath} has been changed`)
this.updateFile(filepath) this.updateFile(filepath)
}) })
.on('unlink', filepath => { .on('unlink', filepath => {
console.log(`[Watcher] File ${filepath} has been removed`) console.log(`[Scanner] File ${filepath} has been removed`)
this.deleteFile(filepath) this.deleteFile(filepath)
}) })
} }