forked from pulseenergy/panorama
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
76 lines (64 loc) · 1.95 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
var express = require('express');
var engine = require('ejs-locals');
var request = require('request');
var githubApi = require('./lib/githubApi');
var actions = require('./lib/actions');
var app = express();
app.engine('ejs', engine);
app.set('view engine', 'ejs');
app.use(express.compress());
app.use(express.json());
app.use(express.urlencoded());
app.use(express.cookieParser());
app.use(express.session({ secret: 'f8fb234f3cf333241e3f7c74' }));
if (process.env.GITHUB_API_TOKEN) {
var token = process.env.GITHUB_API_TOKEN;
console.log('github auth via personal access token');
app.use(require('./lib/noauth')(token));
} else {
var id = process.env.GITHUB_APP_ID;
var secret = process.env.GITHUB_APP_SECRET;
if (!id || !secret) {
console.error('github auth environment variables not found -- see README');
process.exit(1);
}
console.log('github auth via registered oauth application');
app.use(require('./lib/everyauth')(id, secret));
}
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/dist'));
app.get('/', function (req, res) {
res.redirect('/lanes');
});
function loadUserOrganizations(req, res, next) {
if (req.user && !req.user.organizations) {
request(githubApi.organizations(req.user, req.session.authToken), function (err, response, body) {
req.user.organizations = body;
next();
});
} else {
next();
}
}
function checkAuth401(req, res, next) {
if (!req.user) {
res.send(401);
} else {
next();
}
}
app.get('/list', loadUserOrganizations, function (req, res) {
res.render('list');
});
app.get('/lanes', loadUserOrganizations, function (req, res) {
res.render('list');
});
// api
app.get('/a/organization/:organization/events', actions.getOrgEvents);
app.get('/a/user/events', checkAuth401, actions.getUserEvents);
app.use(function handleError(err, req, res, next) { // eslint-disable-line no-unused-vars
console.error(err.stack);
res.send(500);
});
module.exports = app;