forked from davbeck/Postgraphile-todoMVC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (44 loc) · 1.4 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
require("dotenv").config();
const path = require("path");
const express = require("express");
const cookieParser = require("cookie-parser");
const { postgraphile } = require("postgraphile");
const PgSimplifyInflectorPlugin = require("@graphile-contrib/pg-simplify-inflector");
const parseClaims = require("./auth");
const app = express();
app.use(cookieParser());
app.use(
postgraphile(
process.env.GRAPHILE_URL ||
"postgres://todo_graphile:password@localhost/todo?sslmode=disable",
"app",
{
dynamicJson: true,
appendPlugins: [PgSimplifyInflectorPlugin],
enhanceGraphiql: true,
graphiql: process.env.NODE_ENV !== "production",
watchPg: process.env.NODE_ENV !== "production",
ignoreRBAC: false,
pgSettings: async req => {
try {
const claimes = await parseClaims(req);
return {
role: "todo_user",
"user.id": claimes.sub,
};
} catch (error) {
console.error("failed to authenticate", error);
return { role: "todo_anonymous" };
}
},
}
)
);
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "client/build")));
// Handle React routing, return all requests to React app
app.get("*", function(req, res) {
res.sendFile(path.join(__dirname, "client/build", "index.html"));
});
}
app.listen(process.env.PORT || 44593);