-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
91 lines (74 loc) · 2.45 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
const fs = require("fs");
const Koa = require("koa");
const bodyparser = require("koa-bodyparser");
const session = require("koa-session");
const mount = require("koa-mount");
const { jsonify, logger, timer } = require("./middleware");
const URL = require("url");
const redis = require("redis");
const { promisify } = require("util");
const Raven = require("raven");
const router = require("./router");
const { app: UCLAPI } = require("./uclapi");
const { app: notifications } = require("./notifications");
require("dotenv").config();
const { version } = JSON.parse(fs.readFileSync("./package.json"));
Raven.config().install();
const connectionString = process.env.REDIS_URL;
if (connectionString === undefined) {
console.error("Please set the REDIS_URL environment variable");
process.exit(1);
}
const app = new Koa();
app.context.version = version;
console.log(`Running server version ${version}`);
if (
!process.env.UCLAPI_CLIENT_ID ||
!process.env.UCLAPI_CLIENT_SECRET ||
!process.env.UCLAPI_TOKEN
) {
console.error(
"Error! You have not set the UCLAPI_CLIENT_ID, UCLAPI_TOKEN, or UCLAPI_CLIENT_SECRET environment variables.",
);
console.log("Please set them to run this app.");
process.abort();
}
if (!process.env.SECRET) {
console.warn(
"Warning: You have not set the SECRET environment variable. This is not secure and definitely not recommended.",
);
}
if (!process.env.NOTIFICATIONS_URL) {
console.warn(
"Warning: You have not set the NOTIFICATION_URL environment variable. This means that notification actions will be disabled.",
);
}
app.keys = [process.env.SECRET || "secret"];
if (connectionString.startsWith("rediss://")) {
app.context.redisClient = redis.createClient(connectionString, {
tls: { servername: new URL(connectionString).hostname },
});
} else {
app.context.redisClient = redis.createClient(connectionString);
}
app.context.redisGet = promisify(app.context.redisClient.get).bind(
app.context.redisClient,
);
app.context.redisSetex = promisify(app.context.redisClient.setex).bind(
app.context.redisClient,
);
app.context.redisSet = promisify(app.context.redisClient.set).bind(
app.context.redisClient,
);
app.use(session({}, app));
app.use(bodyparser());
app.use(timer);
app.use(logger);
app.use(jsonify);
// import and use the UCL API router.
app.use(mount("/notifications", notifications));
app.use(mount(UCLAPI));
app.use(mount(router));
Raven.context(() => {
app.listen(process.env.PORT || 3000);
});