-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.ts
88 lines (73 loc) · 2.71 KB
/
index.ts
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
import express, { NextFunction, Request, RequestHandler, Response } from "express"
import http from "http"
import morgan from "morgan"
import cors from "cors"
import config from "./config"
import generator from "./generator"
import tokenHandler from "./token_handler"
import register from "./registration_handler"
import bulkData from "./bulk_data_handler"
import env from "./env"
import encodedOutcome from "./outcome_handler"
const app = express();
/* istanbul ignore if */
if (process.env.NODE_ENV === "production") {
app.use(morgan("combined"));
}
function requireMethod(method: string): RequestHandler {
return (req, res) => res.status(400).end(`This endpoint can only be used with ${method} requests`)
}
// HTTP to HTTPS redirect (this is Heroku-specific!)
/* istanbul ignore next */
app.use((req, res, next) => {
let proto = req.headers["x-forwarded-proto"];
let host = req.headers.host;
if (proto && (`${proto}://${host}` !== config.baseUrl)) {
return res.redirect(301, config.baseUrl + req.url);
}
next();
});
// @ts-ignore backend services authorization
app.options("/auth/token", cors({ origin: true }));
// @ts-ignore
app.post("/auth/token", cors({ origin: true }), express.urlencoded({ extended: false }), tokenHandler);
app.get("/auth/token", cors({ origin: true }), requireMethod("POST"));
// @ts-ignore backend services registration
app.post("/auth/register", express.urlencoded({ extended: false }), register);
app.get("/auth/register", cors({ origin: true }), requireMethod("POST"));
// Used as JWKS generator
app.use("/generator", generator);
// host env vars for the client-side
app.get("/env.js", env);
// Send some of the server config vars to the client
app.get("/server-config.js", (req, res) => {
res.type("javascript").send(
`var CFG = {
defaultPageSize: ${config.defaultPageSize},
defaultWaitTime: ${config.defaultWaitTime},
defaultTokenLifeTime: ${config.defaultTokenLifeTime}\n};`);
});
// bulk data implementation
app.use(["/:sim/fhir", "/fhir"], bulkData);
// Generic operation outcomes
app.use("/outcome", encodedOutcome);
// static files
app.use(express.static("static"));
// global error handler
/* istanbul ignore next */
app.use(function (err: Error, req: Request, res: Response, next: NextFunction) {
console.error(err.stack);
res.status(500).send('Something broke!');
});
// If invoked directly start a server (otherwise let the tests do that)
/* istanbul ignore if */
// @ts-ignore
if (!module.parent) {
app.listen(config.port, function() {
console.log("Server listening at " + config.baseUrl);
});
}
module.exports = {
app,
server: http.createServer(app)
};