-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (115 loc) · 3.8 KB
/
index.js
File metadata and controls
140 lines (115 loc) · 3.8 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const express = require('express');
const session = require('express-session');
const bodyParser = require('body-parser');
const db = require('./db.js');
const github = require('./github.js');
const passport = require('passport');
const GitHubStrategy = require('passport-github').Strategy;
const path = require('path')
const schedule = require('node-schedule')
const app = express()
const port = process.env.PORT || '5000';
if (process.env.NODE_ENV !== "production" ){
require('dotenv').config();
}
// ***** Get and update each users' commits once a day at 3 AM *****
let fetchTime = 1*24*60*60*1000 // 1 day in milliseconds
var scheduleRule = new schedule.RecurrenceRule()
scheduleRule.hour = 3;
var job = schedule.scheduleJob(scheduleRule, function() {
db.getAllStudentIdAndGithub()
.then(studentIdAndGithub => {
studentIdAndGithub.forEach(studentIds => {
github.getUserCommits(studentIds.github, fetchTime)
.then(commitsOutArray => {
commitsOutArray.forEach(commit => {
commit.student = studentIds.id
})
db.addCommits(commitsOutArray)
.then(() => {
console.log(`Commits updated for student ${studentIds.github}`)
})
})
.catch(err => {
console.log('error getting github data for student - ', err)
})
})
})
})
passport.use(new GitHubStrategy(
{
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackURL: `${process.env.ROOT_URL}/auth/github/callback`
},
db.authenticateUser
));
// ***** Serialize and deserialize users for Passport session *****
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
db.getUser(id)
.then(user => {
done(null, user)
})
.catch(err => {
done(err)
})
});
// ***** Setup Express / Passport sessions *****
app.use(session({
secret: process.env.PASSPORT_SECRET,
name : process.env.APP_NAME,
proxy : true,
resave : true,
saveUninitialized : true
}));
app.use(bodyParser.urlencoded({extended : false}))
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());
// ***** Set template engine *****
app.set('view engine', 'pug')
app.set('views', './public/views')
// ***** Routes *****
// app.use('/public', express.static('public'));
app.use('/favicon.ico', (req, res) => {
res.status(204)
})
app.get('/logout', (req, res) => {
req.logout();
res.redirect(process.env.ROOT_URL);
})
// let dashboard = require('./routes/dashboard')
// let students = require('./routes/students')
// let cohorts = require('./routes/cohorts')
// let instructors = require('./routes/instructors')
let api = require('./routes/api')
// app.use('/dashboard', dashboard)
// app.use('/', cohorts)
// app.use('/students', students)
// app.use('/instructors', instructors)
app.use('/api', api)
// app.get('/api/test', (req, res) => {
// res.status(200).json({"ok":"ok"})
// })
app.get('/auth/github',
passport.authenticate('github'), function(req,res) {
console.log('Attempting to authenticate w/ github')
});
app.get('/auth/github/callback',
passport.authenticate('github', { failureRedirect: '/' }),
function(req, res) {
console.log('Authenticated with Github')
// res.sendFile(path.join(__dirname+'/client/build/index.html'));
// res.redirect('/');
res.redirect(process.env.ROOT_URL);
});
app.use(express.static(path.join(__dirname, 'client/build')));
app.get('*', (req,res) =>{
res.sendFile(path.join(__dirname+'/client/build/index.html'));
});
app.listen(port, () => {
console.log(`Listening on port ${port}`)
})