This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.ts
125 lines (111 loc) · 3.47 KB
/
app.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
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
// Dotenv is a zero-dependency module that loads environment
// variables from a .env file into process.env
import "dotenv/config";
import * as Sentry from "@sentry/serverless";
import aws_lambda from "aws-lambda";
import compression from "compression";
import cors from "cors";
import express, { ErrorRequestHandler, Request, RequestHandler } from "express";
import expressPlayground from "graphql-playground-middleware-express";
import logger from "morgan";
import path from "path";
import serverless from "serverless-http";
import graphQLRouter from "./graphql/router";
import { createErrorJSON } from "./helpers/errors.helper";
import restRouter from "./rest/versionController";
const app = express();
app.set("trust proxy", 1);
if (process.env.NODE_ENV == "production") {
Sentry.AWSLambda.init({
dsn: process.env.SENTRY_DSN,
tracesSampleRate: 1.0,
});
}
const logging: RequestHandler = (
req: Request<unknown, unknown, { query: string }>,
res,
next
) => {
if (process.argv.includes("--log") || process.env.NODE_ENV == "production") {
const event = {
referer: req.headers.referer,
method: req.method,
url: req.originalUrl,
body: req.body.query,
};
console.log("REQUEST\n" + JSON.stringify(event, null, 2));
res.on("finish", () => {
const finishEvent = {
statusCode: res.statusCode,
statusMessage: res.statusMessage,
};
if (finishEvent.statusCode >= 400) {
console.error("RESPONSE\n" + JSON.stringify(finishEvent, null, 2));
} else {
console.log("RESPONSE\n" + JSON.stringify(finishEvent, null, 2));
}
});
}
next();
};
app.use(cors());
app.use(
compression({
level: 4, // using fourth fastest compression level: https://www.npmjs.com/package/compression
threshold: "128kb",
filter: (req, res) => {
if (req.headers["x-no-compression"]) {
// don't compress responses if this request header is present
return false;
}
// fallback to standard compression
return compression.filter(req, res);
},
})
);
app.use(logger("dev"));
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
app.use(express.static("./docs-site"));
app.use(express.static("./graphql/docs"));
app.set("view engine", "ejs");
app.use("/rest", logging, restRouter);
app.use("/graphql", logging, graphQLRouter);
app.use("/graphql-playground", expressPlayground({ endpoint: "/graphql/" }));
app.use("/docs", express.static("docs-site"));
app.get("/", function (req, res) {
res.redirect("docs");
});
// the 404 Route (ALWAYS keep this as the last route)
app.get("*", function (req, res) {
res
.status(404)
.json(
createErrorJSON(404, "Not Found", "The requested resource was not found.")
);
});
// error handler
const errorHandler: ErrorRequestHandler = (
err: { message: string; status: number },
req,
res
) => {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
const status = err.status || 500;
res.status(status).json(createErrorJSON(status, err.message, ""));
};
app.use(errorHandler);
let serverless_handler: aws_lambda.Handler | serverless.Handler = serverless(
app,
{
binary: ["image/*"],
}
);
if (process.env.NODE_ENV == "production") {
serverless_handler = Sentry.AWSLambda.wrapHandler(serverless_handler);
}
export default app;
export const handler = serverless_handler;