-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.js
108 lines (95 loc) · 3 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
var config = require("./lib/config-loader"),
express = require("express"),
bodyParser = require("body-parser"),
expressSession = require("express-session"),
authManager = require("./lib/authentication"),
passport = authManager.passport,
socketAuthenticator = require("./lib/socket-auth"),
refresh = require("./lib/refresh"),
pullManager = require("./lib/pull-manager"),
dbManager = require("./lib/db-manager"),
pullQueue = require("./lib/pull-queue"),
mainController = require("./controllers/main"),
hooksController = require("./controllers/githubHooks"),
reqLogger = require("./lib/debug")("pulldasher:server:request"),
debug = require("./lib/debug")("pulldasher");
var app = express();
var httpServer = require("http").createServer(app);
const maxPostSize = 1024 * 1024;
app.set("view engine", "html");
/**
* Middleware
*/
app.use("/public", express.static(__dirname + "/public"));
app.use(bodyParser.urlencoded({ limit: maxPostSize, extended: false }));
app.use(bodyParser.json({ limit: maxPostSize }));
app.use(
expressSession({
secret: config.session.secret,
resave: false,
saveUninitialized: false,
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(function (req, res, next) {
reqLogger("%s %s", req.method, req.url);
next();
});
/**
* Routes
*/
authManager.setupRoutes(app);
app.use("/", express.static(__dirname + "/frontend/dist"));
app.get("/token", mainController.getToken);
app.post("/hooks/main", hooksController.main);
debug("Loading all recent pulls from the DB");
dbManager
.getRecentPulls(pullManager.getOldestAllowedPullTimestamp())
.then(function (pulls) {
debug("Loaded %s pulls", pulls.length);
pullQueue.pause();
pulls.forEach(function (pull) {
pullManager.updatePull(pull);
});
pullQueue.resume();
})
.then(function () {
debug("Refreshing all open pulls from the API");
refresh.openPulls();
})
.done();
/*
@TODO: Update pulls which were open last time Pulldasher ran but are closed now.
dbManager.closeStalePulls();
*/
//====================================================
// Socket.IO
const { Server } = require("socket.io");
const io = new Server(httpServer);
io.on("connection", function (socket) {
var unauthenticated_timeout =
config.unauthenticated_timeout !== undefined
? config.unauthenticated_timeout
: 10 * 1000;
var autoDisconnect = setTimeout(function () {
socket.disconnect();
}, unauthenticated_timeout);
socket.once("authenticate", function (token) {
// They did respond. No need to drop their connection for not responding.
clearTimeout(autoDisconnect);
var user = socketAuthenticator.retrieveUser(token);
if (user) {
socket.emit("authenticated");
pullManager.addSocket(socket);
} else {
socket.emit("unauthenticated");
socket.disconnect();
}
});
socket.on("refresh", function (repo, number) {
refresh.pull(repo, number);
});
});
debug("Listening on port %s", config.port);
httpServer.listen(config.port);