-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
163 lines (146 loc) · 4.42 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const app = require('express')();
const bodyParser = require('body-parser');
const basicAuth = require('basic-auth');
const request = require('request');
const upload = require('multer')();
// Posters gallery https://imgur.com/a/RMm7U
const movies = [
{
name: 'Casablanca',
year: '1942',
genre: 'Romance',
poster: 'https://github.com/AnomalyInnovations/toolbeam-example-api/raw/master/images/1.jpg'
},
{
name: 'Gone with the Wind',
year: '1939',
genre: 'Romance',
poster: 'https://github.com/AnomalyInnovations/toolbeam-example-api/raw/master/images/2.jpg'
},
{
name: 'Citizen Kane',
year: '1941',
genre: 'Mystery',
poster: 'https://github.com/AnomalyInnovations/toolbeam-example-api/raw/master/images/3.jpg'
},
{
name: 'The Wizard of Oz',
year: '1939',
genre: 'Fantasy',
poster: 'https://github.com/AnomalyInnovations/toolbeam-example-api/raw/master/images/4.jpg'
},
{
name: 'North by Northwest',
year: '1959',
genre: 'Thriller',
poster: 'https://github.com/AnomalyInnovations/toolbeam-example-api/raw/master/images/5.jpg'
},
{
name: "It's a Wonderful Life",
year: '1946',
genre: 'Fantasy',
poster: 'https://github.com/AnomalyInnovations/toolbeam-example-api/raw/master/images/6.jpg'
},
{
name: 'Some Like It Hot',
year: '1959',
genre: 'Action',
poster: 'https://github.com/AnomalyInnovations/toolbeam-example-api/raw/master/images/7.jpg'
},
{
name: "Singin in the Rain",
year: '1952',
genre: 'Romance',
poster: 'https://github.com/AnomalyInnovations/toolbeam-example-api/raw/master/images/8.jpg'
},
{
name: 'Ben-Hur',
year: '1959',
genre: 'Action',
poster: 'https://github.com/AnomalyInnovations/toolbeam-example-api/raw/master/images/9.jpg'
},
{
name: 'Psycho',
year: '1960',
genre: 'Thriller',
poster: 'https://github.com/AnomalyInnovations/toolbeam-example-api/raw/master/images/10.jpg'
},
];
const getId = str => (parseInt(str, 10) - 1) % 10;
const unauthorized = res => {
res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
return res.sendStatus(401);
};
app.use(bodyParser.urlencoded({ extended: false }));
// Return list of top movies
// Demo:
// curl -i http://localhost:3030/top_movies
app.get('/top_movies', (req, res) => {
res.send(movies);
});
// Return a movie
// Demo:
// curl -i http://localhost:3030/movies/1
app.get('/movies/:id', (req, res) => {
res.send(movies[getId(req.params.id)]);
});
// Update a movie
// Demo:
// curl -i -X PUT -F "genre=Thriller" -F "poster=@/Users/jayair/Downloads/out.gif" http://localhost:3030/movies/1
app.put('/movies/:id', upload.single('poster'), (req, res) => {
res.send(Object.assign({},
movies[getId(req.params.id)],
req.file && { poster: `${req.file.originalname} (${req.file.mimetype})` },
req.body));
});
// Delete a movie
// Demo:
// curl -i -X DELETE -u admin:password http://localhost:3030/movies/1
app.delete('/movies/:id', auth, (req, res) => {
res.send({ deleted: movies[getId(req.params.id)].name });
});
// Subscribe for a movie that will be playing
// Demo:
// curl -i -X POST -H "TB-User-UUID: adqlymds" -H "TB-Tool-UUID: bcgrjtcu" -d location=%7B%22latitude%22%3A43.64%2C%22longitude%22%3A-79.37%7D http://localhost:3030/movies/1/subscribe
app.post('/movies/:id/subscribe', (req, res) => {
const movie = movies[getId(req.params.id)];
setTimeout(() => request({
method: 'POST',
uri: 'https://api.toolbeam.com/v1/app/send_notifications',
headers: {
'TB-API-KEY': 'Put in your API key here'
},
form: {
message: `${movie.name} is now playing nearby!`,
user_uuid: req.get('TB-User-UUID'),
tool_uuid: req.get('TB-Tool-UUID')
}
}), 5000);
res.send({ subscribed: true, movie: movie.name, location: JSON.parse(req.body.location) });
});
// Search for a movie
// Demo:
// curl -i http://localhost:3030/movies?keyword=test
app.get('/movies', (req, res) => {
res.send(movies.map((movie, i) => Object.assign({}, movie, {
'edit': `https://toolbeam.com/t/kldgocfm?id=${i + 1}&genre=${movie.genre}`,
'delete': `https://toolbeam.com/t/moqulqnk/response?id=${i + 1}`,
'subscribe': `https://toolbeam.com/t/bcgrjtcu?id=${i + 1}`
})));
});
app.listen(3030);
///////////////////////
// Private Functions //
///////////////////////
function auth(req, res, next) {
const user = basicAuth(req);
if ( ! user || ! user.name || ! user.pass) {
return unauthorized(res);
}
if (user.name === 'admin' && user.pass === 'password') {
return next();
}
else {
return unauthorized(res);
}
};