2018-12-29 22:07:50 +00:00
|
|
|
import express from 'express'
|
2019-10-19 20:03:31 +00:00
|
|
|
import helmet from 'helmet'
|
2019-03-27 18:10:50 +00:00
|
|
|
import expressStaticGzip from 'express-static-gzip'
|
2019-10-30 19:17:20 +00:00
|
|
|
import path from 'path'
|
2019-11-02 10:46:23 +00:00
|
|
|
import morgan from 'morgan'
|
2019-12-08 16:29:26 +00:00
|
|
|
import mongoose from 'mongoose'
|
2019-10-30 19:17:20 +00:00
|
|
|
import jsonfile from 'jsonfile'
|
2019-10-29 21:49:13 +00:00
|
|
|
import { ServerRenderer } from './utils/serverRender'
|
2019-02-20 20:27:34 +00:00
|
|
|
import { Scanner } from './utils/scanner'
|
2019-12-07 18:28:18 +00:00
|
|
|
import { DataHolder } from './utils/dataHolder'
|
2019-02-18 20:18:55 +00:00
|
|
|
|
2019-02-16 18:32:32 +00:00
|
|
|
const port = process.env.PORT || 3000
|
2018-12-29 22:07:50 +00:00
|
|
|
const app = express()
|
2019-11-03 14:51:26 +00:00
|
|
|
app.set('trust proxy', true)
|
2017-07-15 14:52:09 +00:00
|
|
|
|
2019-11-23 23:42:45 +00:00
|
|
|
const config = jsonfile.readFileSync(path.join(process.cwd(), 'config/config.json'))
|
2019-11-02 00:12:03 +00:00
|
|
|
if (config == null) {
|
|
|
|
throw new Error('Config file not found!')
|
|
|
|
}
|
|
|
|
|
2019-11-02 10:46:23 +00:00
|
|
|
app.use(morgan('common'))
|
|
|
|
|
2019-10-19 20:03:31 +00:00
|
|
|
app.use(helmet.contentSecurityPolicy({
|
|
|
|
directives: {
|
|
|
|
defaultSrc: ["'self'", `*.${config.baseUrl}`],
|
|
|
|
scriptSrc: ["'self'", "'unsafe-inline'", "'unsafe-eval'", `*.${config.baseUrl}`],
|
|
|
|
styleSrc: ["'self'", 'fonts.googleapis.com', 'fonts.gstatic.com', 'maxcdn.bootstrapcdn.com'],
|
|
|
|
fontSrc: ["'self'", 'fonts.googleapis.com', 'fonts.gstatic.com', 'maxcdn.bootstrapcdn.com'],
|
|
|
|
imgSrc: ['*'],
|
|
|
|
workerSrc: false,
|
|
|
|
blockAllMixedContent: true
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2019-03-27 18:10:50 +00:00
|
|
|
app.use('/static', expressStaticGzip('public/static'))
|
2019-01-21 18:21:51 +00:00
|
|
|
|
2019-02-16 16:56:43 +00:00
|
|
|
app.get('/favicon.ico', (req, res) => {
|
2019-12-07 18:34:28 +00:00
|
|
|
res.status(204).send('Not Found !!!')
|
2019-02-16 16:56:43 +00:00
|
|
|
})
|
|
|
|
|
2019-10-30 19:17:20 +00:00
|
|
|
let head = jsonfile.readFileSync(path.join(process.cwd(), 'config/head.json'))
|
|
|
|
if (head == null) {
|
|
|
|
head = {
|
2019-11-23 23:42:45 +00:00
|
|
|
scripts: []
|
2019-10-30 19:17:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 16:29:26 +00:00
|
|
|
const dataHolder = new DataHolder(config)
|
|
|
|
const scanner = new Scanner(config, dataHolder)
|
|
|
|
|
2019-12-07 20:36:53 +00:00
|
|
|
const serverRenderer = new ServerRenderer(head, config, dataHolder)
|
2019-10-30 19:17:20 +00:00
|
|
|
app.get('*', serverRenderer.render.bind(serverRenderer))
|
2017-04-09 19:09:27 +00:00
|
|
|
|
2019-12-08 16:29:26 +00:00
|
|
|
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}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|