Added types and removed requires from app.ts

Signed-off-by: Yannick Bungers <git@innay.de>
Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
Yannick Bungers 2020-04-18 17:56:50 +02:00 committed by David Mehren
parent e0192b5652
commit 9c38a339df
No known key found for this signature in database
GPG key ID: 6017AF117F9756CB

View file

@ -21,7 +21,10 @@ import { logger } from './logger'
import { errors } from './errors' import { errors } from './errors'
import { addNonceToLocals, computeDirectives } from './csp' import { addNonceToLocals, computeDirectives } from './csp'
import { AuthRouter, BaseRouter, HistoryRouter, ImageRouter, NoteRouter, StatusRouter, UserRouter } from './web/' import { AuthRouter, BaseRouter, HistoryRouter, ImageRouter, NoteRouter, StatusRouter, UserRouter } from './web/'
import http from 'http'
import https from 'https'
import SocketIO from 'socket.io'
import WebSocket from 'ws'
// others // others
import { realtime } from './realtime' import { realtime } from './realtime'
@ -34,10 +37,9 @@ const app = express()
let server: any = null let server: any = null
if (config.useSSL) { if (config.useSSL) {
const ca = (function (): string[] { const ca = (function (): string[] {
let i, len
const results: string[] = [] const results: string[] = []
for (i = 0, len = config.sslCAPath.length; i < len; i++) { for (const path of config.sslCAPath) {
results.push(fs.readFileSync(config.sslCAPath[i], 'utf8')) results.push(fs.readFileSync(path, 'utf8'))
} }
return results return results
})() })()
@ -47,11 +49,13 @@ if (config.useSSL) {
ca: ca, ca: ca,
dhparam: fs.readFileSync(config.dhParamPath, 'utf8'), dhparam: fs.readFileSync(config.dhParamPath, 'utf8'),
requestCert: false, requestCert: false,
rejectUnauthorized: false rejectUnauthorized: false,
heartbeatInterval: config.heartbeatInterval,
heartbeatTimeout: config.heartbeatTimeout
} }
server = require('https').createServer(options, app) server = https.createServer(options, app)
} else { } else {
server = require('http').createServer(app) server = http.createServer(app)
} }
// logger // logger
@ -64,8 +68,8 @@ app.use(morgan('combined', {
})) }))
// socket io // socket io
const io = require('socket.io')(server) const io = SocketIO(server)
io.engine.ws = new (require('ws').Server)({ io.engine.ws = new WebSocket.Server({
noServer: true, noServer: true,
perMessageDeflate: false perMessageDeflate: false
}) })
@ -231,16 +235,13 @@ io.use(passportSocketIo.authorize({
success: realtime.onAuthorizeSuccess, success: realtime.onAuthorizeSuccess,
fail: realtime.onAuthorizeFail fail: realtime.onAuthorizeFail
})) }))
// socket.io heartbeat
io.set('heartbeat interval', config.heartbeatInterval)
io.set('heartbeat timeout', config.heartbeatTimeout)
// socket.io connection // socket.io connection
io.sockets.on('connection', realtime.connection) io.sockets.on('connection', realtime.connection)
// listen // listen
function startListen () { function startListen (): void {
let address let address
const listenCallback = function () { const listenCallback = function (): void {
const schema = config.useSSL ? 'HTTPS' : 'HTTP' const schema = config.useSSL ? 'HTTPS' : 'HTTP'
logger.info('%s Server listening at %s', schema, address) logger.info('%s Server listening at %s', schema, address)
realtime.maintenance = false realtime.maintenance = false
@ -261,8 +262,12 @@ sequelize.authenticate().then(function () {
// check if realtime is ready // check if realtime is ready
if (realtime.isReady()) { if (realtime.isReady()) {
Revision.checkAllNotesRevision(function (err, notes) { Revision.checkAllNotesRevision(function (err, notes) {
if (err) throw new Error(err) if (err) {
if (!notes || notes.length <= 0) return startListen() throw new Error(err)
}
if (!notes || notes.length <= 0) {
return startListen()
}
}) })
} else { } else {
throw new Error('server still not ready after db synced') throw new Error('server still not ready after db synced')
@ -278,7 +283,7 @@ process.on('uncaughtException', function (err) {
}) })
// install exit handler // install exit handler
function handleTermSignals () { function handleTermSignals (): void {
logger.info('CodiMD has been killed by signal, try to exit gracefully...') logger.info('CodiMD has been killed by signal, try to exit gracefully...')
realtime.maintenance = true realtime.maintenance = true
// disconnect all socket.io clients // disconnect all socket.io clients
@ -298,7 +303,9 @@ function handleTermSignals () {
const checkCleanTimer = setInterval(function () { const checkCleanTimer = setInterval(function () {
if (realtime.isReady()) { if (realtime.isReady()) {
Revision.checkAllNotesRevision(function (err, notes) { Revision.checkAllNotesRevision(function (err, notes) {
if (err) return logger.error(err) if (err) {
return logger.error(err)
}
if (!notes || notes.length <= 0) { if (!notes || notes.length <= 0) {
clearInterval(checkCleanTimer) clearInterval(checkCleanTimer)
return process.exit(0) return process.exit(0)