namesny-com/src/server.js

92 lines
2.6 KiB
JavaScript
Raw Normal View History

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-26 20:22:18 +00:00
import { FileStorage } from './utils/storage/file'
import { MongoStorage } from './utils/storage/mongo'
2020-05-09 12:37:48 +00:00
import { Config } from './utils/config'
2020-01-07 21:39:04 +00:00
import { Api } from './utils/api'
2019-02-18 20:18:55 +00:00
2020-05-09 10:22:35 +00:00
const configPath = process.argv[2] || path.join(process.cwd(), 'config/config.json')
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
2020-05-09 12:37:48 +00:00
const config = new Config(jsonfile.readFileSync(configPath))
2019-11-02 00:12:03 +00:00
if (config == null) {
throw new Error('Config file not found!')
}
2020-05-09 10:22:35 +00:00
const port = config.port || 3000
2019-11-02 00:12:03 +00:00
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-26 20:22:18 +00:00
let storage
if (config.storage === 'file') {
2019-12-30 15:11:47 +00:00
storage = new FileStorage(config)
2019-12-26 20:22:18 +00:00
} else if (config.storage === 'mongo') {
2019-12-30 15:11:47 +00:00
storage = new MongoStorage(config)
2019-12-26 20:22:18 +00:00
}
2020-01-08 21:28:57 +00:00
if (config.storage === 'mongo') {
const postApi = new Api(storage)
app.get('/api/v1/posts', postApi.getPosts.bind(postApi))
}
2020-01-07 21:39:04 +00:00
2019-12-30 15:11:47 +00:00
const scanner = new Scanner(config, storage)
2019-12-08 16:29:26 +00:00
2019-12-30 15:11:47 +00:00
const serverRenderer = new ServerRenderer(head, config, storage)
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}`)
}
})
}