forked from newtonian-platypus/greenCast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
93 lines (73 loc) · 2.83 KB
/
server.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
//should be abstracted into a server config file
require('dotenv').config({silent: true});
const path = require('path');
const express = require('express');
const pug = require('pug');
const Promise = require('bluebird');
const bodyParser = require('body-parser');
const betterErrors = require('better-express-errors');
const morgan = require('morgan');
const session = require('express-session');
const passport = require('passport');
const GitHubStrategy = require('passport-github2');
const routes = require('./server/routes.js');
const app = express();
// passport.js sessions
passport.use(new GitHubStrategy({
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackURL: 'http://localhost:3000/auth/github/callback'
}, routes.login));
passport.serializeUser((user, done) => done(null, user.username));
passport.deserializeUser((id, done) => done(null, id));
//middleware
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'pug');
app.use(session({ secret: 'keyboard cat', resave: false, saveUninitialized: false }));
app.use(express.static(__dirname + '/public'));
app.use(passport.initialize());
app.use(passport.session());
//basic router
app.get('/', routes.root);
app.param('username', function(req, res, next, username) {
req.user = {username: username};
next();
});
// returns a user's subscriptions
app.get('/user/:username/subscriptions', routes.getSubscriptions);
// adds a new channel to a user's subscriptions
app.post('/user/:username/subscriptions', routes.addSubscription);
// adds a new channel to a user's subscriptions
app.delete('/user/:username/subscriptions', routes.removeSubscription);
// get's a user's public information
app.get('/user/:username', routes.getUser);
// adds a new user
app.post('/user', routes.addUser);
// returns an array of a channel's episodes
app.get('/channel/:channelId', routes.getEpisodes);
// bypasses CORS headers from mp3 domain
app.get('/api/audio/*', routes.getAudio);
// returns the top 20 podcasts
app.get('/api/toppodcasts', routes.topPodcasts);
// sets the user's session to null redirects to /login
app.get('/logout', routes.logout);
// passport auth routes
app.get('/auth/github',
passport.authenticate('github', { scope: [ 'user:email' ] }),
function(req, res) {
// The request will be redirected to GitHub for authentication, so this
// function will not be called.
});
app.get('/auth/github/callback',
passport.authenticate('github', { failureRedirect: '/' }),
function(req, res) {
// Successful authentication, redirect home.
console.log(req.isAuthenticated());
res.redirect('/');
});
app.listen(3000, () => console.log('GreenCast listening on port 3000'));
//run this after all routes and middleware
app.use(betterErrors(app));
module.exports = app;