-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
88 lines (79 loc) · 2.65 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const express = require('express')
const morgan = require('morgan')
const fileStreamRotator = require('file-stream-rotator')
const session = require('express-session')
const Grant = require('grant').express()
const path = require('path');
const https = require('https')
const pem = require('pem')
const router = require('./routes/index')
const fs = require('fs')
const md = require('markdown-it')({
html: true,
linkify: true,
langPrefix: 'highlight-',
typographer: true
}).use(require('markdown-it-anchor'), {
permalink: true
})
// (html linkify langPrefix='highlight-' plugins=['markdown-it-anchor'])
// Configuration
const fpathAppConfig = path.join(__dirname, 'app-config.json')
const appConfig = JSON.parse(fs.readFileSync(fpathAppConfig, "utf8"))
const environment = appConfig['environment']
// Fail if the configured environment is invalid
if (!Object.keys(appConfig.environmentConfigs).includes(environment)) {
let msg = `"${environment}" is an invalid environment. Valid environments are: ${Object.keys(appConfig.environmentConfigs).join(', ')}`
throw msg
}
// Initialize environment config
const environmentConfig = appConfig.environmentConfigs[environment]
console.log(`Environment (${environment}) configuration: `, environmentConfig)
const PORT = environmentConfig.port
const USE_HTTPS = environmentConfig.https
const GRANT_CONFIG_FILENAME = path.join(__dirname, environmentConfig.grant.config_filename)
// Initialize Grant
const grant = Grant(require(GRANT_CONFIG_FILENAME))
const logConfig = {
logConfig: {
logRoute: true,
logErrors: true
}
}
// Web App
const app = express()
app.use(express.json())
// Create a rotating write stream
filePath = 'logs/%DATE%.log'
stream = fileStreamRotator.getStream({
date_format: 'YYYYMMDD',
filename: filePath,
frequency: 'daily',
verbose: false
})
app.use(morgan('combined', { stream: stream }));
app.set('view engine', 'pug')
.use(session({ secret: 'grant', saveUninitialized: true, resave: false }))
.use(grant)
.use(express.static('public'))
.use(express.static('build/ui'))
.use('/', router(grant, environmentConfig, logConfig))
app.locals.filters = {
'my-md': function(text, options) {
return md.render(text)
}
};
if (USE_HTTPS) {
console.log("USING HTTPS")
// Enable HTTPS / Create certificate
pem.createCertificate({ days: 1, selfSigned: true }, function (err, keys) {
console.log("created certificate")
if (err) {
throw err
}
https.createServer({ key: keys.serviceKey, cert: keys.certificate }, app).listen(PORT)
})
} else {
console.log("USING HTTP")
app.listen(PORT)
}