-
Notifications
You must be signed in to change notification settings - Fork 7
/
composite-service.js
150 lines (141 loc) · 4.65 KB
/
composite-service.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const path = require("path");
const { startCompositeService } = require("composite-service");
const { configureHttpGateway } = require("composite-service-http-gateway");
const hasuraContainerName = "hnme_hasura_1"; // dev only
const hasuraImageTag = "v2.0.9.cli-migrations-v3"; // dev only
const authPort = 8000;
const actionsPort = 8001;
const hasuraPort = 8002;
const webPort = 8003;
const dev = process.env.NODE_ENV !== "production";
if (dev) {
// Before starting, make sure the last container created by this script has been removed
require("child_process").spawnSync("docker", ["rm", "--force", hasuraContainerName]);
require("dotenv").config();
}
const {
PATH,
NODE_ENV,
DEBUG,
HASURA_GRAPHQL_ENABLED_LOG_TYPES,
PORT,
HASURA_GRAPHQL_DATABASE_URL,
NODE_PG_SSL_NO_VERIFY,
HASURA_GRAPHQL_ADMIN_SECRET,
NEXTAUTH_URL,
GOOGLE_CLIENT_ID,
GOOGLE_CLIENT_SECRET,
AUTH_JWT_SECRET,
} = process.env;
const commonBackendEnv = {
...(dev && { PATH }), // nodemon needs this
NODE_ENV,
DEBUG,
DATABASE_URL: dev
? HASURA_GRAPHQL_DATABASE_URL.replace("@host.docker.internal:", "@localhost:")
: HASURA_GRAPHQL_DATABASE_URL,
...(NODE_PG_SSL_NO_VERIFY === "true" && { PGSSLMODE: "no-verify" }),
};
const maybeDockerHost = dev ? "host.docker.internal" : "localhost";
const hasuraEnv = {
HASURA_GRAPHQL_ENABLED_LOG_TYPES,
HASURA_GRAPHQL_SERVER_PORT: hasuraPort,
HASURA_GRAPHQL_DATABASE_URL,
HASURA_GRAPHQL_ADMIN_SECRET,
HASURA_GRAPHQL_AUTH_HOOK: `http://${maybeDockerHost}:${authPort}/hasura-auth-hook`,
ACTIONS_URL: `http://${maybeDockerHost}:${actionsPort}`,
};
const HASURA_GRAPHQL_ENDPOINT = `http://localhost:${hasuraPort}`;
startCompositeService({
windowsCtrlCShutdown: true,
services: {
auth: {
cwd: `${__dirname}/auth`,
command: `${dev ? "nodemon --quiet" : "node"} server.js`,
env: {
...commonBackendEnv,
PORT: authPort,
NEXTAUTH_URL,
GOOGLE_CLIENT_ID,
GOOGLE_CLIENT_SECRET,
AUTH_JWT_SECRET,
},
ready: (ctx) => ctx.onceTcpPortUsed(authPort),
},
actions: {
cwd: `${__dirname}/actions`,
command: `${dev ? "nodemon --quiet" : "node"} server.js`,
env: {
...commonBackendEnv,
PORT: actionsPort,
},
ready: (ctx) => ctx.onceTcpPortUsed(actionsPort),
},
hasura: {
dependencies: ["auth", "actions"],
command: dev
? `docker-run-kill
--name ${hasuraContainerName}
${Object.keys(hasuraEnv)
.map((key) => `--env ${key}`)
.join(" ")}
--publish ${hasuraPort}:${hasuraPort}
${
// workaround for https://github.com/docker/for-linux/issues/264
dev && !["win32", "cygwin", "darwin"].includes(process.platform)
? "--add-host=host.docker.internal:host-gateway"
: ""
}
-v ${path.join(__dirname, "hasura", "metadata")}:/hasura-metadata
-v ${path.join(__dirname, "hasura", "migrations")}:/hasura-migrations
hasura/graphql-engine:${hasuraImageTag}`
: `/bin/graphql-engine serve`,
env: dev ? { ...process.env, ...hasuraEnv } : hasuraEnv,
ready: (ctx) => ctx.onceHttpOk({ url: `${HASURA_GRAPHQL_ENDPOINT}/healthz` }),
},
web: {
dependencies: ["hasura"],
cwd: `${__dirname}/web`,
command: `next ${dev ? "dev" : "start"}`,
env: {
PORT: webPort,
HASURA_GRAPHQL_ENDPOINT,
},
ready: (ctx) => ctx.onceTcpPortUsed(webPort),
},
gateway: configureHttpGateway({
dependencies: ["hasura", "auth", "web"],
port: PORT,
routes: {
"/api/auth": { proxy: { target: `http://localhost:${authPort}` } },
"/v1": { proxy: { target: HASURA_GRAPHQL_ENDPOINT } },
"/v1alpha1": { proxy: { target: HASURA_GRAPHQL_ENDPOINT } },
"/v1beta1": { proxy: { target: HASURA_GRAPHQL_ENDPOINT } },
"/healthz": { proxy: { target: HASURA_GRAPHQL_ENDPOINT } },
"/": { proxy: { target: `http://localhost:${webPort}` } },
},
onReady: () => {
if (dev) require("opener")(`http://localhost:${PORT}/`);
},
}),
// services for dev-time only
"hasura-console": dev && {
dependencies: ["hasura"],
cwd: `${__dirname}/hasura`,
command: `hasura console --skip-update-check`,
env: {
HASURA_GRAPHQL_ENDPOINT,
HASURA_GRAPHQL_ADMIN_SECRET,
},
},
"graphql-codegen": dev && {
dependencies: ["hasura"],
cwd: `${__dirname}/web`,
command: `graphql-codegen --watch`,
env: {
HASURA_GRAPHQL_ENDPOINT,
HASURA_GRAPHQL_ADMIN_SECRET,
},
},
},
});