forked from kriasoft/graphql-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
61 lines (45 loc) · 1.48 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
/* SPDX-FileCopyrightText: 2016-present Kriasoft */
/* SPDX-License-Identifier: MIT */
import "./core/source-map-support.js";
import SendGrid from "@sendgrid/mail";
import { default as express } from "express";
import { getApps, initializeApp } from "firebase-admin/app";
import { NotFound } from "http-errors";
import { db, handleError } from "./core/index.js";
import { session } from "./core/session.js";
import env from "./env.js";
import { handleGraphQL, updateSchema } from "./graphql.js";
import { withViews } from "./views/index.js";
const api = withViews(express());
api.enable("trust proxy");
api.disable("x-powered-by");
api.use((req, res, next) => {
if (!req.app.locals.initialized) {
// Configure SendGrid API client
SendGrid.setApiKey(env.SENDGRID_API_KEY);
// Configure Firebase Admin SDK
if (getApps().length === 0) {
initializeApp({ projectId: env.GOOGLE_CLOUD_PROJECT });
}
req.app.locals.initialized = true;
}
next();
});
// Enable body parsing middleware
// http://expressjs.com/en/api.html#express.json
api.use(express.json({ limit: "1024mb" }));
// Authentication middleware
api.use(session);
// GraphQL API middleware
api.use("/api", handleGraphQL);
api.get("/", (req, res) => {
res.render("home");
});
api.get("/favicon.ico", function (req, res) {
res.redirect("https://nodejs.org/static/images/favicons/favicon.ico");
});
api.get("*", function () {
throw new NotFound();
});
api.use(handleError);
export { api, db, env, updateSchema };