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')
|
|
|
|
socketIoConfig = require('./SocketIoConfig')
|
|
|
|
soareqid = require('soa-req-id')
|
|
|
|
Router = require('../router')
|
|
|
|
metrics.inc("startup")
|
|
|
|
redis = require('redis')
|
|
|
|
RedisStore = require('connect-redis')(express)
|
|
|
|
SessionSockets = require('session.socket.io')
|
|
|
|
sessionStore = new RedisStore(host:Settings.redis.web.host, port:Settings.redis.web.port, pass:Settings.redis.web.password)
|
|
|
|
cookieParser = express.cookieParser(Settings.security.sessionSecret)
|
|
|
|
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-03-26 13:30:25 -04:00
|
|
|
translations = require "translations-sharelatex"
|
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
|
|
|
|
|
|
|
Settings.editorIsOpen ||= true
|
|
|
|
|
|
|
|
if Settings.cacheStaticAssets
|
|
|
|
staticCacheAge = (oneDayInMilliseconds * 365)
|
|
|
|
else
|
|
|
|
staticCacheAge = 0
|
|
|
|
|
|
|
|
app = express()
|
|
|
|
|
|
|
|
cookieKey = "sharelatex.sid"
|
|
|
|
cookieSessionLength = 5 * oneDayInMilliseconds
|
|
|
|
|
|
|
|
csrf = express.csrf()
|
|
|
|
ignoreCsrfRoutes = []
|
|
|
|
app.ignoreCsrf = (method, route) ->
|
|
|
|
ignoreCsrfRoutes.push new express.Route(method, route)
|
|
|
|
|
2014-07-30 09:22:36 -04:00
|
|
|
|
|
|
|
|
2014-06-25 05:34:23 -04:00
|
|
|
app.configure () ->
|
|
|
|
if Settings.behindProxy
|
|
|
|
app.enable('trust proxy')
|
2014-02-12 05:23:40 -05:00
|
|
|
app.use express.static(__dirname + '/../../../public', {maxAge: staticCacheAge })
|
|
|
|
app.set 'views', __dirname + '/../../views'
|
|
|
|
app.set 'view engine', 'jade'
|
2014-05-15 11:32:17 -04:00
|
|
|
app.use express.bodyParser(uploadDir: Settings.path.uploadFolder)
|
2014-02-12 05:23:40 -05:00
|
|
|
app.use express.bodyParser(uploadDir: __dirname + "/../../../data/uploads")
|
2014-03-26 12:35:13 -04:00
|
|
|
app.use translations.expressMiddlewear
|
2014-02-12 05:23:40 -05:00
|
|
|
app.use cookieParser
|
|
|
|
app.use express.session
|
2014-06-25 05:34:23 -04:00
|
|
|
proxy: Settings.behindProxy
|
2014-02-12 05:23:40 -05:00
|
|
|
cookie:
|
|
|
|
maxAge: cookieSessionLength
|
|
|
|
secure: Settings.secureCookie
|
|
|
|
store: sessionStore
|
|
|
|
key: cookieKey
|
|
|
|
|
|
|
|
# Measure expiry from last request, not last login
|
|
|
|
app.use (req, res, next) ->
|
|
|
|
req.session.expires = Date.now() + cookieSessionLength
|
|
|
|
next()
|
|
|
|
|
|
|
|
app.use (req, res, next) ->
|
|
|
|
for route in ignoreCsrfRoutes
|
|
|
|
if route.method == req.method?.toLowerCase() and route.match(req.path)
|
|
|
|
return next()
|
|
|
|
csrf(req, res, next)
|
|
|
|
|
|
|
|
app.use ReferalConnect.use
|
|
|
|
app.use express.methodOverride()
|
2014-07-30 09:22:36 -04:00
|
|
|
app.use translations.serverStaticFiles
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
expressLocals(app)
|
|
|
|
|
|
|
|
app.configure 'production', ->
|
|
|
|
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()
|
|
|
|
|
|
|
|
logger.info ("creating HTTP server").yellow
|
|
|
|
server = require('http').createServer(app)
|
|
|
|
|
|
|
|
io = require('socket.io').listen(server)
|
|
|
|
|
|
|
|
sessionSockets = new SessionSockets(io, sessionStore, cookieParser, cookieKey)
|
|
|
|
router = new Router(app, io, sessionSockets)
|
|
|
|
socketIoConfig.configure(io)
|
|
|
|
|
|
|
|
module.exports =
|
|
|
|
io: io
|
|
|
|
app: app
|
|
|
|
server: server
|