forked from coderhive/backend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
65 lines (54 loc) · 1.63 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const express = require("express");
const bodyParser = require("body-parser");
const { graphiqlExpress, graphqlExpress } = require("apollo-server-express");
const { makeExecutableSchema } = require("graphql-tools");
const cors = require("cors");
const { JWT_KEY } = require("./env");
const jwt = require("express-jwt");
const authenticationController = require("./lib/instances/authenticationController");
const PORT = process.env.PORT || 3000;
const renderController = require("./lib/instances/renderController");
const typeDefs = require("./schema");
const resolvers = require("./resolvers");
const app = express();
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.use(cors());
app.use(
jwt({
secret: JWT_KEY,
requestProperty: "jwt.payload",
credentialsRequired: false,
audience: "coder-hive",
issuer: "coder-hive"
})
);
app.use((req, res, next) => {
let authUserId = req.jwt ? req.jwt.payload.sub : undefined;
if (typeof authUserId === "number" && authUserId > 0) {
req.authenticatedUserId = authUserId;
} else {
req.authenticatedUserId = null;
}
next();
});
app.get("/component/:id", renderController.generateHTML);
app.post("/token", authenticationController.token);
app.all("/token", (req, res, next) => {
res.status(405).send("Method Not Allowed");
});
app.use("/graphiql", graphiqlExpress({ endpointURL: "/graphql" }));
app.use(
"/graphql",
graphqlExpress(request => ({
schema,
context: { authenticatedUserId: request.authenticatedUserId }
}))
);
app.listen(PORT);
console.log("Listening on port ", PORT);
module.exports = app;