-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
53 lines (39 loc) · 1.23 KB
/
app.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
const
Koa = require("koa"),
serve = require("koa-static"),
bodyParser = require("koa-bodyparser"),
session = require("koa-session"),
path = require("path"),
crypto = require("crypto"),
config = require("./config"),
defaultroutes = require("./routes/default"),
webuathnroutes = require("./routes/webauthn.js"),
tokenroutes = require("./routes/token"),
app = new Koa();
// Static files (./static)
app.use(serve(path.join(__dirname, "public/static")));
// Session
app.keys = [crypto.randomBytes(32).toString("hex")];
app.use(session({key: "session"}, app));
// Middleware
app.use(bodyParser());
//Routes
app.use(defaultroutes.routes());
app.use(defaultroutes.allowedMethods());
app.use(webuathnroutes.routes());
app.use(webuathnroutes.allowedMethods());
app.use(tokenroutes.routes());
app.use(tokenroutes.allowedMethods());
// Local development
if (config.mode === "development") {
const https = require("https");
const fs = require("fs");
https.createServer({
key: fs.readFileSync("./keys/key.pem"),
cert: fs.readFileSync("./keys/cert.pem")
}, app.callback()).listen(config.port);
// "Production" HTTP - (for use behind https proxy)
} else {
app.listen(config.port);
}
console.log(`Started app on port ${config.port}`);