This repository was archived by the owner on Aug 16, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
93 lines (80 loc) · 2.52 KB
/
Copy pathserver.js
File metadata and controls
93 lines (80 loc) · 2.52 KB
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
"use strict";
require("dotenv-safe").load();
const cfGraphql = require("cf-graphql");
const express = require("express");
const { getLogger, getConfig } = require("./utils");
const logger = getLogger("server");
const cors = require("cors");
const EventCache = require("./eventCache");
async function startServer({ space, accessToken, hostname, port, basePath }) {
logger.info(`Starting server at ${hostname}:${port}`);
const app = express();
app.use(cors());
const events = new EventCache({ space, accessToken });
app.get("/events", (req, res, next) => {
events
.getLandingPageEvents()
.then(events => {
res.send(
events.map(event => {
// Hide secrets.
delete event.secrets;
return event;
})
);
})
.catch(next);
});
// This API call will request an entry with the specified ID from the space defined at the top, using a space-specific access token.
app.get("/event", (req, res, next) => {
events
.getEvent(req.query.name)
.then(event => {
// Hide secrets.
delete event.secrets;
res.send(event);
})
.catch(next);
});
app.get("/:event/schema", (req, res, next) => {
events
.getSchema(req.params.event)
.then(schema => {
return res.send(schema);
})
.catch(next);
});
app.get("/:locale/:event/graphiql", (req, res) => {
const ui = cfGraphql.helpers.graphiql({
title: "Assembly GraphQL Content",
// TODO: Remove locale specific middleware once https://github.com/contentful-labs/cf-graphql/issues/29 has been resolved.
url: `${basePath}${req.params.locale}/${req.params.event}/graphql`
});
res
.set(ui.headers)
.status(ui.statusCode)
.end(ui.body);
});
app.use("/:locale/:event/graphql", (req, res, next) => {
// TODO: Remove locale specific middleware once https://github.com/contentful-labs/cf-graphql/issues/29 has been resolved.
events
.getApi(req.params.event, req.params.locale)
.then(middleware => middleware(req, res, next))
.catch(next);
});
app.listen(port, hostname);
logger.info("Contentful backend running!");
logger.info(
`You can access GraphiQL at http://${hostname}:${port}/{event}/graphiql/`
);
logger.info(
`You can use the GraphQL endpoint at http://${hostname}:${port}/{event}/graphql/`
);
}
(async () => {
const config = getConfig();
await startServer(config);
})().catch(err => {
logger.fatal(err.message);
process.exit(1);
});