-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
129 lines (103 loc) · 5.41 KB
/
index.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var flash = require('connect-flash');
var cors = require('cors');
var passport = require('passport');
var mongoose = require('mongoose');
var mongoUri = process.env.MONGOLAB_URI || 'mongodb://localhost/ubeat';
mongoose.connect(mongoUri);
var authentication = require('./middleware/authentication');
var genres = require('./routes/genres');
var login = require('./routes/login');
var lookup = require('./routes/lookup');
var search = require('./routes/search');
var signup = require('./routes/signup');
var status = require('./routes/status');
var user = require('./routes/user');
var watchlist = require('./routes/watchlists');
var app = express();
var corsOptions = {
origin: '*',
methods: ['GET', 'PUT', 'POST', 'PATCH', 'DELETE', 'UPDATE'],
credentials: true
};
var tokenSecret = 'UBEAT_TOKEN_SECRET' || process.env.TOKEN_SECRET;
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.set('jwtTokenSecret', tokenSecret);
require('./middleware/passport')(passport, app);
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({
secret: 'ubeat_session_secret',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(cors(corsOptions));
app.get('/status', status.getStatus);
app.get('/login', login.showLoginPage);
app.post('/login', passport.authenticate('local-login'), login.getToken);
app.get('/logout', login.logout);
app.get('/signup', signup.showSignupPage);
app.post('/signup', passport.authenticate('local-signup'), login.getToken);
app.get('/welcome', signup.welcome);
app.get('/token', login.getToken);
app.get('/tokenInfo', authentication.isAuthenticated, login.getToken);
// Secure API
app.get('/genres/movies', authentication.isAuthenticated, genres.getMoviesGenres);
app.get('/genres/tvshows', authentication.isAuthenticated, genres.getTvShowsGenres);
app.get('/search', authentication.isAuthenticated, search.search);
app.get('/search/actors', authentication.isAuthenticated, search.searchActor);
app.get('/search/movies', authentication.isAuthenticated, search.searchMovie);
app.get('/search/tvshows/episodes', authentication.isAuthenticated, search.searchTvShowEpisode);
app.get('/search/tvshows/seasons', authentication.isAuthenticated, search.searchTvShowSeason);
app.get('/search/users', authentication.isAuthenticated, user.findByName);
app.get('/users', authentication.isAuthenticated, user.allUsers);
app.get('/users/:id', authentication.isAuthenticated, user.findById);
app.post('/follow', authentication.isAuthenticated, user.follow);
app.delete('/follow/:id', authentication.isAuthenticated, user.unfollow);
app.get('/actors/:id', authentication.isAuthenticated, lookup.getActor);
app.get('/actors/:id/movies', authentication.isAuthenticated, lookup.getActorMovies);
app.get('/movies/:id', authentication.isAuthenticated, lookup.getMovie);
app.get('/tvshows/seasons/:id', authentication.isAuthenticated, lookup.getTvShowSeason);
app.get('/tvshows/seasons/:id/episodes', authentication.isAuthenticated, lookup.getTvShowEpisodes);
app.get('/watchlists', authentication.isAuthenticated, watchlist.getWatchlists);
app.post('/watchlists', authentication.isAuthenticated, watchlist.createWatchlist);
app.get('/watchlists/:id', authentication.isAuthenticated, watchlist.getWatchlistById);
app.post('/watchlists/:id/movies', authentication.isAuthenticated, watchlist.addMovieToWatchlist);
app.delete('/watchlists/:id/movies/:trackId', authentication.isAuthenticated, watchlist.removeMovieFromWatchlist);
app.put('/watchlists/:id', authentication.isAuthenticated, watchlist.updateWatchlist);
app.delete('/watchlists/:id', authentication.isAuthenticated, watchlist.removeWatchlist);
// Unsecure API. Useful for the second release.
app.get('/unsecure/genres/movies', genres.getMoviesGenres);
app.get('/unsecure/genres/tvshows', genres.getTvShowsGenres);
app.get('/unsecure/search', search.search);
app.get('/unsecure/search/actors', search.searchActor);
app.get('/unsecure/search/movies', search.searchMovie);
app.get('/unsecure/search/tvshows/episodes', search.searchTvShowEpisode);
app.get('/unsecure/search/tvshows/seasons', search.searchTvShowSeason);
app.get('/unsecure/search/users', user.findByName);
app.get('/unsecure/users', user.allUsers);
app.get('/unsecure/users/:id', user.findById);
app.post('/unsecure/follow', user.follow);
app.delete('/unsecure/follow/:id', user.unfollow);
app.get('/unsecure/actors/:id', lookup.getActor);
app.get('/unsecure/actors/:id/movies', lookup.getActorMovies);
app.get('/unsecure/movies/:id', lookup.getMovie);
app.get('/unsecure/tvshows/seasons/:id', lookup.getTvShowSeason);
app.get('/unsecure/tvshows/seasons/:id/episodes', lookup.getTvShowEpisodes);
app.get('/unsecure/watchlists', watchlist.getWatchlists);
app.post('/unsecure/watchlists', watchlist.createWatchlistUnsecure);
app.get('/unsecure/watchlists/:id', watchlist.getWatchlistById);
app.post('/unsecure/watchlists/:id/movies', watchlist.addMovieToWatchlist);
app.delete('/unsecure/watchlists/:id/movies/:trackId', watchlist.removeMovieFromWatchlist);
app.put('/unsecure/watchlists/:id', watchlist.updateWatchlist);
app.delete('/unsecure/watchlists/:id', watchlist.removeWatchlistUnsecure);
var port = process.env.PORT || 3000;
app.listen(port);