-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
86 lines (81 loc) · 2.38 KB
/
mod.ts
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
import { app, bullmq, elasticsearch, type express, hyper, minio, mongodb, redis } from './deps.ts';
import { env, verifyAuthorizationHeader } from './utils.ts';
/**
* Given a sub and secret, return a hyper middleware that will
* check that all incoming requests have a properly signed jwt token
* in the authorization header
*/
const authMiddleware =
({ sub, secret }: { sub: string; secret: string }) => (app: express.Express) => {
const verify = verifyAuthorizationHeader({ sub, secret });
app.use(async (req, _res, next) => {
await verify(req.get('authorization') || 'Bearer notoken')
.then(() => next())
// pass error to next, triggering the next error middleware to take over
.catch(next);
});
app.use(
(
// deno-lint-ignore no-explicit-any
err: any,
_req: express.Request,
res: express.Response,
next: express.NextFunction,
): unknown => {
if (err && err.name === 'UnauthorizedError') {
return res.status(401).send({ ok: false, msg: 'not authorized' });
}
// Trigger the next error handler
next(err);
},
);
return app;
};
const MONGO_URL = `mongodb://${encodeURIComponent(env('MONGO_USERNAME'))}:${
encodeURIComponent(env('MONGO_PASSWORD'))
}@${env('MONGO_HOST')}`;
const REDIS_URL = `http://${env('REDIS_HOST')}:${env('REDIS_PORT')}`;
const ELASTICSEARCH_URL = `http://${env('ELASTICSEARCH_HOST')}`;
// Use the public url, so presigned url signatures match
const MINIO_URL = `https://${encodeURIComponent(env('MINIO_USERNAME'))}:${
encodeURIComponent(env('MINIO_PASSWORD'))
}@${env('MINIO_HOST')}.onrender.com`;
export default hyper({
app,
adapters: [
{
port: 'data',
plugins: [mongodb({ url: MONGO_URL })],
},
{
port: 'cache',
plugins: [redis({ url: REDIS_URL })],
},
{
port: 'search',
plugins: [elasticsearch({ url: ELASTICSEARCH_URL })],
},
{
port: 'storage',
plugins: [
minio({
url: MINIO_URL,
bucketPrefix: 'hyper',
useNamespacedBucket: false,
}),
],
},
{
port: 'queue',
plugins: [
bullmq({
url: REDIS_URL,
options: {
keyPrefix: 'hyper',
},
}),
],
},
],
middleware: [authMiddleware({ sub: env('SUB'), secret: env('SECRET') })],
});