2014-05-07 10:29:04 -04:00
|
|
|
Path = require "path"
|
2014-02-12 05:23:40 -05:00
|
|
|
express = require('express')
|
|
|
|
Settings = require('settings-sharelatex')
|
|
|
|
logger = require 'logger-sharelatex'
|
|
|
|
metrics = require('./Metrics')
|
|
|
|
crawlerLogger = require('./CrawlerLogger')
|
|
|
|
expressLocals = require('./ExpressLocals')
|
|
|
|
Router = require('../router')
|
|
|
|
metrics.inc("startup")
|
2014-09-26 12:04:33 -04:00
|
|
|
redis = require("redis-sharelatex")
|
|
|
|
rclient = redis.createClient(Settings.redis.web)
|
|
|
|
|
2015-06-30 07:04:41 -04:00
|
|
|
session = require("express-session")
|
|
|
|
RedisStore = require('connect-redis')(session)
|
|
|
|
bodyParser = require('body-parser')
|
|
|
|
multer = require('multer')
|
|
|
|
methodOverride = require('method-override')
|
|
|
|
csrf = require('csurf')
|
|
|
|
csrfProtection = csrf()
|
|
|
|
cookieParser = require('cookie-parser')
|
|
|
|
|
2014-09-26 12:04:33 -04:00
|
|
|
sessionStore = new RedisStore(client:rclient)
|
|
|
|
|
2015-06-30 07:04:41 -04:00
|
|
|
Mongoose = require("./Mongoose")
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
oneDayInMilliseconds = 86400000
|
|
|
|
ReferalConnect = require('../Features/Referal/ReferalConnect')
|
2014-07-01 10:44:12 -04:00
|
|
|
RedirectManager = require("./RedirectManager")
|
2014-07-01 11:00:42 -04:00
|
|
|
OldAssetProxy = require("./OldAssetProxy")
|
2014-08-05 06:15:17 -04:00
|
|
|
translations = require("translations-sharelatex").setup(Settings.i18n)
|
2014-09-08 10:40:46 -04:00
|
|
|
Modules = require "./Modules"
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-05-07 10:29:04 -04:00
|
|
|
metrics.mongodb.monitor(Path.resolve(__dirname + "/../../../node_modules/mongojs/node_modules/mongodb"), logger)
|
|
|
|
metrics.mongodb.monitor(Path.resolve(__dirname + "/../../../node_modules/mongoose/node_modules/mongodb"), logger)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2015-06-23 08:50:42 -04:00
|
|
|
metrics.event_loop?.monitor(logger)
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
Settings.editorIsOpen ||= true
|
|
|
|
|
|
|
|
if Settings.cacheStaticAssets
|
|
|
|
staticCacheAge = (oneDayInMilliseconds * 365)
|
|
|
|
else
|
|
|
|
staticCacheAge = 0
|
|
|
|
|
|
|
|
app = express()
|
|
|
|
|
|
|
|
ignoreCsrfRoutes = []
|
|
|
|
app.ignoreCsrf = (method, route) ->
|
|
|
|
ignoreCsrfRoutes.push new express.Route(method, route)
|
|
|
|
|
2014-07-30 09:22:36 -04:00
|
|
|
|
2015-06-30 07:04:41 -04:00
|
|
|
if Settings.behindProxy
|
|
|
|
app.enable('trust proxy')
|
|
|
|
app.use express.static(__dirname + '/../../../public', {maxAge: staticCacheAge })
|
|
|
|
app.set 'views', __dirname + '/../../views'
|
|
|
|
app.set 'view engine', 'jade'
|
|
|
|
Modules.loadViewIncludes app
|
|
|
|
app.use cookieParser(Settings.security.sessionSecret)
|
|
|
|
app.use session
|
|
|
|
resave: false
|
|
|
|
secret:Settings.security.sessionSecret
|
|
|
|
proxy: Settings.behindProxy
|
|
|
|
cookie:
|
|
|
|
domain: Settings.cookieDomain
|
|
|
|
maxAge: Settings.cookieSessionLength
|
|
|
|
secure: Settings.secureCookie
|
|
|
|
store: sessionStore
|
|
|
|
key: Settings.cookieName
|
|
|
|
|
|
|
|
app.use bodyParser.urlencoded({ extended: true })
|
|
|
|
app.use bodyParser.json()
|
|
|
|
app.use multer(dest: Settings.path.uploadFolder)
|
|
|
|
app.use translations.expressMiddlewear
|
|
|
|
app.use translations.setLangBasedOnDomainMiddlewear
|
|
|
|
|
|
|
|
# Measure expiry from last request, not last login
|
|
|
|
app.use (req, res, next) ->
|
|
|
|
req.session.touch()
|
|
|
|
next()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2015-06-30 07:04:41 -04:00
|
|
|
app.use (req, res, next) ->
|
|
|
|
for route in ignoreCsrfRoutes
|
|
|
|
if route.method == req.method?.toLowerCase() and route.match(req.path)
|
|
|
|
return next()
|
|
|
|
csrfProtection(req, res, next)
|
|
|
|
|
|
|
|
app.use ReferalConnect.use
|
|
|
|
app.use methodOverride()
|
2014-07-30 09:22:36 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
expressLocals(app)
|
|
|
|
|
2015-06-30 07:04:41 -04:00
|
|
|
if app.get('env') == 'production'
|
2014-02-12 05:23:40 -05:00
|
|
|
logger.info "Production Enviroment"
|
|
|
|
app.enable('view cache')
|
|
|
|
|
2014-05-07 10:29:04 -04:00
|
|
|
app.use metrics.http.monitor(logger)
|
2014-07-01 10:44:12 -04:00
|
|
|
app.use RedirectManager
|
2014-07-01 11:00:42 -04:00
|
|
|
app.use OldAssetProxy
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
app.use (req, res, next)->
|
|
|
|
metrics.inc "http-request"
|
|
|
|
crawlerLogger.log(req)
|
|
|
|
next()
|
|
|
|
|
|
|
|
app.use (req, res, next) ->
|
|
|
|
if !Settings.editorIsOpen
|
|
|
|
res.status(503)
|
2014-08-01 08:47:14 -04:00
|
|
|
res.render("general/closed", {title:"maintenance"})
|
2014-02-12 05:23:40 -05:00
|
|
|
else
|
|
|
|
next()
|
|
|
|
|
|
|
|
app.get "/status", (req, res)->
|
|
|
|
res.send("web sharelatex is alive")
|
|
|
|
req.session.destroy()
|
2015-02-03 06:05:23 -05:00
|
|
|
|
|
|
|
profiler = require "v8-profiler"
|
|
|
|
app.get "/profile", (req, res) ->
|
|
|
|
time = parseInt(req.query.time || "1000")
|
|
|
|
profiler.startProfiling("test")
|
|
|
|
setTimeout () ->
|
|
|
|
profile = profiler.stopProfiling("test")
|
|
|
|
res.json(profile)
|
|
|
|
, time
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
logger.info ("creating HTTP server").yellow
|
|
|
|
server = require('http').createServer(app)
|
|
|
|
|
2015-02-05 11:37:37 -05:00
|
|
|
router = new Router(app)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports =
|
|
|
|
app: app
|
|
|
|
server: server
|