forked from joemccann/dillinger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
141 lines (117 loc) · 3.73 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Main Application File for Dillinger.
*/
'use strict'
const config = require('./config')(),
connect = require('connect'),
methodOverride = require('method-override'),
logger = require('morgan'),
favicon = require('serve-favicon'),
compress = require('compression'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
cookieSession = require('cookie-session'),
express = require('express'),
netjet = require('netjet'),
routes = require('./routes'),
serveStatic = require('serve-static'),
errorHandler = require('errorhandler'),
path = require('path'),
fs = require('fs'),
app = express(),
core = require('./plugins/core/server.js'),
dropbox = require('./plugins/dropbox/server.js'),
bitbucket = require('./plugins/bitbucket/server.js'),
github = require('./plugins/github/server.js'),
medium = require('./plugins/medium/server.js'),
googledrive = require('./plugins/googledrive/server.js'),
onedrive = require('./plugins/onedrive/server.js'),
env = process.env.NODE_ENV || 'development';
require('isomorphic-fetch') /* patch global fetch for dropbox module*/
app.set('port', process.env.PORT || 8080)
app.set('bind-address', process.env.BIND_ADDRESS || 'localhost')
app.set('views', __dirname + '/views')
app.set('view engine', 'ejs')
// // Required to trust GCP proxy for the
// x-forwarded-by heading
app.set('trust proxy', true)
// May not need to use favicon if using nginx for serving
// static assets. Just comment it out below.
app.use(favicon(path.join(__dirname, 'public/favicon.ico')))
if (env === 'development') {
app.use(logger('dev'))
} else {
app.use(logger('short'))
}
if (env === 'production') {
app.use(require('connect-assets')({
paths: ['public/js', 'public/css'],
fingerprinting: true,
build: false
}));
}
app.use(compress())
app.use(bodyParser.json({
limit: '512mb'
}));
app.use(bodyParser.urlencoded({
limit: '512mb',
extended: true
}));
app.use(methodOverride())
app.use(cookieParser('1337 h4x0r'))
app.use(cookieSession({
name: 'dillinger-session',
keys: ['open', 'source']
}))
// Let's 301 redirect to simply dillinger.io
app.use(function forceLiveDomain(req, res, next) {
let host = req.get('Host');
if (host === 'www.dillinger.io') {
return res.redirect(301, 'http://dillinger.io' + req.originalUrl)
}
return next()
})
// Support for HTTP/2 Server Push
app.use(netjet({
cache: {
max: 100
}
}))
// May not need to use serveStatic if using nginx for serving
// static assets. Just comment it out below.
app.use(serveStatic(__dirname + '/public'))
// Setup local variables to be available in the views.
app.locals.title = config.title || 'Dillinger.'
app.locals.description = config.description || 'Dillinger, the last Markdown Editor, ever.'
app.locals.dillinger_version = require('./package.json').version
if (config.googleWebmasterMeta) {
app.locals.googleWebmasterMeta = config.googleWebmasterMeta
}
if (config.keywords) {
app.locals.keywords = config.keywords
}
if (config.author) {
app.locals.author = config.author
}
app.locals.node_version = process.version.replace('v', '')
app.locals.env = process.env.NODE_ENV
// At startup time so sync is ok.
app.locals.readme = fs.readFileSync(path.resolve(__dirname, './README.md'), 'utf-8')
if ('development' == env) {
app.use(errorHandler())
}
app.get('/', routes.index)
app.get('/privacy', routes.privacy)
app.get('/not-implemented', routes.not_implemented)
app.use(core)
app.use(dropbox)
app.use(bitbucket)
app.use(github)
app.use(medium)
app.use(googledrive)
app.use(onedrive)
app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'))
console.log('\nhttp://' + app.get('bind-address') + ':' + app.get('port') + '\n')
})