-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
98 lines (95 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
94
95
96
97
98
require("dotenv").config();
const express = require("express");
const request = require("request");
const app = express();
const Raven = require("raven");
const https = require("https");
const qs = require("qs");
const cors = require("cors");
const metascraper = require("metascraper");
const got = require("got");
const PORT = process.env.PORT || 5000;
// Must configure Raven before doing anything else with it
Raven.config(process.env.__DSN__).install();
app
.use(Raven.requestHandler())
.use(cors())
.get("/auth", (req, res) => {
console.log(req.originalUrl);
res.sendFile(__dirname + "/static/add_to_slack.html");
})
.get("/auth/redirect", async (req, res) => {
try {
var options = {
uri:
"https://slack.com/api/oauth.access?code=" +
req.query.code +
"&client_id=" +
process.env.CLIENT_ID +
"&client_secret=" +
process.env.CLIENT_SECRET +
"&redirect_uri=" +
process.env.REDIRECT_URI,
method: "GET"
};
await request(options, (error, response, body) => {
const JSONresponse = JSON.parse(body);
if (!JSONresponse.ok) {
console.log(JSONresponse);
res
.send("Error encountered: \n" + JSON.stringify(JSONresponse))
.status(200)
.end();
} else {
console.log(JSONresponse);
res.send(`Success! ${JSONresponse.access_token}`);
}
});
} catch (e) {
console.log("/auth", e);
}
})
.get("/validate", async (req, res) => {
try {
const { code } = qs.parse(req.url.split("?")[1]);
const options = {
uri:
"https://slack.com/api/oauth.access?" +
qs.stringify({
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
code,
redirect_uri: process.env.REDIRECT_URI
})
};
await request(options, (error, response, body) => {
if (error) return console.error(errror);
res.json(body);
});
} catch (e) {
console.log("/validate ", e);
}
})
.get("/preview", async (req, res) => {
try {
const targetUrl = req.query.uri;
if (targetUrl.match(/^chrome+/)) return null;
else {
console.log(targetUrl);
(async () => {
const { body: html, url } = await got(targetUrl);
const metadata = await metascraper({ html, url });
res.send(metadata);
})();
}
} catch (e) {
console.log("/preview ", e);
}
})
.use(function onError(err, req, res, next) {
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
res.end(res.sentry + "\n");
})
.listen(PORT, () => console.log(`Listening on ${PORT}`));