-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (40 loc) · 1.36 KB
/
index.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
const fs = require("fs");
const {createGraphQlSchema} = require("oasgraph");
const express = require("express");
const got = require("got");
const graphqlHTTP = require("express-graphql");
const syswidecas = require("syswide-cas");
const port = process.env.PORT || 11456;
const path = process.env.PATHNAME || "/graphql";
const graphiql = process.env.GRAPHIQL === "enabled";
const serverURL = process.env.API_SERVER_URL || "https://kubernetes.default.svc";
const serverCert = process.env.CERTIFICATE_PATH || "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
const serverToken = process.env.TOKEN_PATH || "/var/run/secrets/kubernetes.io/serviceaccount/token";
let token;
try {
token = fs.readFileSync(serverToken);
} catch (e) {
console.error("Missing API token:", e.toString());
process.exit(1);
}
syswidecas.addCAs(serverCert);
const main = async () => {
const oas = await got({
url: serverURL,
path: "/swagger.json",
timeout: 10 * 1e3,
json: true,
}).then((res) => res.body);
const {schema} = await createGraphQlSchema(oas, {
baseUrl: serverURL,
viewer: false,
headers: {Authorization: `Bearer ${token}`},
});
const app = express();
app.use(path, graphqlHTTP({schema, graphiql}));
app.listen(port);
};
main().catch((err) => {
console.error(err.toString());
process.exit(1);
});