-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver.ts
More file actions
193 lines (171 loc) · 5.17 KB
/
server.ts
File metadata and controls
193 lines (171 loc) · 5.17 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { getEnvOptions } from '@launchql/env';
import { cors, healthz, poweredBy } from '@pgpmjs/server-utils';
import { PgpmOptions } from '@pgpmjs/types';
import { middleware as parseDomains } from '@launchql/url-domains';
import express, { Express, NextFunction, Request, Response } from 'express';
import { GraphileCache, graphileCache } from 'graphile-cache';
import graphqlUpload from 'graphql-upload';
// Scalar
import { getPgPool } from 'pg-cache';
import { getPgEnvOptions } from 'pg-env';
import { postgraphile } from 'postgraphile';
import { printDatabases, printSchemas } from './render';
import { getGraphileSettings } from './settings';
export const LaunchQLExplorer = (rawOpts: PgpmOptions = {}): Express => {
const opts = getEnvOptions(rawOpts);
const { pg, server } = opts;
const getGraphileInstanceObj = (
dbname: string,
schemaname: string
): GraphileCache => {
const key = `${dbname}.${schemaname}`;
if (graphileCache.has(key)) {
return graphileCache.get(key);
}
const settings = {
...getGraphileSettings({
...opts,
graphile: { schema: schemaname },
}),
graphqlRoute: '/graphql',
graphiqlRoute: '/graphiql',
};
const pgPool = getPgPool(
getPgEnvOptions({
...opts.pg,
database: dbname,
})
);
const handler = postgraphile(pgPool, schemaname, settings);
const obj = {
pgPool,
pgPoolKey: dbname,
handler,
};
graphileCache.set(key, obj);
return obj;
};
const app = express();
healthz(app);
cors(app, server.origin);
app.use(parseDomains());
app.use(poweredBy('launchql'));
app.use(graphqlUpload.graphqlUploadExpress());
app.use(async (req: Request, res: Response, next: NextFunction) => {
if (req.urlDomains?.subdomains.length === 1) {
const [dbName] = req.urlDomains.subdomains;
try {
const pgPool = getPgPool(
getPgEnvOptions({
...opts.pg,
database: dbName,
})
);
const results = await pgPool.query(`
SELECT s.nspname AS table_schema
FROM pg_catalog.pg_namespace s
WHERE s.nspname !~ '^pg_' AND s.nspname NOT IN ('information_schema');
`);
res.send(
printSchemas({
dbName,
schemas: results.rows,
req,
hostname: server.host,
port: server.port,
})
);
return;
} catch (e: any) {
if (e.message?.match(/does not exist/)) {
res.status(404).send('DB Not found');
return;
}
console.error(e);
res.status(500).send('Something happened...');
return;
}
}
return next();
});
app.use(async (req: Request, res: Response, next: NextFunction) => {
if (req.urlDomains?.subdomains.length === 2) {
const [, dbName] = req.urlDomains.subdomains;
try {
const pgPool = getPgPool(
getPgEnvOptions({
...opts.pg,
database: dbName,
})
);
await pgPool.query('SELECT 1;');
} catch (e: any) {
if (e.message?.match(/does not exist/)) {
res.status(404).send('DB Not found');
return;
}
console.error(e);
res.status(500).send('Something happened...');
return;
}
}
return next();
});
app.use(async (req: Request, res: Response, next: NextFunction) => {
if (req.urlDomains?.subdomains.length === 2) {
const [schemaName, dbName] = req.urlDomains.subdomains;
try {
const { handler } = getGraphileInstanceObj(dbName, schemaName);
handler(req, res, next);
return;
} catch (e: any) {
res.status(500).send(e.message);
return;
}
}
return next();
});
app.use(async (req: Request, res: Response, next: NextFunction) => {
if (req.urlDomains?.subdomains.length === 2 && req.url === '/flush') {
const [schemaName, dbName] = req.urlDomains.subdomains;
const key = `${dbName}.${schemaName}`;
graphileCache.delete(key);
res.status(200).send('OK');
return;
}
return next();
});
app.use(async (req: Request, res: Response, next: NextFunction) => {
if (req.urlDomains?.subdomains.length === 0) {
try {
const rootPgPool = getPgPool(
getPgEnvOptions({
...opts.pg,
database: opts.pg.user, // is this to get postgres?
})
);
const results = await rootPgPool.query(`
SELECT * FROM pg_catalog.pg_database
WHERE datistemplate = FALSE AND datname != 'postgres' AND datname !~ '^pg_'
`);
res.send(
printDatabases({ databases: results.rows, req, port: server.port })
);
return;
} catch (e: any) {
if (e.message?.match(/does not exist/)) {
res.status(404).send('DB Not found');
return;
}
console.error(e);
res.status(500).send('Something happened...');
return;
}
}
return next();
});
app.listen(server.port, server.host, () => {
console.log(`app listening at http://${server.host}:${server.port}`);
});
return app;
};