forked from tomjohnburton/rest-streaming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
158 lines (138 loc) · 4 KB
/
server.js
File metadata and controls
158 lines (138 loc) · 4 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var http = require("http");
var path = require("path");
var express = require("express");
var ioserver = require("socket.io");
var bodyParser = require("body-parser");
var passport = require("passport");
var NestStrategy = require("passport-nest").Strategy;
var session = require("express-session");
var openurl = require("openurl");
require("dotenv").config();
var cors = require("cors");
var axios = require("axios");
const AWS = require("aws-sdk");
const publishToAws = require("./lib/publishToAws").publishToAws;
const startStreaming = require("./lib/startStreaming").startStreaming;
const saveToRedis = require("./lib/redis").saveToRedis;
const getAll = require("./lib/redis").getAll;
// PassportJS options. See http://passportjs.org/docs for more information.
var passportOptions = {
failureRedirect: "/auth/failure" // Redirect to another page on failure.
};
AWS.config.region = "eu-central-1";
AWS.config.credentials = new AWS.Credentials(
process.env.AWS_CLIENT_ID,
process.env.AWS_SECRET_CLIENT_KEY
);
passport.use(
new NestStrategy({
// Read credentials from your environment variables.
clientID: process.env.NEST_ID,
clientSecret: process.env.NEST_SECRET
})
);
/**
* No user data is available in the Nest OAuth
* service, just return the empty user objects.
*/
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
var app = express();
app.use(
cors({
origin: "https://nest-camera-frontend.herokuapp.com",
credentials: true
})
);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(
session({
secret: process.env.SESSIOM_SECRET_KEY,
resave: false,
saveUninitialized: false
})
);
app.use(passport.initialize());
app.use(passport.session());
/**
* Listen for calls and redirect the user to the Nest OAuth
* URL with the correct parameters.
*/
app.get("/auth/nest", passport.authenticate("nest", passportOptions), function(
req,
res
) {
res.redirect;
});
/**
* Upon return from the Nest OAuth endpoint, grab the user's
* accessToken and start streaming the events.
*/
app.post("/auth/nest/callback", function(req, res) {
console.log(req.body.code);
axios.default
.post(`${process.env.LAMBDA_AUTH_URL}?authCode=${req.body.code}`)
.then(response => {
let { token } = response.data;
res.send({ token });
})
.catch(err => {
console.log(err);
res.end();
});
});
/**
* When authentication fails, present the user with
* an error requesting they try the request again.
*/
app.get("/auth/failure", function(req, res) {
res.send("Authentication failed. Please try again.");
});
app.get("/events", async function(req, res) {
const events = await getAll();
res.send(events);
});
app.get("/hello", function(req, res) {
console.log("world");
res.send("world");
});
/**
* Get port from environment and store in Express.
*/
var port = process.env.PORT || 4000;
app.set("port", port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
const io = ioserver(server);
console.log("websocket: start");
io.on("connection", function(socket) {
console.log("someone connected");
startStreaming(process.env.NEST_ACCESS_TOKEN, socket);
});
server.listen(port);
startStreaming(process.env.NEST_ACCESS_TOKEN, null, publishToAws, saveToRedis);