Move db connect initialization
This commit is contained in:
parent
c124cc842d
commit
fcde6c9548
|
@ -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))
|
||||||
|
|
||||||
|
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) {
|
app.listen(port, function (error) {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error(error)
|
console.error(`[Server] Unable to start server\n${error}`)
|
||||||
} else {
|
} else {
|
||||||
console.info('[Server] Listening on port %s', port)
|
console.info(`[Server] Listening on port ${port}`)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue