This repository was archived by the owner on May 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathapp.coffee
More file actions
111 lines (85 loc) · 3.85 KB
/
app.coffee
File metadata and controls
111 lines (85 loc) · 3.85 KB
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
logger = require "logger-sharelatex"
logger.initialize("github-latex-ci")
metrics = require "metrics-sharelatex"
metrics.initialize "github-latex-ci"
metrics.memory.monitor(logger)
metrics.memory.Check(logger)
settings = require "settings-sharelatex"
{mountPoint, publicUrl} = settings.internal.github_latex_ci
express = require "express"
app = express()
app.use metrics.http.monitor(logger)
csrf = require("csurf")()
# We use forms to do POST requests, with a _csrf field. bodyParser takes
# care of parsing these to req.body._csrf
bodyParser = require("body-parser")
redis = require("redis-sharelatex")
rclient = redis.createClient(settings.redis.web)
session = require('express-session')
RedisStore = require('connect-redis')(session)
sessionStore = new RedisStore(client:rclient)
IndexController = require "./app/js/IndexController"
AuthenticationController = require "./app/js/AuthenticationController"
RepositoryController = require "./app/js/RepositoryController"
WebHookController = require "./app/js/WebHookController"
BuildController = require "./app/js/BuildController"
app.set('views', './app/views')
app.set('view engine', 'jade')
app.set("mountPoint", mountPoint)
app.set("publicUrl", publicUrl)
app.set("style", settings.style)
# Cookies and sessions.
fiveDaysInMilliseconds = 5 * 24 * 60 * 60 * 1000
app.use(session(
store:sessionStore
secret: settings.security.sessionSecret,
name: settings.cookieName,
cookie:
domain: settings.cookieDomain
maxAge: fiveDaysInMilliseconds
secure: settings.secureCookie
proxy: settings.behindProxy
))
destroySession = (req, res, next) ->
if req.session?
req.session.destroy()
next()
app.use(AuthenticationController.setupLoginStatus)
app.get("#{mountPoint}/", IndexController.index)
app.get("#{mountPoint}/login", AuthenticationController.login)
app.get("#{mountPoint}/auth", AuthenticationController.auth)
app.get("#{mountPoint}/repos", csrf, AuthenticationController.requireLogin, RepositoryController.list)
app.get("#{mountPoint}/repos/:owner/:repo", csrf, RepositoryController.show)
app.get("#{mountPoint}/repos/:owner/:repo/git/blobs/:sha", RepositoryController.proxyBlob)
app.post("#{mountPoint}/repos/:owner/:repo/hook", bodyParser(), csrf, AuthenticationController.requireLogin, WebHookController.createHook)
app.post("#{mountPoint}/repos/:owner/:repo/hook/destroy", bodyParser(), csrf, AuthenticationController.requireLogin, WebHookController.destroyHook)
app.post("#{mountPoint}/events", destroySession, WebHookController.webHookEvent)
app.get("#{mountPoint}/repos/:owner/:repo/builds/:sha", BuildController.showBuild)
app.get("#{mountPoint}/repos/:owner/:repo/builds/latest/badge.svg", BuildController.latestPdfBadge)
# Note that ../output.pdf is a clever link which will redirect to the build page if there is no PDF,
# while ../raw/output.pdf will fail if the PDF is not there.
app.get("#{mountPoint}/repos/:owner/:repo/builds/latest/output.pdf", BuildController.downloadLatestBuild)
app.get regex = new RegExp("^#{mountPoint.replace('/', '\/')}\/repos\/([^\/]+)\/([^\/]+)\/builds\/([^\/]+)\/raw\/(.*)$"), (req, res, next) ->
params = {
owner: req.params[0]
repo: req.params[1]
sha: req.params[2]
path: req.params[3]
}
req.params = params
next()
, BuildController.downloadOutputFile
app.post("#{mountPoint}/repos/:owner/:repo/builds/latest", bodyParser(), csrf, AuthenticationController.requireLogin, BuildController.buildLatestCommit)
app.get "#{mountPoint}/status", destroySession, (req, res, next) ->
res.send("github-latex-ci is alive")
port = settings.internal.github_latex_ci.port
host = settings.internal.github_latex_ci.host
app.listen port, host, (error) ->
throw error if error?
logger.info "github-latex-ci starting up, listening on #{host}:#{port}"
if global.gc?
gcTimer = setInterval () ->
global.gc()
logger.log process.memoryUsage(), "global.gc"
, 3 * oneMinute = 60 * 1000
gcTimer.unref()